Coverage Report

Created: 2026-04-01 07:24

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