In the last article, we worked on the backbone of GUIS, the signal and slot mechanism. In this article, we move ahead to the GUI part of Qt.
The basic block of any GUI application is the widget, which provides the functionality of GUI components like buttons, lists, etc. In Qt, Qwidget is the base class of all other GUI classes. It provides the main functionality, like keyboard and mouse events.
One of the simplest, yet most important widgets is the button. For example, in Figure 1, when I tried to close Firefox with multiple tabs opened, I was prompted by a dialogue box with two push-buttons, Cancel and Close Tabs. Without buttons, a GUI would be useless. All button widgets are derived from the Qabstractbutton class, which provides four signals— clicked(), pressed(), released() and toggled(), and provides five states— ischecked(), isenabled(), isdown(), setcheckable() and setautorepeat(). We will cover these in the sample code. Some of the classes derived from Qabstractbutton are: Qpushbutton, Qradiobutton and Qcheckbox.
Let’s start with Qpushbutton. This widget is used to let the user initiate some action, like OK, Cancel, Apply, Save, etc. The Qpushbutton widget emits clicked(), pressed() and released() signals. We will use the signals and slot mechanism covered in the last article, to demonstrate the use of buttons.
Create a GUI application and drag-drop a Pushbutton on the dialogue window. You can change the name from the default pushbutton by right-clicking the widget, then select Change objectname, and give it a new name. Similarly, you can rename the dialogue window and other classes. private slots: void button_clicked(); void button_pressed(); void button_released();
};