PyQt:如何将选择的 Combobox 值从 QDialog 传递到 main window?

PyQt: How to pass chosen Combobox value from QDialog to main window?

我一直坚持将子项 class (QDialog) 的值传递给其父项 class(在我的示例 MainApp 中)。 这是 main window 的 QtDesigner 派生文件。它由window、文本标签和按钮组成。

# -*- coding: utf-8 -*-
# Created by: PyQt4 UI code generator 4.12
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(218, 184)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName(_fromUtf8("label"))
        self.verticalLayout.addWidget(self.label)
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.label.setText(_translate("MainWindow", "TextLabel", None))
        self.pushButton.setText(_translate("MainWindow", "PushButton", None))

下面是dialog.py定义。它包含组合框和按钮。

# -*- coding: utf-8 -*-

# Created by: PyQt4 UI code generator 4.12
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 284)
        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(Qt.Gui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName(_fromUtf8("buttonBox"))
        self.comboBox = QtGui.QComboBox(Dialog)
        self.comboBox.setGeometry(QtCore.QRect(100, 40, 201, 22))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))

下面是主要 class 及其方法:

from PyQt4 import QtGui #import the PyQt4 module we need
from PyQt4.Qt import QDialog
import sys #sys needed so we can pass argv to QApplication
import mainwin
import dialog


class MainApp(QtGui.QMainWindow, mainwin.Ui_MainWindow):
    '''Main window of the application'''
    def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)

        self.setupUi(self) # This is defined in design.py file automatically
                           # It sets up layout and widgets that are defined
        self.label.setText("I am unchanged text")
        self.pushButton.clicked.connect(self.openDialog)#button binding - call openDialog method

    def openDialog(self): # Opening a new popup window...
        self.dialog = MyDialog()
        self.a = self.dialog.exec_() #exec_() for python2.x, before python3


        if self.a == self.dialog.Accepted:
            self.label.setText("Accepted") #<-----HERE--
        elif self.a == self.dialog.Rejected: #0
            self.label.setText("Rejected")



class MyDialog(QtGui.QDialog, dialog.Ui_Dialog):

    def __init__(self, parent = MainApp):
        QDialog.__init__(self)
        self.setupUi(self) #takes ui from my-defined dialog.py

        self.comboBox.addItem('item 1');#adding items to the QCombobox to be displayed
        self.comboBox.addItem('item 2');
        self.comboBox.addItem('item 3');
        self.comboBox.addItem('special1');

        self.comboBox.activated[str].connect(self.my_method)

    def my_method(self, my_text):
        print my_text
        #return my_text


def main():
    app = QtGui.QApplication(sys.argv)# A new instance of QApplication
    form = MainApp() # We set the form to be our ExampleApp (design)
    form.show() # Show the form
    app.exec_() # and execute the app

if __name__ == '__main__': #if the app is running directly, and is not imported
    main()

我想将位于 Dialog 上的组合框('item1' 或 'item2' 等等...)中的选定文本传递到 MainApp window 并从该位置打印标记为 <-----此处--。 我宁愿不使用全局变量。 谁能指出我的好方向?我应该尝试操纵槽信号机制吗?或者尝试重载 accept() 方法?

感谢 ekhumoro,答案是:

self.label.setText(self.dialog.comboBox.currentText())