2. Signals and Slots

Signals are notifications emitted by widgets when something happens.
Slots is the name Qt uses for the receivers of signals.

QPushButton Signals

Here we create a simple custom slot named the_button_was_clicked which accepts the clicked signal from the QPushButton.

Receiving data

The .clicked signal is no exception, also providing a checked (or toggled) state for the button. For normal buttons this is always False, so our first slot ignored this data.

# Listing 7: signals_and_slots_1b.py
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")

        button = QPushButton("Press Me!")
        button.setCheckable(True)
        button.clicked.connect(self.the_button_was_clicked)
        button.clicked.connect(self.the_button_was_toggled)

        self.setCentralWidget(button)

    def the_button_was_clicked(self):
        print("Clicked!")

    def the_button_was_toggled(self, clicked):
        print("Clicked?", clicked)

app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()

Storing data

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容