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
qaction.h
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#ifndef QACTION_H
6#define QACTION_H
7
8#include <QtGui/qtguiglobal.h>
9#if QT_CONFIG(shortcut)
10# include <QtGui/qkeysequence.h>
11#endif
12#include <QtGui/qicon.h>
13#include <QtCore/qstring.h>
14#include <QtCore/qvariant.h>
15#include <QtCore/qobject.h>
16
18
19QT_BEGIN_NAMESPACE
20
21class QActionEvent;
22class QActionGroup;
23class QActionPrivate;
24class QMenu;
25#if QT_DEPRECATED_SINCE(6,0)
26class QWidget;
27class QGraphicsWidget;
28#endif
29
30class Q_GUI_EXPORT QAction : public QObject
31{
32 Q_OBJECT
33 Q_DECLARE_PRIVATE(QAction)
34
35 Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY checkableChanged FINAL)
36 Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled)
37 Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged
38 RESET resetEnabled FINAL)
39 Q_PROPERTY(QIcon icon READ icon WRITE setIcon NOTIFY changed)
40 Q_PROPERTY(QString text READ text WRITE setText NOTIFY changed)
41 Q_PROPERTY(QString iconText READ iconText WRITE setIconText NOTIFY changed)
42 Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip NOTIFY changed)
43 Q_PROPERTY(QString statusTip READ statusTip WRITE setStatusTip NOTIFY changed)
44 Q_PROPERTY(QString whatsThis READ whatsThis WRITE setWhatsThis NOTIFY changed)
45 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY changed)
46#if QT_CONFIG(shortcut)
47 Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut NOTIFY changed)
48 Q_PROPERTY(Qt::ShortcutContext shortcutContext READ shortcutContext WRITE setShortcutContext
49 NOTIFY changed)
50 Q_PROPERTY(bool autoRepeat READ autoRepeat WRITE setAutoRepeat NOTIFY changed)
51#endif // QT_CONFIG(shortcut)
52 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged FINAL)
53 Q_PROPERTY(MenuRole menuRole READ menuRole WRITE setMenuRole NOTIFY changed)
54 Q_PROPERTY(bool iconVisibleInMenu READ isIconVisibleInMenu WRITE setIconVisibleInMenu
55 NOTIFY changed)
56 Q_PROPERTY(bool shortcutVisibleInContextMenu READ isShortcutVisibleInContextMenu
57 WRITE setShortcutVisibleInContextMenu NOTIFY changed)
58 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY changed)
60public:
61 // note this is copied into qplatformmenu.h, which must stay in sync
62 enum MenuRole { NoRole = 0, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole,
63 AboutRole, PreferencesRole, QuitRole };
64 Q_ENUM(MenuRole)
65 enum Priority { LowPriority = 0,
66 NormalPriority = 128,
67 HighPriority = 256};
68 Q_ENUM(Priority)
69 explicit QAction(QObject *parent = nullptr);
70 explicit QAction(const QString &text, QObject *parent = nullptr);
71 explicit QAction(const QIcon &icon, const QString &text, QObject *parent = nullptr);
72
73 ~QAction();
74
75 QList<QObject *> associatedObjects() const;
76
77#if QT_DEPRECATED_SINCE(6,0)
78#ifdef Q_QDOC
79 QWidget *parentWidget() const;
80 QList<QWidget*> associatedWidgets() const;
81 QList<QGraphicsWidget*> associatedGraphicsWidgets() const;
82#else
83 /*
84 These are templates so that instantiation happens only in calling code, when
85 QWidget, QMenu, and QGraphicsWidget can be expected to be fully defined.
86 */
87 template<typename T = QWidget*>
88 QT_DEPRECATED_VERSION_X_6_0("Use parent() with qobject_cast() instead")
89 T parentWidget() const
90 {
91 auto result = parent();
92 while (result && !qobject_cast<T>(result))
93 result = result->parent();
94 return static_cast<T>(result);
95 }
96
97 template<typename T = QWidget*>
98 QT_DEPRECATED_VERSION_X_6_0("Use associatedObjects() with qobject_cast() instead")
99 QList<T> associatedWidgets() const
100 {
101 QList<T> result;
102 for (auto object : associatedObjects())
103 if (auto widget = qobject_cast<T>(object))
104 result.append(widget);
105 return result;
106 }
107 template<typename T = QGraphicsWidget*>
108 QT_DEPRECATED_VERSION_X_6_0("Use associatedObjects() with qobject_cast() instead")
109 QList<T> associatedGraphicsWidgets() const
110 {
111 QList<T> result;
112 for (auto object : associatedObjects())
113 if (auto graphicsWidget = qobject_cast<T>(object))
114 result.append(graphicsWidget);
115 return result;
116 }
117#endif
118#endif
119
120 void setActionGroup(QActionGroup *group);
121 QActionGroup *actionGroup() const;
122 void setIcon(const QIcon &icon);
123 QIcon icon() const;
124
125 void setText(const QString &text);
126 QString text() const;
127
128 void setIconText(const QString &text);
129 QString iconText() const;
130
131 void setToolTip(const QString &tip);
132 QString toolTip() const;
133
134 void setStatusTip(const QString &statusTip);
135 QString statusTip() const;
136
137 void setWhatsThis(const QString &what);
138 QString whatsThis() const;
139
140 void setPriority(Priority priority);
141 Priority priority() const;
142
143 void setSeparator(bool b);
144 bool isSeparator() const;
145
146#if QT_CONFIG(shortcut)
147 void setShortcut(const QKeySequence &shortcut);
148 QKeySequence shortcut() const;
149
150 void setShortcuts(const QList<QKeySequence> &shortcuts);
151 void setShortcuts(QKeySequence::StandardKey);
152 QList<QKeySequence> shortcuts() const;
153
154 void setShortcutContext(Qt::ShortcutContext context);
155 Qt::ShortcutContext shortcutContext() const;
156
157 void setAutoRepeat(bool);
158 bool autoRepeat() const;
159#endif // QT_CONFIG(shortcut)
160
161 void setFont(const QFont &font);
162 QFont font() const;
163
164 void setCheckable(bool);
165 bool isCheckable() const;
166
167 QVariant data() const;
168 void setData(const QVariant &var);
169
170 bool isChecked() const;
171
172 bool isEnabled() const;
174 bool isVisible() const;
175
176 enum ActionEvent { Trigger, Hover };
177 void activate(ActionEvent event);
178
179 void setMenuRole(MenuRole menuRole);
180 MenuRole menuRole() const;
181
182#ifdef Q_QDOC
183 QMenu *menu() const;
184 void setMenu(QMenu *menu);
185#else
186 template<typename T = QMenu*>
187 T menu() const
188 {
189 return qobject_cast<T>(menuObject());
190 }
191 template<typename T = QMenu*>
192 void setMenu(T m)
193 {
194 setMenuObject(m);
195 }
196#endif
197
198 void setIconVisibleInMenu(bool visible);
199 bool isIconVisibleInMenu() const;
200
201 void setShortcutVisibleInContextMenu(bool show);
202 bool isShortcutVisibleInContextMenu() const;
203
204 bool showStatusText(QObject *object = nullptr);
205
206protected:
207 bool event(QEvent *) override;
208 QAction(QActionPrivate &dd, QObject *parent);
210public Q_SLOTS:
211 void trigger() { activate(Trigger); }
212 void hover() { activate(Hover); }
213 void setChecked(bool);
214 void toggle();
215 void setEnabled(bool);
216 void resetEnabled();
217 inline void setDisabled(bool b) { setEnabled(!b); }
218 void setVisible(bool);
220Q_SIGNALS:
221 void changed();
222 void enabledChanged(bool enabled);
223 void checkableChanged(bool checkable);
224 void visibleChanged();
225 void triggered(bool checked = false);
226 void hovered();
227 void toggled(bool);
229private:
230 Q_DISABLE_COPY(QAction)
231 friend class QActionGroup;
232 friend class QWidget;
233 friend class QMenu;
234 friend class QMenuPrivate;
235 friend class QToolButton;
236 friend class QGraphicsWidget;
237
238 QObject *menuObject() const;
239 void setMenuObject(QObject *object);
240};
241
242#ifndef QT_NO_DEBUG_STREAM
243Q_GUI_EXPORT QDebug operator<<(QDebug, const QAction *);
244#endif
245
246QT_END_NAMESPACE
247
248#endif // QACTION_H
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:31
QT_REQUIRE_CONFIG(action)
Combined button and popup list for selecting options.
#define QAPP_CHECK(functionName)
Definition qaction.cpp:19
static QString qt_strippedText(QString s)
Definition qaction.cpp:32
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582