The QAbstractButton class is the abstract base class of button widgets, providing functionality common to buttons.
More...
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
Inherits:
If the button is a text button with a string containing an ampersand ('&'), QAbstractButton automatically creates a shortcut key. For example:
-
pressed
() is emitted when the left mouse button is pressed while the mouse cursor is inside the button.
-
released
() is emitted when the left mouse button is released.
-
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.
-
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
.
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.
The property has no effect on buttons that belong to a button group.
autoExclusive is off by default, except for radio buttons.
Access functions:
bool
autoExclusive
() const
void
setAutoExclusive
(bool)
See also
QRadioButton
.
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.
Access functions:
int
autoRepeatDelay
() const
void
setAutoRepeatDelay
(int)
See also
autoRepeat
and
autoRepeatInterval
.
autoRepeatInterval
:
int
This property holds the interval of auto-repetition
If
autoRepeat
is enabled, then autoRepeatInterval defines the length of the auto-repetition interval in millisecons.
Access functions:
int
autoRepeatInterval
() const
void
setAutoRepeatInterval
(int)
See also
autoRepeat
and
autoRepeatDelay
.
checkable
:
bool
This property holds whether the button is checkable
By default, the button is not checkable.
Access functions:
bool
isCheckable
() const
void
setCheckable
(bool)
See also
checked
.
checked
:
bool
This property holds whether the button is checked
Only checkable buttons can be checked. By default, the button is unchecked.
Access functions:
bool
isChecked
() const
void
setChecked
(bool)
Notifier signal:
void
toggled
(bool
checked
)
See also
checkable
.
down
:
bool
This property holds whether the button is pressed down
If this property is
true
, the button is pressed down. The signals
pressed
() and
clicked
() are not emitted if you set this property to true. The default is false.
Access functions:
bool
isDown
() const
void
setDown
(bool)
This property holds the icon shown on the button
The icon's default size is defined by the GUI style, but can be adjusted by setting the
iconSize
property.
Access functions:
QIcon
icon
() const
void
setIcon
(const QIcon &
icon
)
iconSize
:
QSize
This property holds the icon size used for this button.
The default size is defined by the GUI style. This is a maximum size for the icons. Smaller icons will not be scaled up.
Access functions:
QSize
iconSize
() const
void
setIconSize
(const QSize &
size
)
This property holds the mnemonic associated with the button
Access functions:
QKeySequence
shortcut
() const
void
setShortcut
(const QKeySequence &
key
)
This property holds the text shown on the button
If the button has no text, the text() function will return 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 '&&'.
There is no default text.
Access functions:
QString
text
() const
void
setText
(const QString &
text
)
Member Function Documentation
Constructs an abstract button with a
parent
.
Destroys the button.
Performs an animated click: the button is pressed immediately, and released 100ms later.
Calling this function again before the button is released resets the release timer.
All signals associated with a click are emitted as appropriate.
This function does nothing if the button is
disabled.
See also
click
().
void
QAbstractButton::
changeEvent
(
QEvent
*
e
)
Reimplements:
QWidget::changeEvent
(QEvent *event).
void
QAbstractButton::
checkStateSet
()
This virtual handler is called when
setChecked
() is used, unless it is called from within
nextCheckState
(). It allows subclasses to reset their intermediate button states.
See also
nextCheckState
().
void
QAbstractButton::
click
()
Performs a click.
All the usual signals associated with a click are emitted as appropriate. If the button is checkable, the state of the button is toggled.
This function does nothing if the button is
disabled.
See also
animateClick
().
void
QAbstractButton::
clicked
(
bool
checked
= false)
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.
See also
pressed
(),
released
(), and
toggled
().
bool
QAbstractButton::
event
(
QEvent
*
e
)
Reimplements:
QWidget::event
(QEvent *event).
void
QAbstractButton::
focusInEvent
(
QFocusEvent
*
e
)
Reimplements:
QWidget::focusInEvent
(QFocusEvent *event).
void
QAbstractButton::
focusOutEvent
(
QFocusEvent
*
e
)
Reimplements:
QWidget::focusOutEvent
(QFocusEvent *event).
QButtonGroup
*QAbstractButton::
group
() const
Returns the group that this button belongs to.
If the button is not a member of any
QButtonGroup
, this function returns
nullptr
.
See also
QButtonGroup
.
Returns
true
if
pos
is inside the clickable button rectangle; otherwise returns
false
.
By default, the clickable area is the entire widget. Subclasses may reimplement this function to provide support for clickable areas of different shapes and sizes.
Reimplements:
QWidget::keyPressEvent
(QKeyEvent *event).
void
QAbstractButton::
keyReleaseEvent
(
QKeyEvent
*
e
)
Reimplements:
QWidget::keyReleaseEvent
(QKeyEvent *event).
void
QAbstractButton::
mouseMoveEvent
(
QMouseEvent
*
e
)
Reimplements:
QWidget::mouseMoveEvent
(QMouseEvent *event).
void
QAbstractButton::
mousePressEvent
(
QMouseEvent
*
e
)
Reimplements:
QWidget::mousePressEvent
(QMouseEvent *event).
void
QAbstractButton::
mouseReleaseEvent
(
QMouseEvent
*
e
)
Reimplements:
QWidget::mouseReleaseEvent
(QMouseEvent *event).
void
QAbstractButton::
nextCheckState
()
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.
See also
checkStateSet
().
void
QAbstractButton::
paintEvent
(
QPaintEvent
*
e
)
Reimplements:
QWidget::paintEvent
(QPaintEvent *event).
void
QAbstractButton::
pressed
()
This signal is emitted when the button is pressed down.
See also
released
() and
clicked
().
void
QAbstractButton::
released
()
This signal is emitted when the button is released.
See also
pressed
(),
clicked
(), and
toggled
().
void
QAbstractButton::
timerEvent
(
QTimerEvent
*
e
)
Reimplements:
QObject::timerEvent
(QTimerEvent *event).
void
QAbstractButton::
toggle
()
Toggles the state of a checkable button.
See also
checked
.
void
QAbstractButton::
toggled
(
bool
checked
)
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
() is 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:
void MyWidget::reactToToggle(bool checked)
if (checked) {
...
Button groups can be created using the QButtonGroup class, and updates to the button states monitored with the QButtonGroup::buttonClicked() signal.
Note: Notifier signal for property checked.
See also checked and clicked().
© 2024 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries
worldwide. All other trademarks are property of their respective owners.