@[TOC]


QAbstractButton

#include<QAbstractButton>

Qmake: QT + = widgets

Inherits from: QWidget

Inherited from: QCheckBox, QPushButton, QRadioButton, QToolButton

QAbstractButton is an abstract base class for all buttons. QCheckBox, QPushButton, QRadioButton, and QToolButton all inherit from this class. These properties include icon, IconSize, TEXT, shortcut, Down, checkable, CHECKED, autoRepeat, autoExclusive, autoRepeatDelay, and autoRepeatInterval.

The corresponding QCheckBox, QPushButton, QRadioButton, QToolButton are all inherited from this class, Therefore, the properties mentioned above are available for QCheckBox, QPushButton, QRadioButton, QToolButton, and for icon, iconSize, and text, which are described in the following section (1.2QPushButton). Without further elaboration in this article, let’s look at the remaining eight attributes.

Note: In addition to the four buttons mentioned above, there are CommandLinkButton buttons (inherited from QPushButton), ButtonBox button boxes (wrapped by the QDialogButtonBox class), both of which will be covered in a later section. For now, All we need to know is the QAbstractButton class.


2. QAbstractButton properties

Now let’s take a look at these attributes, with appropriate examples, and try to get the reader to understand what they mean. We drag a PushButton button in the UI, a PlainTextEdit (to display text, when we press the button, the PushButton will send a signal, which will then show that the button was pressed!)Since the default button is not very effective when pressed, we add a style sheet to the button (this chapter will not cover style sheets, as each type of button will be illustrated later in this chapter, if you want to learn about the style sheet in advance, you can click here).

#pushButton
{
border:1px;
background-color: rgb(196.112.255);
}
#pushButton:hover
{
border:1px;
background-color: rgb(47.120.255);
}

#pushButton:pressed{
border:1px;
background-color: rgb(47.120.255);

}
#pushButton:checked{
border:1px;
background-color: rgb(47.120.255);

}
Copy the code

You can see that our text box output is garbled, how do we solve it? Simply add the following code to the code to display it.

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
Copy the code

The down properties

This property saves whether the button is pressed, and if this property is true, the button is pressed. If this property is set to true, the press and click will not be signaled. The default is false. The UI does not have this property. Code can access and set the Down property via isDown() and setDown().


Shortcut properties

This property holds the shortcut keys associated with the button. Unlike the & shortcut, which is next to the first letter of the button and needs to be triggered by the Alt + letter. With the shortcut property, you can set multiple letters such as DELETE, Enter, and do not need Alt to trigger. Of course, you can also set the parameter to Alt +XXXX.

Set in the button property bar on the UI.Code can access and set this property through shortcut() and setShortcut(QKeySequence).

The parameter is a custom key or a defined constant. For example, the constant QKeySequence::New is CTRL +N, a New shortcut key. Let’s take a look and add code to the button:

ui->pushButton->setShortcut(QKeySequence::New);
Copy the code

We use the shortcut key CTRL +N to press the button instead of the mouse.


Checkable property

This property determines whether the button can be selected. By default, the value is False, meaning the button cannot be selected. If it is selected, the PushButton will not automatically pop up after it is pressed, indicating the selected state. If it is clicked again, the PushButton will pop up, indicating the unselected state.Code can access and setChecked properties through isChecked() and setChecked(bool).

Notice that every time you press it, it gives out a signal.

The optional pushButton, toolButton, and commandLinkButton classes do not automatically pop up when the button is pressed. The button is selected. When the button is clicked again, the button will pop up and become unselected.

When the selectable Radio Button and checkBox are pressed, the Button switches between the selected and not selected states. When the Button is not selected, the Radio Button cannot operate. The checkBox Button needs to see if tristate is set to False. If set to False, the checkBox class button does not operate, otherwise it can toggle between unchecked and half-checked.

This property can be applied to button multiple selections.


Checked attribute

This property saves the status of whether the button is selected, true if it is selected, false if it is not selected. UI, which can be manually selectedCode can access and setChecked properties through isChecked() and setChecked(bool).


AutoRepeat properties

If autoRepeat is enabled, pressed (), Released (), and clicked () signals are periodically emitted when the button is pressed and the mouse is not released. By default, autoRepeat is disabled. When autoRepeat is in the allowed state, the delay time and repeat interval are defined by autoRepeatDelay and autoRepeatInterva in milliseconds.

Code can access and set the autoRepeat property with autoRepeat() and setAutoRepeat(bool).

Note: If a button is pressed with a shortcut key, the system will enable automatic repetition and timing, not the QAbstractButton class. In this case, the pressed (), released (), and clicked () signals are emitted as normal.


AutoExclusive properties

The autoExclusive property preserves whether to enable the autoExclusive feature of a button. If this is enabled, only one of the selected buttons belonging to the same parent can be selected at any time. Selecting another button automatically deselects the previously selected button, similar to the function of the exclusive button group.

This property has no effect on buttons that belong to a button group.

By default, with the exception of radio buttons, the autoExclusive property is disabled (False).

This property can be accessed and set using autoExclusive(), setAutoExclusive(bool).


AutoRepeatDelay properties

This property keeps the initial delay, in milliseconds, repeating automatically.

If autoRepeat is enabled, autoRepeatDelay defines the initial delay before automatic repeat begins. The delay before the first signal is sent, that is, the time interval between when the button is pressed and the first signal is sent.The autoRepeatDelay property can be accessed and set with autoRepeatDelay () and setAutoRepeatDelay (int).


AutoRepeatInterval properties

The autoRepeatInterval property holds the time interval, in milliseconds, between each of the Pressed (), Released (), and clicked () signals for automatically repeating the trigger button.

The autoRepeatDelay property can be accessed and set with autoRepeatInterval() and setAutoRepeatInterval (int).