在发布我的问题之前,我搜索了很多关于它的问题,我发现了一些可能类似的问题,但它们并没有解决我的问题。我相信这很容易,但我不知道如何:
下面是问题的最小例子:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(574, 521)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.firstPushButton.clicked.connect(self.showDialog)
# the other stuff related to layout setup is ommited
def showDialog(self):
dialog = MyDialog(MainWindow)
dialog.exec()
class MyDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(400, 200)
self.myButton = QtWidgets.QPushButton("Write something")
# When I click the myButton, I want it to change the text of MainWindow lineEdit
self.myButton.clicked.connect(self.writeHello)
def writeHello(self):
# How can I access firstLineEdit from MainWindow? I want to write "Hello" to the firstLineEdit
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.setWindowTitle("BEM Analysis for propellers")
MainWindow.show()
sys.exit(app.exec())
你能告诉我如何实现writeHello()
方法,以便在MainWindow中的firstLineEdit
中写一些东西
谢谢
首先,你不应该修改Qt Designer生成的代码,因为它不是一个GUI,它只是一个填充GUI的类,并带来一些不便,例如无法覆盖小部件的方法,或者一些您想要在该类中使用窗口小部件的方法。相反,它从一个合适的小部件继承,并使用另一个类作为接口。
说到你不应该混合类,因为它们之间会有很多依赖关系,将来如果你修改一个类,你将不得不修改另一个类是无与伦比的,而是你使用信号通知任何改变或行动。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
#MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(574, 521)
MainWindow.setWindowIcon(QtGui.QIcon(':/icons/drone.ico'))
MainWindow.setIconSize(QtCore.QSize(32, 32))
self.centralwidget = QtWidgets.QWidget(MainWindow)
MainWindow.setCentralWidget(self.centralwidget)
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
self.firstPushButton = QtWidgets.QPushButton(self.centralwidget)
self.firstLineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.verticalLayout_2.addWidget(self.firstPushButton)
self.verticalLayout_2.addWidget(self.firstLineEdit)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.firstPushButton.clicked.connect(self.showDialog)
def showDialog(self):
dialog = MyDialog()
dialog.clicked.connect(self.writeHello)
dialog.exec()
@QtCore.pyqtSlot()
def writeHello(self):
self.firstLineEdit.setText('Hello')
class MyDialog(QtWidgets.QDialog):
clicked = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(400, 200)
self.myButton = QtWidgets.QPushButton("Write something")
# When I click the myButton, I want it to change the text of MainWindow lineEdit
self.myButton.clicked.connect(self.clicked)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.myButton)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.setWindowTitle("BEM Analysis for propellers")
w.show()
sys.exit(app.exec())