简体   繁体   English

如何从对话框窗口中检索数据到pyqt中的主窗口?

[英]How to retrieve data from dialog window to mainwindow in pyqt?

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

# Form implementation generated from reading ui file 'maintest.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from test import Ui_Dialog

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 connect(self):
        self.updateWindow=QtGui.QDialog()
        self.ui_update=Ui_Dialog()
        self.ui_update.setupUi(self.updateWindow)
        self.updateWindow.show()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(391, 248)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(80, 60, 113, 27))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(80, 100, 112, 34))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 391, 31))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "B1", None))
        self.pushButton.clicked.connect(self.connect)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

this was the main file code 这是主要的文件代码

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

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# 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 save_data(self):
        d= self.lineEdit.text()
        print(d)

    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(397, 219)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(110, 100, 112, 34))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(110, 60, 113, 27))
        self.lineEdit.setText(_fromUtf8(""))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "B2", None))
        self.pushButton.clicked.connect(self.save_data)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

this is the dialog file code 这是对话框文件代码

i have a main window form which has a lineedit l1 and a button B1 on it. 我有一个主窗口形式,其上有一个lineedit l1和一个按钮B1。 on clicking the button B1 a new dialog window opens. 单击按钮B1时,将打开一个新的对话框窗口。 dialog window has a lineedit l2 and a button B2. 对话框窗口有一个lineedit l2和一个按钮B2。 i write something in the l2 and then click b2 in dialog window. 我在l2中写了一些东西,然后在对话框窗口中单击b2。 how can i send my data in l2 on clicking b2 to l1 in mainwindow and also close dialog window after button b2 click and return control to mainwindow 如何在主窗口中单击b2到l1时在l2中发送我的数据,并在按钮b2单击并将控制返回到主窗口后关闭对话窗口

It is always advisable not to mess up the code that generates Qt Designer, it is better to create another file where the logic is implemented. 始终建议不要弄乱生成Qt Designer的代码,最好创建另一个实现逻辑的文件。 In addition Qt Designer generates a design class that is not a widget but serves to fill the widget. 此外,Qt Designer生成的设计类不是小部件,而是用于填充小部件。 So it is recommended to create a class that implements logic and use the design. 因此,建议创建一个实现逻辑并使用该设计的类。

Going to the point, if you want to close the dialog when the QPushButton is pressed then connect the clicked signal to the close() method. 要点,如果要在按下QPushButton时关闭对话框,则将clicked信号连接到close()方法。 If you want to get some data of a QDialog one of the simplest forms is first to make the dialog is shown using the exec_() method since it generates its own loop and when the window is closed, the following lines of code are executed. 如果你想获得QDialog一些数据,最简单的形式之一是首先使用exec_()方法显示对话框,因为它生成自己的循环,当窗口关闭时,执行以下代码行。

class Dialog(QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.close)

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.onClicked)

    def onClicked(self):
        updateDialog = Dialog()
        updateDialog.exec_()
        self.lineEdit.setText(updateDialog.lineEdit.text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

Note: I recommend removing the changes you made in the designs or get them back with pyuic 注意:我建议删除您在设计中所做的更改或使用pyuic将其恢复

You can connect the Button on your Dialog with a Function in your Main Application just like you already have done it. 您可以将Dialog上的Button与主应用程序中的Function连接,就像您已经完成的那样。

Update your connect routine like this: 像这样更新你的connect例程:

def connect(self):
        self.updateWindow=QtGui.QDialog()
        self.ui_update=Ui_Dialog()
        self.ui_update.setupUi(self.updateWindow)
        self.ui_update.pushButton.connect(self.printContent)
        self.updateWindow.show()

with

def printContent(self):
     print self.ui_update.lineEdit.text()

You should also be careful with the names of your variables. 您还应该小心变量的名称。 Errors will occour in the future if you use placeholders that are already taken. 如果您使用已经占用的占位符,将来会出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM