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#ifndef QSHORTCUT_H
5#define QSHORTCUT_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtGui/qkeysequence.h>
9#include <QtCore/qobject.h>
10
11QT_REQUIRE_CONFIG(shortcut);
12
13QT_BEGIN_NAMESPACE
14
15class QShortcutPrivate;
16class QWindow;
17
18class Q_GUI_EXPORT QShortcut : public QObject
19{
20 Q_OBJECT
21 Q_DECLARE_PRIVATE(QShortcut)
22 Q_PROPERTY(QKeySequence key READ key WRITE setKey)
23 Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled)
24 Q_PROPERTY(bool autoRepeat READ autoRepeat WRITE setAutoRepeat)
25 Q_PROPERTY(Qt::ShortcutContext context READ context WRITE setContext)
26public:
27 explicit QShortcut(QObject *parent);
28 explicit QShortcut(const QKeySequence& key, QObject *parent,
29 const char *member = nullptr, const char *ambiguousMember = nullptr,
30 Qt::ShortcutContext context = Qt::WindowShortcut);
31 explicit QShortcut(QKeySequence::StandardKey key, QObject *parent,
32 const char *member = nullptr, const char *ambiguousMember = nullptr,
33 Qt::ShortcutContext context = Qt::WindowShortcut);
34
35#ifdef Q_QDOC
36 template<typename Functor>
37 QShortcut(const QKeySequence &key, QObject *parent,
38 Functor functor,
39 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
40 template<typename Functor>
41 QShortcut(const QKeySequence &key, QObject *parent,
42 const QObject *context, Functor functor,
43 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
44 template<typename Functor, typename FunctorAmbiguous>
45 QShortcut(const QKeySequence &key, QObject *parent,
46 const QObject *context1, Functor functor,
47 FunctorAmbiguous functorAmbiguous,
48 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
49 template<typename Functor, typename FunctorAmbiguous>
50 QShortcut(const QKeySequence &key, QObject *parent,
51 const QObject *context1, Functor functor,
52 const QObject *context2, FunctorAmbiguous functorAmbiguous,
53 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
54
55 template<typename Functor>
56 QShortcut(QKeySequence::StandardKey key, QObject *parent,
57 Functor functor,
58 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
59 template<typename Functor>
60 QShortcut(QKeySequence::StandardKey key, QObject *parent,
61 const QObject *context, Functor functor,
62 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
63 template<typename Functor, typename FunctorAmbiguous>
64 QShortcut(QKeySequence::StandardKey key, QObject *parent,
65 const QObject *context1, Functor functor,
66 FunctorAmbiguous functorAmbiguous,
67 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
68 template<typename Functor, typename FunctorAmbiguous>
69 QShortcut(QKeySequence::StandardKey key, QObject *parent,
70 const QObject *context1, Functor functor,
71 const QObject *context2, FunctorAmbiguous functorAmbiguous,
72 Qt::ShortcutContext shortcutContext = Qt::WindowShortcut);
73#else
74 template<typename Func1>
75 QShortcut(const QKeySequence &key, QObject *parent,
76 Func1 slot1,
77 Qt::ShortcutContext context = Qt::WindowShortcut)
78 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
79 {
80 connect(this, &QShortcut::activated, std::move(slot1));
81 }
82 template<class Obj1, typename Func1>
83 QShortcut(const QKeySequence &key, QObject *parent,
84 const Obj1 *object1, Func1 slot1,
85 Qt::ShortcutContext context = Qt::WindowShortcut,
86 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0)
87 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
88 {
89 connect(this, &QShortcut::activated, object1, std::move(slot1));
90 }
91 template<class Obj1, typename Func1, typename Func2>
92 QShortcut(const QKeySequence &key, QObject *parent,
93 const Obj1 *object1, Func1 slot1, Func2 slot2,
94 Qt::ShortcutContext context = Qt::WindowShortcut,
95 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0)
96 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
97 {
98 connect(this, &QShortcut::activated, object1, std::move(slot1));
99 connect(this, &QShortcut::activatedAmbiguously, object1, std::move(slot2));
100 }
101 template<class Obj1, typename Func1, class Obj2, typename Func2>
102 QShortcut(const QKeySequence &key, QObject *parent,
103 const Obj1 *object1, Func1 slot1,
104 const Obj2 *object2, Func2 slot2,
105 Qt::ShortcutContext context = Qt::WindowShortcut,
106 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0,
107 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj2*>::Value>::type* = 0)
108 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
109 {
110 connect(this, &QShortcut::activated, object1, std::move(slot1));
111 connect(this, &QShortcut::activatedAmbiguously, object2, std::move(slot2));
112 }
113
114 template<typename Func1>
115 QShortcut(QKeySequence::StandardKey key, QObject *parent,
116 Func1 slot1,
117 Qt::ShortcutContext context = Qt::WindowShortcut)
118 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
119 {
120 connect(this, &QShortcut::activated, std::move(slot1));
121 }
122 template<class Obj1, typename Func1>
123 QShortcut(QKeySequence::StandardKey key, QObject *parent,
124 const Obj1 *object1, Func1 slot1,
125 Qt::ShortcutContext context = Qt::WindowShortcut,
126 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0)
127 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
128 {
129 connect(this, &QShortcut::activated, object1, std::move(slot1));
130 }
131 template<class Obj1, typename Func1, typename Func2>
132 QShortcut(QKeySequence::StandardKey key, QObject *parent,
133 const Obj1 *object1, Func1 slot1, Func2 slot2,
134 Qt::ShortcutContext context = Qt::WindowShortcut,
135 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0)
136 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
137 {
138 connect(this, &QShortcut::activated, object1, std::move(slot1));
139 connect(this, &QShortcut::activatedAmbiguously, object1, std::move(slot2));
140 }
141 template<class Obj1, typename Func1, class Obj2, typename Func2>
142 QShortcut(QKeySequence::StandardKey key, QObject *parent,
143 const Obj1 *object1, Func1 slot1,
144 const Obj2 *object2, Func2 slot2,
145 Qt::ShortcutContext context = Qt::WindowShortcut,
146 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj1*>::Value>::type* = 0,
147 typename std::enable_if<QtPrivate::IsPointerToTypeDerivedFromQObject<Obj2*>::Value>::type* = 0)
148 : QShortcut(key, parent, static_cast<const char*>(nullptr), static_cast<const char*>(nullptr), context)
149 {
150 connect(this, &QShortcut::activated, object1, std::move(slot1));
151 connect(this, &QShortcut::activatedAmbiguously, object2, std::move(slot2));
152 }
153#endif
154
155 ~QShortcut();
156
157 void setKey(const QKeySequence& key);
158 QKeySequence key() const;
159 void setKeys(QKeySequence::StandardKey key);
160 void setKeys(const QList<QKeySequence> &keys);
161 QList<QKeySequence> keys() const;
162
163 void setEnabled(bool enable);
164 bool isEnabled() const;
165
166 void setContext(Qt::ShortcutContext context);
167 Qt::ShortcutContext context() const;
168
169 void setAutoRepeat(bool on);
170 bool autoRepeat() const;
171
172#if QT_DEPRECATED_SINCE(6,0)
173 QT_DEPRECATED_VERSION_6_0 int id() const;
174#endif
175
176 void setWhatsThis(const QString &text);
177 QString whatsThis() const;
178
179#if QT_DEPRECATED_SINCE(6,0)
180#ifdef Q_QDOC
181 QWidget *parentWidget() const;
182#else
183 template<typename T = QWidget*>
184 QT_DEPRECATED_VERSION_X_6_0("Use parent() and qobject_cast instead")
185 inline T parentWidget() const
186 { return static_cast<T>(QObject::parent()); }
187#endif
188#endif
189
190Q_SIGNALS:
191 void activated();
192 void activatedAmbiguously();
193
194protected:
195 bool event(QEvent *e) override;
196};
197
198QT_END_NAMESPACE
199
200#endif // QSHORTCUT_H
201

source code of qtbase/src/gui/kernel/qshortcut.h