PyQt5:如何将 QPushButton 连接到插槽? [英] PyQt5: How can I connect a QPushButton to a slot?

查看:58
本文介绍了PyQt5:如何将 QPushButton 连接到插槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,几乎每个教程/可理解的人类语言文档都是针对 PyQt4 的.但是,PyQt5 改变了整个连接按钮到插槽"的工作方式,我仍然不知道如何去做.

Okay, so pretty much every tutorial/understandable-written-in-human-language-documentation is for PyQt4. But, PyQt5 changed how the whole 'connect button to a slot' works, and I still can't figure it out how to do it.

我在 QtDesigner 中做了一个快速的 gui,我有一个 QPushButton 和一个标签.当我单击按钮时,我希望标签上的文本发生变化.在 QtDesigner 中的 C++ 中,很容易将两者连接起来.但是我必须全部用python编写.

I did a quick gui in QtDesigner, and I have a QPushButton and a label. When I click the button, I want the text on the label to change. in C++ in QtDesigner, It's easy to connect the two. But I have to write it all in python.

我使用 pyuic5 将 .ui 文件转换为 .py 文件.在那里,在 Ui_MainWindow 类中,我可以看到 setupUi 方法,它初始化 self.button 如下

I convert .ui file with pyuic5 to .py file. There, in Ui_MainWindow class, I can see setupUi method, that initializes self.button as follows

self.testButton = QtWidgets.QPushButton(self.centralWidget)
self.testButton.setObjectName("newGame")

然后,在方法的最后,

QtCore.QMetaObject.connectSlotsByName(MainWindow)

被调用,但是,老实说,我无法弄清楚它做什么以及它连接到哪里.

is called, but, to be honest, I can't figure out what it does and what it connects to where.

在主类中,继承自QMainWindow,我编写了如下方法

in Main class, inheriting from QMainWindow, i write a following method

@pyqtSlot(name='change')
def change_text(self):
    self.ui.testLabel.setText("Button Clicked!")

而且我不知道如何将按钮信号连接到该插槽.在 pyqt4 中,我可以通过执行 button.clicked.connect(self.change_text) 手动设置它,但正如我发现的那样,PyQt5 已经过时并丢弃了这种简单的设置.

And i can't figure out how to connect the button signal to that slot. In pyqt4 I could set it up manually by doing button.clicked.connect(self.change_text), but as I've found out, PyQt5 obsoleted and discarded such simple set-up.

请问,有人能帮我解决这个问题吗?

Please, can anybody help me with that one?

推荐答案

我不知道你从哪里得到PyQt5 改变了整个‘连接按钮到插槽’的工作方式"的想法,但它是完全和彻底的错误的.从 PyQt 官方文档中可以很容易地看出,没有发生这样的变化:

I don't know where you got the idea that "PyQt5 changed how the whole 'connect button to a slot' works", but it is completely and utterly wrong. There have been no such changes, as can be readily seen from the official PyQt documentation:

但即使不阅读任何文档,自己进行测试也很容易.例如,在下面的脚本中,只需在前两行切换注释,它就会运行一样:

But even without reading any documentation, it's easy enough to test for yourself. For example, in the following script, just switch comments on the first two lines, and it will run just the same:

# from PyQt5.QtWidgets import (
from PyQt4.QtGui import (
    QApplication, QWidget, QVBoxLayout, QPushButton, QLabel,
    )

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.button = QPushButton('Test', self)
        self.label = QLabel(self)
        self.button.clicked.connect(self.handleButton)
        layout = QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(self.button)

    def handleButton(self):
        self.label.setText('Button Clicked!')

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

至于其他要点:在您目前的知识状态下,我认为您可以放心地忽略 connectSlotsByNamepyqtSlot.尽管它们有其用途(有关详细信息,请参阅上述文档),但很少有真正需要在 95% 的应用程序中使用它们.

As for the other points: at your current state of knowledge, I would say you can safely ignore connectSlotsByName and pyqtSlot. Although they have their uses (see the above docs for details), there's very rarely any real need to use them in 95% of applications.

对于您的具体情况,语法很简单:

For your specific case, the syntax is simply:

    self.testButton.clicked.connect(self.change_text)
    ...

def change_text(self):
    self.ui.testLabel.setText("Button Clicked!")

这篇关于PyQt5:如何将 QPushButton 连接到插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆