[PyQt] Qt4: Open a QDialog.

Tina I tinaweb at bestemselv.com
Mon Mar 12 08:44:48 GMT 2007


I'm having some problems moving from Qt3 to Qt4. Kinda stuck in old 
habits I guess.
Anyway, my current problem is to simply open a dialog window I have made 
with Designer but I can't for the life of me understand how to do it. In 
Qt3 it was just a matter of importing the dialog and do something like 
"if dialog.exec_loop() == QDialog.Accepted:". I have looked at the 
examples but I'm none the wiser I'm afraid.

My test code:
Main program:
-----------------------

import sys
from PyQt4 import QtGui , QtCore

from testGUI import Ui_MainWindow
from testdialog import Ui_Dialog #Not sure about this???

class testmain(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self )
       
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
       
        self.connect(self.ui.testButton, QtCore.SIGNAL("clicked()"), 
self.doClicked)
        self.connect(self.ui.dialogButton, QtCore.SIGNAL("clicked()"), 
self.doDialog)
       
    def doClicked(self):
        self.ui.textEdit.insertPlainText("You clicked the button!")
   
    def doDialog(self):
        ''' Want to do the equivalent to the Qt3 "if dialog.exec_loop() 
== QDialog.Accepted:"
        That is, open the 'testdialog.py' and check if the user click OK 
or Cancel'''
        pass   
       
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    f = testmain()
    f.show()
    sys.exit(app.exec_())

----------------------------------
The dialog: tesdialog.py
---------------------------------
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'testDialog.ui'
#
# Created: Mon Mar 12 08:57:12 2007
#      by: PyQt4 UI code generator 4.1.1
#
# WARNING! All changes made in this file will be lost!

import sys
from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        
Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,142).size()).expandedTo(Dialog.minimumSizeHint()))

        self.gridlayout = QtGui.QGridLayout(Dialog)
        self.gridlayout.setMargin(9)
        self.gridlayout.setSpacing(6)
        self.gridlayout.setObjectName("gridlayout")

        self.label = QtGui.QLabel(Dialog)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.gridlayout.addWidget(self.label,0,0,1,1)

        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.NoButton|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.gridlayout.addWidget(self.buttonBox,1,0,1,1)

        self.retranslateUi(Dialog)
        
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("accepted()"),Dialog.accept)
        
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("rejected()"),Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", 
"Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Dialog", "Click 
OK or Cancel", None, QtGui.QApplication.UnicodeUTF8))

-------------------------------------
If more info is needed let me know. I have also put the entire thing, 
including the .ui files at:
http://www.bestemselv.com/admin_priv/dialogtest.tar.gz

Thanks
Tina


More information about the PyQt mailing list