This widget provides the checkbox with text. It can have three states – checked, unchecked and no change of state. In essence it is an option button that can be turned on or off. In an application checkbox it can come handy when a set of options have to be presented to the user and user has the choice of selecting one or more options.
Typically checkboxes are used when their state doesn’t effect the states of other widgets. However there are exceptions to this rule as we will see in the example. The main aspect of QCheckBox is that just like QButton, it also has been derived from QAbstractButton and doesn’t have any slots. However, the methods provide all the required services. Secondly, a group of logically similar checkboxes can be grouped visually into a category using the QButtonGroup. The main signal of QCheckbox is stateChange(), and its main methods are checkStateSet() and setText(). The second method is inherited.
The stateChange() signal is used whenever the user checks or unchecks the widget. In other words, when the state of the widget changes, the widget emits a stateChange signal. The argument contains the check box’s new Toggle state. The Toggle state is an integer that contains current state.
The checkStateSet() method takes Qt.CheckState as argument. The valid values are Qt.Unchecked, Qt.PartiallyChecked and Qt.Checked. The second value comes into the picture when items in hierarchical models may be partially checked if some, but not all, of their children are checked.
The setText() method can be used to set the text of a checkbox. The parameter passed is QString, representing the text to be set. However, it doesn’t cause any signals to be emitted. Using the setIcon method, an icon can be placed beside the checkbox.
To create a checkbox with the text "secure," the code would be:
That brings us to the end of this section. In the next section I would be designing a form that would provide minimal connection details to connect to a server.