Pregunta

I am working with python and pyqt. I have a dialog that I want to temporarily hide. After calling

dlg.hide()

I try calling

dlg.show()

but nothing happens. It is never re-displayed.

I am new to pyqt so any help is greatly appreciated.

Thanks in advance.

¿Fue útil?

Solución

You are looking for the exec_ method that makes the dialog modal, see how this works:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui

class myDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(myDialog, self).__init__(parent)

        self.dialog = None

        self.buttonShow = QtGui.QPushButton(self)
        self.buttonShow.setText("Show Dialog")
        self.buttonShow.clicked.connect(self.on_buttonShow_clicked)

        self.buttonHide = QtGui.QPushButton(self)
        self.buttonHide.setText("Close")
        self.buttonHide.clicked.connect(self.on_buttonHide_clicked)

        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.buttonShow)
        self.layout.addWidget(self.buttonHide)

    @QtCore.pyqtSlot()
    def on_buttonHide_clicked(self):
        self.accept()

    @QtCore.pyqtSlot()
    def on_buttonShow_clicked(self):
        self.dialog = myDialog(self)
        self.dialog.exec_()

class myWindow(QtGui.QWidget):  
    def __init__(self, parent=None):
        super(myWindow, self).__init__(parent)

        self.buttonShow = QtGui.QPushButton(self)
        self.buttonShow.setText("Show Dialog")
        self.buttonShow.clicked.connect(self.on_buttonShow_clicked)

        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.buttonShow)

        self.dialog = myDialog(self)

    @QtCore.pyqtSlot()
    def on_buttonHide_clicked(self):
        self.dialog.accept()

    @QtCore.pyqtSlot()
    def on_buttonShow_clicked(self):
        self.dialog.exec_()

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myWindow')

    main = myWindow()
    main.show()

    sys.exit(app.exec_())
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top