Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
4
5#include "private/qbuttongroup_p.h"
6
7#include "private/qabstractbutton_p.h"
8
9QT_BEGIN_NAMESPACE
10
11// detect a checked button other than the current one
12void QButtonGroupPrivate::detectCheckedButton()
13{
14 QAbstractButton *previous = checkedButton;
15 checkedButton = nullptr;
16 if (exclusive)
17 return;
18 for (int i = 0; i < buttonList.size(); i++) {
19 if (buttonList.at(i) != previous && buttonList.at(i)->isChecked()) {
20 checkedButton = buttonList.at(i);
21 return;
22 }
23 }
24}
25
26/*!
27 \class QButtonGroup
28 \brief The QButtonGroup class provides a container to organize groups of
29 button widgets.
30
31 \ingroup organizers
32 \ingroup geomanagement
33 \inmodule QtWidgets
34
35 QButtonGroup provides an abstract container into which button widgets can
36 be placed. It does not provide a visual representation of this container
37 (see QGroupBox for a container widget), but instead manages the states of
38 each of the buttons in the group.
39
40 An \l {QButtonGroup::exclusive} {exclusive} button group switches
41 off all checkable (toggle) buttons except the one that has been
42 clicked. By default, a button group is exclusive. The buttons in a
43 button group are usually checkable \l{QPushButton}s, \l{QCheckBox}es
44 (normally for non-exclusive button groups), or \l{QRadioButton}s.
45 If you create an exclusive button group, you should ensure that
46 one of the buttons in the group is initially checked; otherwise,
47 the group will initially be in a state where no buttons are
48 checked.
49
50 A button can be added to the group with addButton() and removed
51 with removeButton(). If the group is exclusive, the
52 currently checked button is available with checkedButton(). If a
53 button is clicked, the buttonClicked() signal is emitted; for a
54 checkable button in an exclusive group this means that the button
55 has been checked. The list of buttons in the group is returned by
56 buttons().
57
58 In addition, QButtonGroup can map between integers and buttons.
59 You can assign an integer id to a button with setId(), and
60 retrieve it with id(). The id of the currently checked button is
61 available with checkedId(), and there is a signal
62 idClicked() that emits the id of the button. The id \c {-1}
63 is reserved by QButtonGroup to mean "no such button". The purpose
64 of the mapping mechanism is to simplify the representation of enum
65 values in a user interface.
66
67 \sa QGroupBox, QPushButton, QCheckBox, QRadioButton
68*/
69
70/*!
71 Constructs a new, empty button group with the given \a parent.
72
73 \sa addButton(), setExclusive()
74*/
75QButtonGroup::QButtonGroup(QObject *parent)
76 : QObject(*new QButtonGroupPrivate, parent)
77{
78}
79
80/*!
81 Destroys the button group.
82*/
83QButtonGroup::~QButtonGroup()
84{
85 Q_D(QButtonGroup);
86 for (int i = 0; i < d->buttonList.size(); ++i)
87 d->buttonList.at(i)->d_func()->group = nullptr;
88}
89
90/*!
91 \property QButtonGroup::exclusive
92 \brief whether the button group is exclusive
93
94 If this property is \c true, then only one button in the group can be checked
95 at any given time. The user can click on any button to check it, and that
96 button will replace the existing one as the checked button in the group.
97
98 In an exclusive group, the user cannot uncheck the currently checked button
99 by clicking on it; instead, another button in the group must be clicked
100 to set the new checked button for that group.
101
102 By default, this property is \c true.
103*/
104bool QButtonGroup::exclusive() const
105{
106 Q_D(const QButtonGroup);
107 return d->exclusive;
108}
109
110void QButtonGroup::setExclusive(bool exclusive)
111{
112 Q_D(QButtonGroup);
113 d->exclusive = exclusive;
114}
115
116
117
118/*!
119 \fn void QButtonGroup::buttonClicked(QAbstractButton *button)
120
121 This signal is emitted when the given \a button is clicked. A
122 button is clicked when it is first pressed and then released, when
123 its shortcut key is typed, or when QAbstractButton::click()
124 or QAbstractButton::animateClick() is programmatically called.
125
126
127 \sa checkedButton(), QAbstractButton::clicked()
128*/
129
130/*!
131 \fn void QButtonGroup::idClicked(int id)
132 \since 5.15
133
134 This signal is emitted when a button with the given \a id is
135 clicked.
136
137 \sa checkedButton(), QAbstractButton::clicked()
138*/
139
140/*!
141 \fn void QButtonGroup::buttonPressed(QAbstractButton *button)
142 \since 4.2
143
144 This signal is emitted when the given \a button is pressed down.
145
146 \sa QAbstractButton::pressed()
147*/
148
149/*!
150 \fn void QButtonGroup::idPressed(int id)
151 \since 5.15
152
153 This signal is emitted when a button with the given \a id is
154 pressed down.
155
156 \sa QAbstractButton::pressed()
157*/
158
159/*!
160 \fn void QButtonGroup::buttonReleased(QAbstractButton *button)
161 \since 4.2
162
163 This signal is emitted when the given \a button is released.
164
165 \sa QAbstractButton::released()
166*/
167
168/*!
169 \fn void QButtonGroup::idReleased(int id)
170 \since 5.15
171
172 This signal is emitted when a button with the given \a id is
173 released.
174
175 \sa QAbstractButton::released()
176*/
177
178/*!
179 \fn void QButtonGroup::buttonToggled(QAbstractButton *button, bool checked)
180 \since 5.2
181
182 This signal is emitted when the given \a button is toggled.
183 \a checked is true if the button is checked, or false if the button is unchecked.
184
185 \sa QAbstractButton::toggled()
186*/
187
188/*!
189 \fn void QButtonGroup::idToggled(int id, bool checked)
190 \since 5.15
191
192 This signal is emitted when a button with the given \a id is toggled.
193 \a checked is true if the button is checked, or false if the button is unchecked.
194
195 \sa QAbstractButton::toggled()
196*/
197
198/*!
199 Adds the given \a button to the button group. If \a id is -1,
200 an id will be assigned to the button.
201 Automatically assigned ids are guaranteed to be negative,
202 starting with -2. If you are assigning your own ids, use
203 positive values to avoid conflicts.
204
205 \sa removeButton(), buttons()
206*/
207void QButtonGroup::addButton(QAbstractButton *button, int id)
208{
209 Q_D(QButtonGroup);
210 if (QButtonGroup *previous = button->d_func()->group)
211 previous->removeButton(button);
212 button->d_func()->group = this;
213 d->buttonList.append(button);
214 if (id == -1) {
215 const auto it = 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 >= 0) ? -2 : (*it - 1);
220 } else {
221 d->mapping[button] = id;
222 }
223
224 if (d->exclusive && button->isChecked())
225 button->d_func()->notifyChecked();
226}
227
228/*!
229 Removes the given \a button from the button group.
230
231 \sa addButton(), buttons()
232*/
233void QButtonGroup::removeButton(QAbstractButton *button)
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
246/*!
247 Returns the button group's list of buttons. This may be empty.
248
249 \sa addButton(), removeButton()
250*/
251QList<QAbstractButton*> QButtonGroup::buttons() const
252{
253 Q_D(const QButtonGroup);
254 return d->buttonList;
255}
256
257/*!
258 Returns the button group's checked button, or \nullptr if no
259 buttons are checked.
260
261 \sa buttonClicked()
262*/
263QAbstractButton *QButtonGroup::checkedButton() const
264{
265 Q_D(const QButtonGroup);
266 return d->checkedButton;
267}
268
269/*!
270 \since 4.1
271
272 Returns the button with the specified \a id, or \nullptr if no
273 such button exists.
274*/
275QAbstractButton *QButtonGroup::button(int id) const
276{
277 Q_D(const QButtonGroup);
278 return d->mapping.key(id);
279}
280
281/*!
282 \since 4.1
283
284 Sets the \a id for the specified \a button. Note that \a id cannot
285 be -1.
286
287 \sa id()
288*/
289void QButtonGroup::setId(QAbstractButton *button, int id)
290{
291 Q_D(QButtonGroup);
292 if (button && id != -1)
293 d->mapping[button] = id;
294}
295
296/*!
297 \since 4.1
298
299 Returns the id for the specified \a button, or -1 if no such button
300 exists.
301
302
303 \sa setId()
304*/
305int QButtonGroup::id(QAbstractButton *button) const
306{
307 Q_D(const QButtonGroup);
308 return d->mapping.value(button, -1);
309}
310
311/*!
312 \since 4.1
313
314 Returns the id of the checkedButton(), or -1 if no button is checked.
315
316 \sa setId()
317*/
318int QButtonGroup::checkedId() const
319{
320 Q_D(const QButtonGroup);
321 return d->mapping.value(d->checkedButton, -1);
322}
323
324QT_END_NAMESPACE
325
326#include "moc_qbuttongroup.cpp"