QButtonGroup Class
The QButtonGroup class organizes buttons that belong together, such as a choice of radio buttons. More…
| Header | #include <QButtonGroup> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QButtonGroup(QObject *parent = nullptr) | |
| ~QButtonGroup() | |
| void | setExclusive(bool exclusive) |
| bool | exclusive() const |
| void | addButton(QAbstractButton *button, int id = -1) |
| void | removeButton(QAbstractButton *button) |
| QList<QAbstractButton *> | buttons() const |
| QAbstractButton * | checkedButton() const |
| QAbstractButton * | button(int id) const |
| void | setId(QAbstractButton *button, int id) |
| int | id(QAbstractButton *button) const |
| int | checkedId() const |
Signals
| void | buttonClicked(QAbstractButton *button) |
| void | buttonPressed(QAbstractButton *button) |
| void | buttonReleased(QAbstractButton *button) |
| void | buttonToggled(QAbstractButton *button, bool checked) |
| void | idClicked(int id) |
| void | idPressed(int id) |
| void | idReleased(int id) |
| void | idToggled(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
QButtonGroup::QButtonGroup(QObject *parent = nullptr)
Constructs an exclusive button group with the given parent.
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().
void QButtonGroup::buttonClicked(QAbstractButton *button)
This signal is emitted when button of the group is clicked, after its own clicked() signal.
See also idClicked().
void QButtonGroup::buttonPressed(QAbstractButton *button)
This signal is emitted when button of the group is pressed down.
See also idPressed().
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.
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().
void QButtonGroup::idClicked(int id)
This signal is emitted when the button with the given id is clicked, just before buttonClicked().
void QButtonGroup::idPressed(int id)
This signal is emitted when the button with the given id is pressed down.
void QButtonGroup::idReleased(int id)
This signal is emitted when the button with the given id is released.
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().