[PyQt] How do you close a QDialog?

Demetrius Cassidy dcassidy36 at mass.rr.com
Fri Apr 10 16:27:29 BST 2009


You are still doing the same thing as last time =(

Ok, first of all, separate your GUI code from your dialog code. Use Qt
Designer to create your GUI, then pyuic to convert the ui code into python
equivalent. This will create a python file with a class called Ui_MainWindow
or Ui_Dialog, depending on the type of window.

Create a new class, say MyAppMainWindow, and derive it from both the Ui code
(Ui_MainWindow) and QMainWindow. In __init__, call
QMainWindow.__init__(self) and self.setupUi(self).

Your code will look just like my previous example, except that MainWindow
will contain a button to open a dialog. 

Here is some sample code, with the GUI code omitted since it's irrelevant.

import sys
from PyQt4.QtGui import QMainWindow, QDialog

class MyAppMainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        
        #same as self.connect(self.pushButton, SIGNAL('clicked'),
self.openDialog) but more Pythonic.
        self.pushButton.connect(self.openDialog) 
        
    def openDialog(self):
        self.dlg = MyAppDialog()
        self.dlg.show()
        
    
class MyAppDialog(QDialog, Ui_Dialog):
    def __init__(self):
        QDialog.__init__(self)
        self.setupUi(self)
        self.pushButton.connect(self.close)
        
        
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = MyAppMainWindow()
    MainWindow.show()
    sys.exit(app.exec_())


    



GatorAlli wrote:
> 
> The mainwindow is a QMainWindow.
> 
> Here's some code that has the problem.
> 
> ___________main.py______________________________________________________________
> from PyQt4 import QtCore, QtGui
> class Ui_MainWindow(object):
>     def setupUi(self, MainWindow):
>         MainWindow.setObjectName("MainWindow")
>         MainWindow.resize(154, 96)
>         self.centralwidget = QtGui.QWidget(MainWindow)
>         self.centralwidget.setObjectName("centralwidget")
>         self.gridLayout = QtGui.QGridLayout(self.centralwidget)
>         self.gridLayout.setObjectName("gridLayout")
>         self.pushButton = QtGui.QPushButton(self.centralwidget)
>         self.pushButton.setObjectName("pushButton")
>         self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
>         MainWindow.setCentralWidget(self.centralwidget)
>         self.menubar = QtGui.QMenuBar(MainWindow)
>         self.menubar.setGeometry(QtCore.QRect(0, 0, 154, 21))
>         self.menubar.setObjectName("menubar")
>         MainWindow.setMenuBar(self.menubar)
>         self.statusbar = QtGui.QStatusBar(MainWindow)
>         self.statusbar.setObjectName("statusbar")
>         MainWindow.setStatusBar(self.statusbar)
>         self.retranslateUi(MainWindow)
>         QtCore.QObject.connect(self.pushButton,
> QtCore.SIGNAL("clicked()"), self.display)
>         QtCore.QMetaObject.connectSlotsByName(MainWindow)
>     def retranslateUi(self, MainWindow):
>        
> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow",
> "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
>         self.pushButton.setText(QtGui.QApplication.translate("MainWindow",
> "Show the QDialog", None, QtGui.QApplication.UnicodeUTF8))
>     def display(self):
>         import d
>         import sys
>         app = QtGui.QApplication(sys.argv)
>         ui = d.sprite_dialog()
>         ui.exec_()  
>         #sys.exit(app.exec_())
> if __name__ == "__main__":
>     import sys
>     app = QtGui.QApplication(sys.argv)
>     MainWindow = QtGui.QMainWindow()
>     ui = Ui_MainWindow()
>     ui.setupUi(MainWindow)
>     MainWindow.show()
>     sys.exit(app.exec_())
> _______________________________________________________________________________
> 
> 
> ___________d.py________________________________________________________________
> from PyQt4 import QtCore, QtGui
> class Ui_Dialog(object):
>     def setupUi(self, Dialog):
>         Dialog.setObjectName("Dialog")
>         Dialog.resize(400, 300)
>         self.gridLayout = QtGui.QGridLayout(Dialog)
>         self.gridLayout.setObjectName("gridLayout")
>         self.pushButton = QtGui.QPushButton(Dialog)
>         self.pushButton.setObjectName("pushButton")
>         self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
>         self.retranslateUi(Dialog)
>         QtCore.QObject.connect(self.pushButton,
> QtCore.SIGNAL("clicked()"), self._close_)
>         QtCore.QMetaObject.connectSlotsByName(Dialog)
>     def retranslateUi(self, Dialog):
>         Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog",
> "Dialog", None, QtGui.QApplication.UnicodeUTF8))
>         self.pushButton.setText(QtGui.QApplication.translate("Dialog",
> "Close this dialog", None, QtGui.QApplication.UnicodeUTF8))
> class sprite_dialog(QtGui.QDialog, Ui_Dialog):
>     def __init__(self): 
>         QtGui.QDialog.__init__(self) 
>         self.setupUi(self) 
>     def _close_(self):
>         self.hide()
> ________________________________________________________________________
> 
> Put main.py and d.py in the same directory. 
> Run main.py.
> Click "Show the QDialog."
> Click "Close this dialog".
> Try clicking "Show the QDialog" again.
> -no response-       
> 
> There's the problem.
> 

-- 
View this message in context: http://www.nabble.com/How-do-you-close-a-QDialog--tp22874235p22989724.html
Sent from the PyQt mailing list archive at Nabble.com.



More information about the PyQt mailing list