6.0.0-rc4Source

QButtonGroup Class

The QButtonGroup class organizes buttons that belong together, such as a choice of radio buttons. More…

Header#include <QButtonGroup>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQObject

List of all members, including inherited members

Public Functions

QButtonGroup(QObject *parent = nullptr)
~QButtonGroup()
voidsetExclusive(bool exclusive)
boolexclusive() const
voidaddButton(QAbstractButton *button, int id = -1)
voidremoveButton(QAbstractButton *button)
QList<QAbstractButton *>buttons() const
QAbstractButton *checkedButton() const
QAbstractButton *button(int id) const
voidsetId(QAbstractButton *button, int id)
intid(QAbstractButton *button) const
intcheckedId() const

Signals

voidbuttonClicked(QAbstractButton *button)
voidbuttonPressed(QAbstractButton *button)
voidbuttonReleased(QAbstractButton *button)
voidbuttonToggled(QAbstractButton *button, bool checked)
voididClicked(int id)
voididPressed(int id)
voididReleased(int id)
voididToggled(int id, bool checked)

Detailed Description

A button group is not a widget: the buttons stay where they are in their layouts. An exclusive group, the default, lets one checkable button be checked at a time, whatever the parents of the buttons are. The group gives each button an id and reports clicks, presses and toggles of any of its buttons:

auto *sizes = new QButtonGroup(window);
sizes->addButton(new QRadioButton("Small"), 1);
sizes->addButton(new QRadioButton("Large"), 2);
connect(sizes, &QButtonGroup::idClicked, [](int id) { qDebug() << "size" << id; });

A button belongs to one group at most. A button in a group is exclusive by the rules of the group, not by QAbstractButton::autoExclusive().

See also QAbstractButton, QRadioButton and QGroupBox.

Differences from Qt

None known.

Member Function Documentation

[explicit] QButtonGroup::QButtonGroup(QObject *parent = nullptr)

Constructs an exclusive button group with the given parent.

[override] QButtonGroup::~QButtonGroup()

Destroys the group. The buttons stay; they no longer belong to a group.

void QButtonGroup::addButton(QAbstractButton *button, int id = -1)

Adds button to the group with the given id. With an id of -1 the group gives the button a negative id of its own, starting at -2. A button that is in another group leaves it.

See also removeButton() and id().

QAbstractButton * QButtonGroup::button(int id) const

Returns the button with the given id, or nullptr if there is none.

See also id().

[signal] void QButtonGroup::buttonClicked(QAbstractButton *button)

This signal is emitted when button of the group is clicked, after its own clicked() signal.

See also idClicked().

[signal] void QButtonGroup::buttonPressed(QAbstractButton *button)

This signal is emitted when button of the group is pressed down.

See also idPressed().

[signal] void QButtonGroup::buttonReleased(QAbstractButton *button)

This signal is emitted when button of the group is released.

See also idReleased().

QList<QAbstractButton *> QButtonGroup::buttons() const

Returns the buttons of the group, in the order they were added.

[signal] void QButtonGroup::buttonToggled(QAbstractButton *button, bool checked)

This signal is emitted when button of the group is toggled; checked is its new state.

See also idToggled().

QAbstractButton * QButtonGroup::checkedButton() const

Returns the checked button of an exclusive group, or the first checked one of a group that is not exclusive, or nullptr if no button is checked.

See also checkedId().

int QButtonGroup::checkedId() const

Returns the id of the checked button, or -1 if no button is checked.

See also checkedButton().

bool QButtonGroup::exclusive() const

Returns whether at most one button of the group can be checked. The default is true.

See also setExclusive().

int QButtonGroup::id(QAbstractButton *button) const

Returns the id of button, or -1 if it is not in the group.

See also setId() and button().

[signal] void QButtonGroup::idClicked(int id)

This signal is emitted when the button with the given id is clicked, just before buttonClicked().

[signal] void QButtonGroup::idPressed(int id)

This signal is emitted when the button with the given id is pressed down.

[signal] void QButtonGroup::idReleased(int id)

This signal is emitted when the button with the given id is released.

[signal] void QButtonGroup::idToggled(int id, bool checked)

This signal is emitted when the button with the given id is toggled; checked is its new state.

void QButtonGroup::removeButton(QAbstractButton *button)

Removes button from the group.

See also addButton().

void QButtonGroup::setExclusive(bool exclusive)

Sets whether at most one button of the group can be checked to exclusive. In an exclusive group the checked button cannot be unchecked by clicking it; checking another one unchecks it.

See also exclusive().

void QButtonGroup::setId(QAbstractButton *button, int id)

Sets the id of button to id. An id of -1 is not taken, as in Qt.

See also id().