PyQt:如何将选择的 Combobox 值从 QDialog 传递到主窗口?

Posted

技术标签:

【中文标题】PyQt:如何将选择的 Combobox 值从 QDialog 传递到主窗口?【英文标题】:PyQt: How to pass chosen Combobox value from QDialog to main window? 【发布时间】:2017-08-21 15:43:10 【问题描述】:

我一直坚持将值从子类 (QDialog) 传递到其父类(在我的示例 MainApp 中)。 这是一个用于主窗口的 QtDesigner 派生文件。它由窗口、文本标签和按钮组成。

# -*- 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))

下面是主类及其方法:

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 上的 Combobox('item1' 或 'item2' 等...)中的选定文本传递到 MainApp 窗口,并从标记为

【问题讨论】:

self.label.setText(self.dialog.comboBox.currentText()). 谢谢你,ekhumoro。这就是解决方案。 【参考方案1】:

感谢ekhumoro,答案是:

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

【讨论】:

以上是关于PyQt:如何将选择的 Combobox 值从 QDialog 传递到主窗口?的主要内容,如果未能解决你的问题,请参考以下文章

如何正确地将 ComboBox 的模型从 python (pyQt5) 传递给 QML?

Kendo ComboBox - 如何根据其文本()而不是值()选择选项?

在pyqt中ComboBox项目更改后动态更改UI

如何正确地将值从 pyqt 返回到 JavaScript?

如何将值从主传递到对话框

通过单击提交按钮将值从一个 jsp 移动到另一个 [重复]