gpt4 book ai didi

python - 如何在pyqt中将数据从对话框窗口检索到主窗口?

转载 作者:太空宇宙 更新时间:2023-11-03 14:03:28 24 4
gpt4 key购买 nike

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

这是主文件代码

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

这是对话框文件代码

我有一个主窗体,上面有一个 lineedit l1 和一个按钮 B1。单击按钮 B1 将打开一个新的对话窗口。对话框窗口有一个 lineedit l2 和一个按钮 B2。我在 l2 中写了一些东西,然后在对话框窗口中单击 b2。我如何在主窗口中单击 b2 时将 l2 中的数据发送到 l1,并在单击按钮 b2 并将控制返回到主窗口后关闭对话框窗口

最佳答案

始终建议不要弄乱生成 Qt Designer 的代码,最好创建另一个实现逻辑的文件。此外,Qt Designer 生成一个设计类,它不是一个小部件,但用于填充小部件。所以建议创建一个实现逻辑并使用设计的类。

进入正题,如果你想在按下 QPushButton 时关闭对话框,那么将 clicked 信号连接到 close() 方法。如果您想获取 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_())

注意:我建议删除您在设计中所做的更改或使用 pyuic 恢复它们

关于python - 如何在pyqt中将数据从对话框窗口检索到主窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46401601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com