PyQt5: How can I connect a QPushButton to a slot?

◇◆丶佛笑我妖孽 提交于 2019-12-13 11:33:05

问题


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.

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.

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")

then, at the end of the method,

QtCore.QMetaObject.connectSlotsByName(MainWindow)

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

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

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

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?


回答1:


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:

  • PyQt4 Signals & Slots documentation
  • PyQt5 Signals & Slots 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_())

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!")


来源:https://stackoverflow.com/questions/19966056/pyqt5-how-can-i-connect-a-qpushbutton-to-a-slot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!