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
qactiongroup.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 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 "qactiongroup.h"
5
6#include "qaction.h"
7#include "qaction_p.h"
8#include "qactiongroup_p.h"
9#include "qevent.h"
10#include "qlist.h"
11
13
18
20
21void QActionGroup::_q_actionChanged()
22{
23 Q_D(QActionGroup);
24 auto action = qobject_cast<QAction*>(sender());
25 Q_ASSERT_X(action != nullptr, "QActionGroup::_q_actionChanged", "internal error");
26 if (d->exclusionPolicy != QActionGroup::ExclusionPolicy::None) {
27 if (action->isChecked()) {
28 if (action != d->current) {
29 if (!d->current.isNull())
30 d->current->setChecked(false);
31 d->current = action;
32 }
33 } else if (action == d->current) {
34 d->current = nullptr;
35 }
36 }
37}
38
39void QActionGroup::_q_actionTriggered()
40{
41 auto action = qobject_cast<QAction*>(sender());
42 Q_ASSERT_X(action != nullptr, "QActionGroup::_q_actionTriggered", "internal error");
43 emit triggered(action);
44}
45
46void QActionGroup::_q_actionHovered()
47{
48 auto action = qobject_cast<QAction*>(sender());
49 Q_ASSERT_X(action != nullptr, "QActionGroup::_q_actionHovered", "internal error");
50 emit hovered(action);
51}
52
105
107 QObject(dd, parent)
108{
109}
110
115
127{
128 Q_D(QActionGroup);
129 if (!d->actions.contains(a)) {
130 d->actions.append(a);
131 QObject::connect(a, &QAction::triggered, this, &QActionGroup::_q_actionTriggered);
132 QObject::connect(a, &QAction::changed, this, &QActionGroup::_q_actionChanged);
133 QObject::connect(a, &QAction::hovered, this, &QActionGroup::_q_actionHovered);
134 }
135 a->d_func()->setEnabled(d->enabled, true);
136 if (!a->d_func()->forceInvisible)
137 a->d_func()->setVisible(d->visible);
138 if (a->isChecked())
139 d->current = a;
140 QActionGroup *oldGroup = a->d_func()->group;
141 if (oldGroup != this) {
142 if (oldGroup)
143 oldGroup->removeAction(a);
144 a->d_func()->group = this;
145 a->d_func()->sendDataChanged();
146 }
147 return a;
148}
149
160{
161 return new QAction(text, this);
162}
163
174{
175 return new QAction(icon, text, this);
176}
177
185{
186 Q_D(QActionGroup);
187 if (d->actions.removeAll(action)) {
188 if (action == d->current)
189 d->current = nullptr;
190 QObject::disconnect(action, &QAction::triggered, this, &QActionGroup::_q_actionTriggered);
191 QObject::disconnect(action, &QAction::changed, this, &QActionGroup::_q_actionChanged);
192 QObject::disconnect(action, &QAction::hovered, this, &QActionGroup::_q_actionHovered);
193 action->d_func()->group = nullptr;
194 }
195}
196
200QList<QAction*> QActionGroup::actions() const
201{
202 Q_D(const QActionGroup);
203 return d->actions;
204}
205
220
232
252
254{
255 Q_D(const QActionGroup);
256 return d->exclusionPolicy;
257}
258
277{
278 Q_D(QActionGroup);
279 d->enabled = b;
280 for (auto action : std::as_const(d->actions)) {
281 action->d_func()->setEnabled(b, true);
282 }
283}
284
286{
287 Q_D(const QActionGroup);
288 return d->enabled;
289}
290
296{
297 Q_D(const QActionGroup);
298 return d->current.data();
299}
300
311{
312 Q_D(QActionGroup);
313 d->visible = b;
314 for (auto action : std::as_const(d->actions)) {
315 if (!action->d_func()->forceInvisible)
316 action->d_func()->setVisible(b);
317 }
318}
319
321{
322 Q_D(const QActionGroup);
323 return d->visible;
324}
325
327
328#include "moc_qactiongroup.cpp"
The QActionGroup class groups actions together.
void setEnabled(bool)
QActionGroup(QObject *parent)
Constructs an action group for the parent object.
void hovered(QAction *)
ExclusionPolicy
This enum specifies the different policies that can be used to control how the group performs exclusi...
bool isEnabled() const
QAction * checkedAction() const
Returns the currently checked action in the group, or \nullptr if none are checked.
void removeAction(QAction *a)
Removes the action from this group.
QActionGroup::ExclusionPolicy exclusionPolicy
This property holds the group exclusive checking policy.
bool isExclusive() const
Returns true if the group is exclusive.
QList< QAction * > actions() const
Returns the list of this groups's actions.
bool isVisible() const
~QActionGroup()
Destroys the action group.
void setExclusionPolicy(ExclusionPolicy policy)
void setExclusive(bool)
Enable or disable the group exclusion checking.
void triggered(QAction *)
QAction * addAction(QAction *a)
Adds the action to this group, and returns it.
void setVisible(bool)
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void hovered()
This signal is emitted when an action is highlighted by the user; for example, when the user pauses w...
void triggered(bool checked=false)
This signal is emitted when an action is activated by the user; for example, when the user clicks a m...
void changed()
This signal is emitted when an action has changed.
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
\threadsafe
Definition qobject.cpp:3236
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString text
Combined button and popup list for selecting options.
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLenum GLsizei const GLuint GLboolean enabled
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
#define emit
QSizePolicy policy