Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbuttongroup.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "private/qbuttongroup_p.h"
5
6#include "private/qabstractbutton_p.h"
7
9
10// detect a checked button other than the current one
12{
14 checkedButton = nullptr;
15 if (exclusive)
16 return;
17 for (int i = 0; i < buttonList.size(); i++) {
18 if (buttonList.at(i) != previous && buttonList.at(i)->isChecked()) {
20 return;
21 }
22 }
23}
24
78
83{
84 Q_D(QButtonGroup);
85 for (int i = 0; i < d->buttonList.size(); ++i)
86 d->buttonList.at(i)->d_func()->group = nullptr;
87}
88
104{
105 Q_D(const QButtonGroup);
106 return d->exclusive;
107}
108
109void QButtonGroup::setExclusive(bool exclusive)
110{
111 Q_D(QButtonGroup);
112 d->exclusive = exclusive;
113}
114
115
116
207{
208 Q_D(QButtonGroup);
209 if (QButtonGroup *previous = button->d_func()->group)
210 previous->removeButton(button);
211 button->d_func()->group = this;
212 d->buttonList.append(button);
213 if (id == -1) {
215 = std::min_element(d->mapping.cbegin(), d->mapping.cend());
216 if (it == d->mapping.cend())
217 d->mapping[button] = -2;
218 else
219 d->mapping[button] = *it - 1;
220 } else {
221 d->mapping[button] = id;
222 }
223
224 if (d->exclusive && button->isChecked())
225 button->d_func()->notifyChecked();
226}
227
234{
235 Q_D(QButtonGroup);
236 if (d->checkedButton == button) {
237 d->detectCheckedButton();
238 }
239 if (button->d_func()->group == this) {
240 button->d_func()->group = nullptr;
241 d->buttonList.removeAll(button);
242 d->mapping.remove(button);
243 }
244}
245
251QList<QAbstractButton*> QButtonGroup::buttons() const
252{
253 Q_D(const QButtonGroup);
254 return d->buttonList;
255}
256
264{
265 Q_D(const QButtonGroup);
266 return d->checkedButton;
267}
268
276{
277 Q_D(const QButtonGroup);
278 return d->mapping.key(id);
279}
280
290{
291 Q_D(QButtonGroup);
292 if (button && id != -1)
293 d->mapping[button] = id;
294}
295
306{
307 Q_D(const QButtonGroup);
308 return d->mapping.value(button, -1);
309}
310
319{
320 Q_D(const QButtonGroup);
321 return d->mapping.value(d->checkedButton, -1);
322}
323
325
326#include "moc_qbuttongroup.cpp"
The QAbstractButton class is the abstract base class of button widgets, providing functionality commo...
bool isChecked() const
QPointer< QAbstractButton > checkedButton
QList< QAbstractButton * > buttonList
The QButtonGroup class provides a container to organize groups of button widgets.
QButtonGroup(QObject *parent=nullptr)
Constructs a new, empty button group with the given parent.
~QButtonGroup()
Destroys the button group.
void setExclusive(bool)
void removeButton(QAbstractButton *)
Removes the given button from the button group.
void setId(QAbstractButton *button, int id)
QAbstractButton * checkedButton() const
Returns the button group's checked button, or \nullptr if no buttons are checked.
int checkedId() const
void addButton(QAbstractButton *, int id=-1)
Adds the given button to the button group.
bool exclusive
whether the button group is exclusive
QAbstractButton * button(int id) const
QList< QAbstractButton * > buttons() const
Returns the button group's list of buttons.
int id(QAbstractButton *button) const
\inmodule QtCore
Definition qhash.h:1145
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
\inmodule QtCore
Definition qobject.h:103
const_iterator cend() const noexcept
Definition qset.h:142
QPushButton * button
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
GLenum GLuint id
[7]