如何在 PyQt5 中使用一个 class 中的信号调用另一个中的函数?
How do I use the signal in one class to call a function in another in PyQt5?
我想在我的 PyQt5 应用程序中创建一个按钮,它将 window 标题名称更改为新值。
我试图在 class PushButton(QPushButton) 中创建一个自定义函数,它会告诉 MainWindow 将 setWindowTitle 设置为一个新名称。然后我将 PushButton class 中的 .clicked 信号连接到该函数,但是,每当我按下按钮时,应用程序就会崩溃。
我是新人,所以如果我class/function关系有什么不对的,请指正。
我在这里错过了什么?
windowTitle : QString
This property holds the window title (caption)
This property only makes sense for top-level widgets, such as windows and dialogs. If no caption has been set, the title is based of the windowFilePath. If neither of these is set, then the title is an empty string.
setWindowTitle(const QString &)
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Change the window title name')
self.resize(300, 150)
self.lineEdit = QLineEdit(self)
self.lineEdit.move(30, 65)
button = QPushButton("Button", self)
button.clicked.connect(self.onClicked)
button.move(190, 65)
def onClicked(self):
self.setWindowTitle(self.lineEdit.text()) # <---
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
我想在我的 PyQt5 应用程序中创建一个按钮,它将 window 标题名称更改为新值。
我试图在 class PushButton(QPushButton) 中创建一个自定义函数,它会告诉 MainWindow 将 setWindowTitle 设置为一个新名称。然后我将 PushButton class 中的 .clicked 信号连接到该函数,但是,每当我按下按钮时,应用程序就会崩溃。
我是新人,所以如果我class/function关系有什么不对的,请指正。
我在这里错过了什么?
windowTitle : QString
This property holds the window title (caption)
This property only makes sense for top-level widgets, such as windows and dialogs. If no caption has been set, the title is based of the windowFilePath. If neither of these is set, then the title is an empty string.
setWindowTitle(const QString &)
import sys
from PyQt5.QtWidgets import (QPushButton, QWidget, QLineEdit, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Change the window title name')
self.resize(300, 150)
self.lineEdit = QLineEdit(self)
self.lineEdit.move(30, 65)
button = QPushButton("Button", self)
button.clicked.connect(self.onClicked)
button.move(190, 65)
def onClicked(self):
self.setWindowTitle(self.lineEdit.text()) # <---
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())