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
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
4#ifndef QACTION_H
5#define QACTION_H
6
7#include <QtGui/qtguiglobal.h>
8#if QT_CONFIG(shortcut)
9# include <QtGui/qkeysequence.h>
10#endif
11#include <QtGui/qicon.h>
12#include <QtCore/qstring.h>
13#include <QtCore/qvariant.h>
14#include <QtCore/qobject.h>
15
17
19
20class QActionEvent;
21class QActionGroup;
22class QActionPrivate;
23class QMenu;
24#if QT_DEPRECATED_SINCE(6,0)
25class QWidget;
26class QGraphicsWidget;
27#endif
28
29class Q_GUI_EXPORT QAction : public QObject
30{
32 Q_DECLARE_PRIVATE(QAction)
33
34 Q_PROPERTY(bool checkable READ isCheckable WRITE setCheckable NOTIFY checkableChanged FINAL)
35 Q_PROPERTY(bool checked READ isChecked WRITE setChecked NOTIFY toggled)
36 Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged
37 RESET resetEnabled FINAL)
38 Q_PROPERTY(QIcon icon READ icon WRITE setIcon NOTIFY changed)
39 Q_PROPERTY(QString text READ text WRITE setText NOTIFY changed)
40 Q_PROPERTY(QString iconText READ iconText WRITE setIconText NOTIFY changed)
41 Q_PROPERTY(QString toolTip READ toolTip WRITE setToolTip NOTIFY changed)
42 Q_PROPERTY(QString statusTip READ statusTip WRITE setStatusTip NOTIFY changed)
43 Q_PROPERTY(QString whatsThis READ whatsThis WRITE setWhatsThis NOTIFY changed)
44 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY changed)
45#if QT_CONFIG(shortcut)
46 Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut NOTIFY changed)
47 Q_PROPERTY(Qt::ShortcutContext shortcutContext READ shortcutContext WRITE setShortcutContext
48 NOTIFY changed)
49 Q_PROPERTY(bool autoRepeat READ autoRepeat WRITE setAutoRepeat NOTIFY changed)
50#endif // QT_CONFIG(shortcut)
51 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged FINAL)
52 Q_PROPERTY(MenuRole menuRole READ menuRole WRITE setMenuRole NOTIFY changed)
53 Q_PROPERTY(bool iconVisibleInMenu READ isIconVisibleInMenu WRITE setIconVisibleInMenu
54 NOTIFY changed)
55 Q_PROPERTY(bool shortcutVisibleInContextMenu READ isShortcutVisibleInContextMenu
56 WRITE setShortcutVisibleInContextMenu NOTIFY changed)
57 Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY changed)
58
59public:
60 // note this is copied into qplatformmenu.h, which must stay in sync
61 enum MenuRole { NoRole = 0, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole,
62 AboutRole, PreferencesRole, QuitRole };
63 Q_ENUM(MenuRole)
64 enum Priority { LowPriority = 0,
65 NormalPriority = 128,
66 HighPriority = 256};
67 Q_ENUM(Priority)
68 explicit QAction(QObject *parent = nullptr);
69 explicit QAction(const QString &text, QObject *parent = nullptr);
70 explicit QAction(const QIcon &icon, const QString &text, QObject *parent = nullptr);
71
72 ~QAction();
73
74 QList<QObject *> associatedObjects() const;
75
76#if QT_DEPRECATED_SINCE(6,0)
77#ifdef Q_QDOC
78 QWidget *parentWidget() const;
79 QList<QWidget*> associatedWidgets() const;
80 QList<QGraphicsWidget*> associatedGraphicsWidgets() const;
81#else
82 /*
83 These are templates so that instantiation happens only in calling code, when
84 QWidget, QMenu, and QGraphicsWidget can be expected to be fully defined.
85 */
86 template<typename T = QWidget*>
87 QT_DEPRECATED_VERSION_X_6_0("Use parent() with qobject_cast() instead")
88 T parentWidget() const
89 {
90 auto result = parent();
91 while (result && !qobject_cast<T>(result))
92 result = result->parent();
93 return static_cast<T>(result);
94 }
95
96 template<typename T = QWidget*>
97 QT_DEPRECATED_VERSION_X_6_0("Use associatedObjects() with qobject_cast() instead")
98 QList<T> associatedWidgets() const
99 {
100 QList<T> result;
101 for (auto object : associatedObjects())
102 if (auto widget = qobject_cast<T>(object))
104 return result;
105 }
106 template<typename T = QGraphicsWidget*>
107 QT_DEPRECATED_VERSION_X_6_0("Use associatedObjects() with qobject_cast() instead")
108 QList<T> associatedGraphicsWidgets() const
109 {
110 QList<T> result;
111 for (auto object : associatedObjects())
112 if (auto graphicsWidget = qobject_cast<T>(object))
113 result.append(graphicsWidget);
114 return result;
115 }
116#endif
117#endif
118
119 void setActionGroup(QActionGroup *group);
120 QActionGroup *actionGroup() const;
121 void setIcon(const QIcon &icon);
122 QIcon icon() const;
123
124 void setText(const QString &text);
125 QString text() const;
126
127 void setIconText(const QString &text);
128 QString iconText() const;
129
130 void setToolTip(const QString &tip);
131 QString toolTip() const;
132
133 void setStatusTip(const QString &statusTip);
134 QString statusTip() const;
135
136 void setWhatsThis(const QString &what);
137 QString whatsThis() const;
138
139 void setPriority(Priority priority);
140 Priority priority() const;
141
142 void setSeparator(bool b);
143 bool isSeparator() const;
144
145#if QT_CONFIG(shortcut)
146 void setShortcut(const QKeySequence &shortcut);
147 QKeySequence shortcut() const;
148
149 void setShortcuts(const QList<QKeySequence> &shortcuts);
150 void setShortcuts(QKeySequence::StandardKey);
151 QList<QKeySequence> shortcuts() const;
152
153 void setShortcutContext(Qt::ShortcutContext context);
154 Qt::ShortcutContext shortcutContext() const;
155
156 void setAutoRepeat(bool);
157 bool autoRepeat() const;
158#endif // QT_CONFIG(shortcut)
159
160 void setFont(const QFont &font);
161 QFont font() const;
162
163 void setCheckable(bool);
164 bool isCheckable() const;
165
166 QVariant data() const;
167 void setData(const QVariant &var);
168
169 bool isChecked() const;
170
171 bool isEnabled() const;
172
173 bool isVisible() const;
174
175 enum ActionEvent { Trigger, Hover };
176 void activate(ActionEvent event);
177
178 void setMenuRole(MenuRole menuRole);
179 MenuRole menuRole() const;
180
181#ifdef Q_QDOC
182 QMenu *menu() const;
183 void setMenu(QMenu *menu);
184#else
185 template<typename T = QMenu*>
186 T menu() const
187 {
188 return qobject_cast<T>(menuObject());
189 }
190 template<typename T = QMenu*>
191 void setMenu(T m)
192 {
193 setMenuObject(m);
194 }
195#endif
196
197 void setIconVisibleInMenu(bool visible);
198 bool isIconVisibleInMenu() const;
199
200 void setShortcutVisibleInContextMenu(bool show);
201 bool isShortcutVisibleInContextMenu() const;
202
203 bool showStatusText(QObject *object = nullptr);
204
205protected:
206 bool event(QEvent *) override;
207 QAction(QActionPrivate &dd, QObject *parent);
208
209public Q_SLOTS:
210 void trigger() { activate(Trigger); }
211 void hover() { activate(Hover); }
212 void setChecked(bool);
213 void toggle();
214 void setEnabled(bool);
215 void resetEnabled();
216 inline void setDisabled(bool b) { setEnabled(!b); }
217 void setVisible(bool);
218
220 void changed();
222 void checkableChanged(bool checkable);
224 void triggered(bool checked = false);
225 void hovered();
226 void toggled(bool);
227
228private:
229 Q_DISABLE_COPY(QAction)
230 friend class QActionGroup;
231 friend class QWidget;
232 friend class QMenu;
233 friend class QMenuPrivate;
234 friend class QToolButton;
235 friend class QGraphicsWidget;
236
237 QObject *menuObject() const;
238 void setMenuObject(QObject *object);
239};
240
241#ifndef QT_NO_DEBUG_STREAM
242Q_GUI_EXPORT QDebug operator<<(QDebug, const QAction *);
243#endif
244
246
247#endif // QACTION_H
The QActionEvent class provides an event that is generated when a QAction is added,...
The QActionGroup class groups actions together.
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition qaction.h:30
void hover()
This is a convenience slot that calls activate(Hover).
Definition qaction.h:211
void checkableChanged(bool checkable)
ActionEvent
This enum type is used when calling QAction::activate()
Definition qaction.h:175
T menu() const
Returns the menu contained by this action.
Definition qaction.h:186
void toggled(bool)
This signal is emitted whenever a checkable action changes its isChecked() status.
void visibleChanged()
void hovered()
This signal is emitted when an action is highlighted by the user; for example, when the user pauses w...
MenuRole
This enum describes how an action should be moved into the application menu on \macos.
Definition qaction.h:61
@ AboutQtRole
Definition qaction.h:61
@ AboutRole
Definition qaction.h:62
void enabledChanged(bool enabled)
void setMenu(T m)
Sets the menu contained by this action to the specified menu.
Definition qaction.h:191
Priority
This enum defines priorities for actions in user interface.
Definition qaction.h:64
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.
void trigger()
This is a convenience slot that calls activate(Trigger).
Definition qaction.h:210
void setDisabled(bool b)
This is a convenience function for the \l enabled property, that is useful for signals–slots connecti...
Definition qaction.h:216
\inmodule QtCore
\inmodule QtCore
Definition qcoreevent.h:45
\reentrant
Definition qfont.h:22
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
The QKeySequence class encapsulates a key sequence as used by shortcuts.
Definition qlist.h:75
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
\inmodule QtCore
Definition qobject.h:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QToolButton class provides a quick-access button to commands or options, usually used inside a QT...
Definition qtoolbutton.h:20
\inmodule QtCore
Definition qvariant.h:65
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QOpenGLWidget * widget
[1]
QString text
myinstance setPriority(MyClass::VeryHigh)
list append(new Employee("Blackpool", "Stephen"))
Combined button and popup list for selecting options.
bool isEnabled()
ShortcutContext
static void * context
static bool isSeparator(char c)
Definition qhsts.cpp:280
T qobject_cast(QObject *object)
\variable QObject::staticMetaObject
Definition qobject.h:419
GLboolean GLboolean GLboolean b
const GLfloat * m
GLuint object
[3]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLboolean GLuint group
struct _cl_event * event
GLuint64EXT * result
[6]
decorationRoleName toolTipRoleName setWhatsThis
decorationRoleName setToolTip
#define QT_REQUIRE_CONFIG(feature)
#define QT_DEPRECATED_VERSION_X_6_0(text)
#define Q_ENUM(x)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SLOTS
#define Q_SIGNALS
view show()
[18] //! [19]
if(qFloatDistance(a, b)<(1<< 7))
[0]
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
mimeData setData("text/csv", csvData)
edit isVisible()
myAction setIconVisibleInMenu(true)
myAction setIcon(SomeIcon)
insertRed setText("insert red text")
button setShortcut(tr("Alt+F7"))
radio1 setChecked(true)
QMenu menu
[5]