如何将值从主传递到对话框
Posted
技术标签:
【中文标题】如何将值从主传递到对话框【英文标题】:How to passing value from main to dialog 【发布时间】:2019-02-27 17:05:44 【问题描述】:我是使用 Pyqt5 的新手。 我使用 qtdesigner 来构建 GUI。
我有 MainWindow 用于将值传递给对话框窗口
我想 LineEdit(在对话框窗口中) 在用户输入并单击按钮后显示值(在主窗口中)
我试试
self.ui = Ui_Dialog(self,data)
但它不起作用 我的代码主页 主窗口.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
class Ui_MainWindow(object):
def openDialog(self):
data = self.lineEdit.text()
self.window = QtWidgets.QDialog()
self.ui = Ui_Dialog(self,data)
self.ui.setupUi(self.window)
# MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(505, 236)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(120, 40, 91, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(290, 40, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 110, 93, 28))
self.pushButton.clicked.connect(self.openDialog)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 505, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Passing Value"))
self.pushButton.setText(_translate("MainWindow", "Send"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
对话框代码.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(577, 253)
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(260, 100, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(180, 100, 55, 16))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Value"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
请给我建议 谢谢, 薄荷
【问题讨论】:
【参考方案1】:PyQt 建议不要修改由 pyuic 和 Qt Designer 生成的 .py,而是创建另一个使用该类来填充小部件的文件,因此我建议重新生成 MainWindow.py 和 dialog.py 文件。
现在创建一个 main.py,我们通过设置具有要求的构造函数来继承适当的类:
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from dialog import Ui_Dialog
from MainWindow import Ui_MainWindow
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, text, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText(text)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.openDialog)
def openDialog(self):
data = self.lineEdit.text()
w = Dialog(data)
w.exec_()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
【讨论】:
它有效! .非常感谢兄弟。你让我知道了很多值得学习的东西。以上是关于如何将值从主传递到对话框的主要内容,如果未能解决你的问题,请参考以下文章
如何将值从 tableview 传递到 collectionview