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