HomePython Page 2 - PyQT: Handling Windows and Buttons
Slots - Python
In this article, you will continue to learn about the process of building a GUI in PyQT. Specifically, you will learn about QDialog and QPushButton, which handle windows and buttons, the building blocks of most GUIs.
The show() and exec_loop() methods determine whether the dialogs are modal or modeless. However the main purpose of dialogs is to provide information feedback to the user, as well as to gather information from the user using buttons such as OK and Cancel. For these purposes, the accept() and reject() slots have been provided by the QDialog class.
Both of these are protected Slots. They are available only to the classes inherited from QDialog.
The accept() slot closes the dialog and sets the return value to ACCEPTED. This value is equivalent to the Boolean value of 1. The best scenario to visualize the usage of accept() is a button representing OK. If the clicked signal of this button is connected with the accept() slot, then upon the button being clicked, the dialog box would be closed and the return value would become ACCEPTED. In code this would be:
The above code is a snippet from an about dialog. When the OK button is clicked, the signal connects to the accept() dialog.
The reject() slot provides functionalities that are the opposite of accept(). It comes into picture when the dialog is canceled. The return code is set to Rejected. This slot can be connected to the signal corresponding to the dialog box or any button's clicked() signal. If the button's clicked() signal is connected, then the code would be:
QDialog (as of version 3.x) doesn't emit any signal of its own. That's about it for QDialog and its main slots and methods. Next is the most common widget of all: the command button, or in the terminology of PyQT, QPushButton.