|  | Home · Overviews · Examples |

# QAbstractButton Class Reference
[com.trolltech.qt.gui module]

The `QAbstractButton` class is the abstract base class of button widgets, providing functionality common to buttons. More...

Inherits `QWidget`.

Inherited by `QCheckBox`, `QPushButton`, `QRadioButton`, and `QToolButton`.

## Detailed Description

The `QAbstractButton` class is the abstract base class of button widgets, providing functionality common to buttons.

This class implements an abstract button. Subclasses of this class handle user actions, and specify how the button is drawn.

`QAbstractButton` provides support for both push buttons and checkable (toggle) buttons. Checkable buttons are implemented in the `QRadioButton` and `QCheckBox` classes. Push buttons are implemented in the `QPushButton` and `QToolButton` classes; these also provide toggle behavior if required.

Any button can display a label containing text and an icon. `setText` sets the text; `setIcon` sets the icon. If a button is disabled, its label is changed to give the button a "disabled" appearance.

If the button is a text button with a string containing an ampersand ('&'), `QAbstractButton` automatically creates a shortcut key. For example:

        QPushButton *button = new QPushButton(tr("Ro&ck && Roll"), this);

The Alt+C shortcut is assigned to the button, i.e., when the user presses Alt+C the button will call `animateClick`. See the QShortcut documentation for details (to display an actual ampersand, use '&&').

You can also set a custom shortcut key using the `setShortcut` function. This is useful mostly for buttons that do not have any text, because they have no automatic shortcut.

        button->setIcon(QIcon(":/images/print.png"));
        button->setShortcut(tr("Alt+F7"));

All of the buttons provided by Qt (`QPushButton`, `QToolButton`, `QCheckBox`, and `QRadioButton`) can display both text and icons.

A button can be made the default button in a dialog are provided by QPushButton::setDefault() and QPushButton::setAutoDefault().

`QAbstractButton` provides most of the states used for buttons:

- `isDown` indicates whether the button is pressed down.
- `isChecked` indicates whether the button is checked. Only checkable buttons can be checked and unchecked (see below).
- `isEnabled` indicates whether the button can be pressed by the user.
- `setAutoRepeat` sets whether the button will auto-repeat if the user holds it down. `autoRepeatDelay` and `autoRepeatInterval` define how auto-repetition is done.
- `setCheckable` sets whether the button is a toggle button or not.

The difference between `isDown` and `isChecked` is as follows. When the user clicks a toggle button to check it, the button is first pressed then released into the checked state. When the user clicks it again (to uncheck it), the button moves first to the pressed state, then to the unchecked state (`isChecked` and `isDown` are both false).

`QAbstractButton` provides four signals:

1. `pressed` is emitted when the left mouse button is pressed while the mouse cursor is inside the button.
2. `released` is emitted when the left mouse button is released.
3. `clicked` is emitted when the button is first pressed and then released, when the shortcut key is typed, or when `click` or `animateClick` is called.
4. `toggled` is emitted when the state of a toggle button changes.

To subclass `QAbstractButton`, you must reimplement at least `paintEvent` to draw the button's outline and its text or pixmap. It is generally advisable to reimplement `sizeHint` as well, and sometimes `hitButton` (to determine whether a button press is within the button). For buttons with more than two states (like tri-state buttons), you will also have to reimplement `checkStateSet` and `nextCheckState`.

See also `QButtonGroup`.