I'm attempting to add a custom dialog box to my current GUI that can be launched for the user to set some parameters. Ideally, I would like to create the custom dialog using QtDesigner. Below is the code generated by pyuic4
from the QtDesigner ui code for the dialog box.
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(508, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(150, 250, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 120, 181, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.sl_value = QtGui.QSlider(Dialog)
self.sl_value.setGeometry(QtCore.QRect(220, 120, 161, 31))
self.sl_value.setOrientation(QtCore.Qt.Horizontal)
self.sl_value.setObjectName("sl_value")
self.ed_value = QtGui.QLineEdit(Dialog)
self.ed_value.setGeometry(QtCore.QRect(400, 120, 41, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.ed_value.setFont(font)
self.ed_value.setObjectName("ed_value")
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", "Set example value:", None, QtGui.QApplication.UnicodeUTF8))
This is saved in Sub2.py
Then, in my main python file, I add
from Sub2 import Ui_Dialog
I create a new class called StartSub2
with the following code
class StartSub2(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.ui = Ui_Dialog
self.ui.setupUi(self)
Then finally in inside my main GUI there's a function with the following code that should launch the dialog
def exampleSubGui(self):
dialog = StartSub2(self)
result = dialog.exec_()
Please note that the dialog is not done. Once I resolve how to even launch it I will add signal/slot connections for the slider and edit box. Also, if I understand it correctly, I need to overload the accept()
method to return the user's input.
The first problem I run into is with the __init__
method of StartSub2
. I get the following error:
TypeError: unbound method setupUi() must be called with Ui_Dialog instance as
first argument (got StartSub2 instance instead)
I'm attempting to take the same approach that the main GUI is taking which uses the following code
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
But this does not complain about setupUi()
getting a StartQT4
instance instead of a Ui_MainWindow
instance. Can anyone explain the proper way to accomplish what I'm trying to do? Or can somebody point me to a clear example or reference? Please let me know if you need more information or clarification.
class StartSub2(QtGui.QDialog, Ui_Dialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.setupUi(self)
should resolve your first problem of getting the dialog to initialize.
To get info back I usually add a method called something like getValues
to StartSub2
, i.e.
def getValues(self):
return somethingUseful
then do
dlg = StartSub2()
if dlg.exec_():
values = dlg.getValues()
# Do stuff with values
Just wanted to provide my own answer for setting up a custom dialog with return values (not answering the code specific questions, which Whatang already did).
I found it a bit cleaner to construct a simple dialog class with a class method that can return different things as needed. I've been making these a lot lately! The idea is that the class method will construct an instance of the dialog class, and return objects out of the instance (in this case the bool ok
) which is more or less a factory method (from what I understand anyway, still somewhat new to OOP).
Here's a very simplified example dialog. It should be relatively easy to extend this to whatever you need within the dialog class:
class OkDialog(QtGui.QDialog):
def __init__(self, parent):
super(OkDialog, self).__init__(parent)
self.ok = False
self.btn_ok = QtGui.QPushButton("Ok", self)
self.btn_ok.clicked.connect(self.button_press)
self.btn_cancel = QtGui.QPushButton("Cancel", self)
self.btn_cancel.clicked.connect(self.button_press)
self.btn_cancel.move(80, 0)
def button_press(self):
if self.sender() == self.btn_ok:
self.ok = True
self.close()
@classmethod
def isOkay(cls, parent):
dialog = cls(parent)
dialog.exec_()
return dialog.ok
The beauty is that when it it comes time to construct this dialog you can do it with just one line OkDialog.isOkay(parent)
. Putting it together into a fully working sample:
import sys
from PyQt4 import QtCore, QtGui
class OkDialog(QtGui.QDialog):
def __init__(self, parent):
super(OkDialog, self).__init__(parent)
self.ok = False
self.btn_ok = QtGui.QPushButton("Ok", self)
self.btn_ok.clicked.connect(self.button_press)
self.btn_cancel = QtGui.QPushButton("Cancel", self)
self.btn_cancel.clicked.connect(self.button_press)
self.btn_cancel.move(80, 0)
def button_press(self):
if self.sender() == self.btn_ok:
self.ok = True
self.close()
@classmethod
def isOkay(cls, parent):
dialog = cls(parent)
dialog.exec_()
return dialog.ok
class Ui_Dialog(QtGui.QDialog):
def __init__(self):
super(Ui_Dialog, self).__init__()
button = QtGui.QPushButton("Launch custom dialog", self)
button.pressed.connect(self.launch_dialog)
def launch_dialog(self):
print OkDialog.isOkay(self)
def main():
app = QtGui.QApplication(sys.argv)
ex = Ui_Dialog()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With