Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

python - PyQt4: Create a custom dialog that returns parameters

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...