Additional Inherited Members

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:

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.

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:

pressed() is emitted when the left mouse button is pressed while the mouse cursor is inside the button.

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().

Property Documentation

autoExclusive : bool

This property holds whether auto-exclusivity is enabled.

If auto-exclusivity is enabled, checkable buttons that belong to the same parent widget behave as if they were part of the same exclusive button group. In an exclusive button group, only one button can be checked at any time; checking another button automatically unchecks the previously checked one.

autoRepeat : bool

This property holds whether autoRepeat is enabled.

If autoRepeat is enabled, then the pressed(), released(), and clicked() signals are emitted at regular intervals when the button is down. autoRepeat is off by default. The initial delay and the repetition interval are defined in milliseconds by autoRepeatDelay and autoRepeatInterval.

Note: If a button is pressed down by a shortcut key, then auto-repeat is enabled and timed by the system and not by this class. The pressed(), released(), and clicked() signals will be emitted like in the normal case.

Access functions:

bool

autoRepeat () const

void

setAutoRepeat ( bool )

autoRepeatDelay : int

This property holds the initial delay of auto-repetition.

If autoRepeat is enabled, then autoRepeatDelay defines the initial delay in milliseconds before auto-repetition kicks in.

If the button has no text, the text() function will return a an empty string.

If the text contains an ampersand character ('&'), a shortcut is automatically created for it. The character that follows the '&' will be used as the shortcut key. Any previous shortcut will be overwritten, or cleared if no shortcut is defined by the text. See the QShortcut documentation for details (to display an actual ampersand, use '&&').

void QAbstractButton::clicked ( boolchecked = false ) [signal]

This signal is emitted when the button is activated (i.e. pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click() or animateClick() is called. Notably, this signal is not emitted if you call setDown(), setChecked() or toggle().

If the button is checkable, checked is true if the button is checked, or false if the button is unchecked.

void QAbstractButton::nextCheckState () [virtual protected]

This virtual handler is called when a button is clicked. The default implementation calls setChecked(!isChecked()) if the button isCheckable(). It allows subclasses to implement intermediate button states.

void QAbstractButton::toggle () [slot]

void QAbstractButton::toggled ( boolchecked ) [signal]

This signal is emitted whenever a checkable button changes its state. checked is true if the button is checked, or false if the button is unchecked.

This may be the result of a user action, click() slot activation, or because setChecked() was called.

The states of buttons in exclusive button groups are updated before this signal is emitted. This means that slots can act on either the "off" signal or the "on" signal emitted by the buttons in the group whose states have changed.

For example, a slot that reacts to signals emitted by newly checked buttons but which ignores signals from buttons that have been unchecked can be implemented using the following pattern:

Copyright 2013 Digia Plc and/or it's subsidiaries. Documentation contributions included herein are the copyrights of their respective owners.
Information about Qt licenses are available in the Qt Licensing page.