簡體   English   中英

PyQt:如何將所選的組合框值從QDialog傳遞到主窗口?

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

我一直堅持將值從子類(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窗口,並從標記為<----- HERE--的位置打印出來。 我不想使用全局變量。 誰能指出我的正確方向? 我應該嘗試操縱插槽信號機制嗎? 還是嘗試重載accept()方法?

感謝ekhumoro,答案是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM