QAbstractButton

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

Inheritance diagram of PySide2.QtWidgets.QAbstractButton

Inherited by: QCheckBox, QCommandLinkButton, QPushButton, QRadioButton, QToolButton

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

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:

button = QPushButton(QObject.tr("Ro&ck && Roll"), self)

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, and therefore can’t have any automatic shortcut.

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

All 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 by means of setDefault() and 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.

    Note

    As opposed to other widgets, buttons derived from QAbstractButton accept mouse and context menu events when disabled.

  • 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

class QAbstractButton([parent=None])
param parent

QWidget

Constructs an abstract button with a parent .

PySide2.QtWidgets.QAbstractButton.animateClick([msec=100])
Parameters

msecint

Performs an animated click: the button is pressed immediately, and released msec milliseconds later (the default is 100 ms).

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 PySide2.QtWidgets.QWidget.setEnabled()

See also

click()

PySide2.QtWidgets.QAbstractButton.autoExclusive()
Return type

bool

PySide2.QtWidgets.QAbstractButton.autoRepeat()
Return type

bool

See also

setAutoRepeat()

PySide2.QtWidgets.QAbstractButton.autoRepeatDelay()
Return type

int

PySide2.QtWidgets.QAbstractButton.autoRepeatInterval()
Return type

int

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

PySide2.QtWidgets.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 PySide2.QtWidgets.QWidget.setEnabled()

See also

animateClick()

PySide2.QtWidgets.QAbstractButton.clicked([checked=false])
Parameters

checkedbool

PySide2.QtWidgets.QAbstractButton.group()
Return type

QButtonGroup

Returns the group that this button belongs to.

If the button is not a member of any QButtonGroup , this function returns None .

See also

QButtonGroup

PySide2.QtWidgets.QAbstractButton.hitButton(pos)
Parameters

posQPoint

Return type

bool

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.

PySide2.QtWidgets.QAbstractButton.icon()
Return type

QIcon

See also

setIcon()

PySide2.QtWidgets.QAbstractButton.iconSize()
Return type

QSize

See also

setIconSize()

PySide2.QtWidgets.QAbstractButton.isCheckable()
Return type

bool

PySide2.QtWidgets.QAbstractButton.isChecked()
Return type

bool

PySide2.QtWidgets.QAbstractButton.isDown()
Return type

bool

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

PySide2.QtWidgets.QAbstractButton.pressed()
PySide2.QtWidgets.QAbstractButton.released()
PySide2.QtWidgets.QAbstractButton.setAutoExclusive(arg__1)
Parameters

arg__1bool

See also

autoExclusive()

PySide2.QtWidgets.QAbstractButton.setAutoRepeat(arg__1)
Parameters

arg__1bool

See also

autoRepeat()

PySide2.QtWidgets.QAbstractButton.setAutoRepeatDelay(arg__1)
Parameters

arg__1int

PySide2.QtWidgets.QAbstractButton.setAutoRepeatInterval(arg__1)
Parameters

arg__1int

PySide2.QtWidgets.QAbstractButton.setCheckable(arg__1)
Parameters

arg__1bool

See also

isCheckable()

PySide2.QtWidgets.QAbstractButton.setChecked(arg__1)
Parameters

arg__1bool

See also

isChecked()

PySide2.QtWidgets.QAbstractButton.setDown(arg__1)
Parameters

arg__1bool

See also

isDown()

PySide2.QtWidgets.QAbstractButton.setIcon(icon)
Parameters

iconQIcon

See also

icon()

PySide2.QtWidgets.QAbstractButton.setIconSize(size)
Parameters

sizeQSize

See also

iconSize()

PySide2.QtWidgets.QAbstractButton.setShortcut(key)
Parameters

keyQKeySequence

See also

shortcut()

PySide2.QtWidgets.QAbstractButton.setText(text)
Parameters

text – unicode

See also

text()

PySide2.QtWidgets.QAbstractButton.shortcut()
Return type

QKeySequence

See also

setShortcut()

PySide2.QtWidgets.QAbstractButton.text()
Return type

unicode

See also

setText()

PySide2.QtWidgets.QAbstractButton.toggle()

Toggles the state of a checkable button.

See also

checked

PySide2.QtWidgets.QAbstractButton.toggled(checked)
Parameters

checkedbool