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
qcoreapplication.h
Go to the documentation of this file.
1// Copyright (C) 2021 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 QCOREAPPLICATION_H
6#define QCOREAPPLICATION_H
7
8#include <QtCore/qglobal.h>
9#include <QtCore/qstring.h>
10#ifndef QT_NO_QOBJECT
11#include <QtCore/qcoreevent.h>
12#include <QtCore/qdeadlinetimer.h>
13#include <QtCore/qeventloop.h>
14#include <QtCore/qobject.h>
15#else
16#include <QtCore/qscopedpointer.h>
17#endif
18#include <QtCore/qnativeinterface.h>
19
20#ifndef QT_NO_QOBJECT
21#if defined(Q_OS_WIN) && !defined(tagMSG)
22typedef struct tagMSG MSG;
23#endif
24#endif
25
27
28class QAbstractEventDispatcher;
29class QAbstractNativeEventFilter;
30class QDebug;
32class QPermission;
33#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
34class QPostEventList;
35#endif
36class QTranslator;
37
38#define qApp QCoreApplication::instance()
39
40class QCoreApplicationPrivate;
41class Q_CORE_EXPORT QCoreApplication
42#ifndef QT_NO_QOBJECT
43 : public QObject
44#endif
45{
46#ifndef QT_NO_QOBJECT
47 Q_OBJECT
48 Q_PROPERTY(QString applicationName READ applicationName WRITE setApplicationName
49 NOTIFY applicationNameChanged)
50 Q_PROPERTY(QString applicationVersion READ applicationVersion WRITE setApplicationVersion
51 NOTIFY applicationVersionChanged)
52 Q_PROPERTY(QString organizationName READ organizationName WRITE setOrganizationName
53 NOTIFY organizationNameChanged)
54 Q_PROPERTY(QString organizationDomain READ organizationDomain WRITE setOrganizationDomain
55 NOTIFY organizationDomainChanged)
56 Q_PROPERTY(bool quitLockEnabled READ isQuitLockEnabled WRITE setQuitLockEnabled)
57#endif
58
59 Q_DECLARE_PRIVATE(QCoreApplication)
60 friend class QEventLoopLocker;
61#if QT_CONFIG(permissions)
62 using RequestPermissionPrototype = void(*)(QPermission);
63#endif
64
65public:
66 enum { ApplicationFlags = QT_VERSION
67 };
68
69 QCoreApplication(int &argc, char **argv
70#ifndef Q_QDOC
71 , int = ApplicationFlags
72#endif
73 );
74
75 ~QCoreApplication();
76
77 static QStringList arguments();
78
79 static void setAttribute(Qt::ApplicationAttribute attribute, bool on = true);
80 static bool testAttribute(Qt::ApplicationAttribute attribute);
82 static void setOrganizationDomain(const QString &orgDomain);
83 static QString organizationDomain();
84 static void setOrganizationName(const QString &orgName);
85 static QString organizationName();
86 static void setApplicationName(const QString &application);
87 static QString applicationName();
88 static void setApplicationVersion(const QString &version);
89 static QString applicationVersion();
90
91 static void setSetuidAllowed(bool allow);
92 static bool isSetuidAllowed();
93
94#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
95 static QCoreApplication *instance() noexcept { return self.loadRelaxed(); }
96 static bool instanceExists() noexcept { return instance() != nullptr; }
97#else
98# ifdef __cpp_lib_atomic_ref
99 static QCoreApplication *instance() noexcept
100 { return std::atomic_ref(self).load(std::memory_order_relaxed); }
101# else
102 static QCoreApplication *instance() noexcept { return self; }
103# endif
104 static bool instanceExists() noexcept;
105#endif
106
107#ifndef QT_NO_QOBJECT
108 static int exec();
109 static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
110 static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime);
111 static void processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline);
112
113 static bool sendEvent(QObject *receiver, QEvent *event);
114 static void postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority);
115 static void sendPostedEvents(QObject *receiver = nullptr, int event_type = 0);
116 static void removePostedEvents(QObject *receiver, int eventType = 0);
117 static QAbstractEventDispatcher *eventDispatcher();
118 static void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);
119
120 virtual bool notify(QObject *, QEvent *);
121
122 static bool startingUp();
123 static bool closingDown();
124#endif
125
126 static QString applicationDirPath();
127 static QString applicationFilePath();
128 Q_DECL_CONST_FUNCTION static qint64 applicationPid() noexcept;
129
130#if QT_CONFIG(permissions) || defined(Q_QDOC)
131 Qt::PermissionStatus checkPermission(const QPermission &permission);
132
133# ifdef Q_QDOC
134 template <typename Functor>
135 void requestPermission(const QPermission &permission, const QObject *context, Functor functor);
136# else
137 // requestPermission with context or receiver object; need to require here that receiver is the
138 // right type to avoid ambiguity with the private implementation function.
139 template <typename Functor,
140 std::enable_if_t<
141 QtPrivate::AreFunctionsCompatible<RequestPermissionPrototype, Functor>::value,
142 bool> = true>
143 void requestPermission(const QPermission &permission,
144 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
145 Functor &&func)
146 {
147 requestPermissionImpl(permission,
148 QtPrivate::makeCallableObject<RequestPermissionPrototype>(std::forward<Functor>(func)),
149 receiver);
150 }
151# endif // Q_QDOC
152
153#ifndef QT_NO_CONTEXTLESS_CONNECT
154 #ifdef Q_QDOC
155 template <typename Functor>
156 #else
157 // requestPermission to a functor or function pointer (without context)
158 template <typename Functor,
159 std::enable_if_t<
160 QtPrivate::AreFunctionsCompatible<RequestPermissionPrototype, Functor>::value,
161 bool> = true>
162 #endif
163 void requestPermission(const QPermission &permission, Functor &&func)
164 {
165 requestPermission(permission, nullptr, std::forward<Functor>(func));
166 }
167#endif // QT_NO_CONTEXTLESS_CONNECT
168
169#if QT_CORE_REMOVED_SINCE(6, 10)
170private:
171 void requestPermission(const QPermission &permission,
172 QtPrivate::QSlotObjectBase *slotObj, const QObject *context);
173public:
174#endif
175
176#endif // QT_CONFIG(permission)
177
178#if QT_CONFIG(library)
179 static void setLibraryPaths(const QStringList &);
180 static QStringList libraryPaths();
181 static void addLibraryPath(const QString &);
182 static void removeLibraryPath(const QString &);
183#endif // QT_CONFIG(library)
184
185#ifndef QT_NO_TRANSLATION
186 static bool installTranslator(QTranslator * messageFile);
187 static bool removeTranslator(QTranslator * messageFile);
188#endif
189
190 static QString translate(const char * context,
191 const char * key,
192 const char * disambiguation = nullptr,
193 int n = -1);
194
195 QT_DECLARE_NATIVE_INTERFACE_ACCESSOR(QCoreApplication)
196
197#ifndef QT_NO_QOBJECT
198 void installNativeEventFilter(QAbstractNativeEventFilter *filterObj);
199 void removeNativeEventFilter(QAbstractNativeEventFilter *filterObj);
200
201 static bool isQuitLockEnabled();
202 static void setQuitLockEnabled(bool enabled);
203
204public Q_SLOTS:
205 static void quit();
206 static void exit(int retcode = 0);
208Q_SIGNALS:
209 void aboutToQuit(QPrivateSignal);
211 void organizationNameChanged();
212 void organizationDomainChanged();
213 void applicationNameChanged();
214 void applicationVersionChanged();
215
216protected:
217 bool event(QEvent *) override;
218
219# if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
220 QT_DEPRECATED_VERSION_X_6_10("This feature will be removed in Qt 7")
221 virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
222# endif
223#endif // QT_NO_QOBJECT
224
225protected:
226 QCoreApplication(QCoreApplicationPrivate &p);
227
228#ifdef QT_NO_QOBJECT
229 std::unique_ptr<QCoreApplicationPrivate> d_ptr;
230#endif
231
232private:
233#ifndef QT_NO_QOBJECT
234 static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
235 static bool notifyInternal2(QObject *receiver, QEvent *);
236 static bool forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent = nullptr);
237
238 void requestPermissionImpl(const QPermission &permission, QtPrivate::QSlotObjectBase *slotObj,
239 const QObject *context);
240#endif
241
242#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
243 static QBasicAtomicPointer<QCoreApplication> self;
244#else
245 static QCoreApplication *self;
246#endif
248 Q_DISABLE_COPY(QCoreApplication)
250 friend class QApplication;
251 friend class QApplicationPrivate;
252 friend class QGuiApplication;
253 friend class QGuiApplicationPrivate;
254 friend class QWidget;
255 friend class QWidgetWindow;
256 friend class QWidgetPrivate;
257 friend class QWindowPrivate;
258#ifndef QT_NO_QOBJECT
259 friend class QEventDispatcherUNIXPrivate;
260 friend class QCocoaEventDispatcherPrivate;
261 friend bool qt_sendSpontaneousEvent(QObject *, QEvent *);
262#endif
263 friend Q_CORE_EXPORT QString qAppName();
264 friend class QCommandLineParserPrivate;
265};
266
267#define Q_DECLARE_TR_FUNCTIONS(context) public
268 :
269 static inline QString tr(const char *sourceText, const char *disambiguation = nullptr, int n = -1)
270 { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } private
271 :
272
273typedef void (*QtStartUpFunction)();
274typedef void (*QtCleanUpFunction)();
275
276Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction);
279Q_CORE_EXPORT QString qAppName(); // get application name
280
281#define Q_COREAPP_STARTUP_FUNCTION(AFUNC)
282 static void AFUNC ## _ctor_function() {
283 qAddPreRoutine(AFUNC);
284 }
285 Q_CONSTRUCTOR_FUNCTION(AFUNC ## _ctor_function)
286
287#ifndef QT_NO_QOBJECT
288#if defined(Q_OS_WIN) && !defined(QT_NO_DEBUG_STREAM)
289#if QT_DEPRECATED_SINCE(6, 12)
290QT_DEPRECATED_VERSION_X_6_12("Use qt_decodeMSG() instead.")
291Q_CORE_EXPORT QString decodeMSG(const MSG &);
292#endif
293Q_CORE_EXPORT QString qt_decodeMSG(const MSG &);
294Q_CORE_EXPORT QDebug operator<<(QDebug, const MSG &);
295#endif
296#endif
297
298QT_END_NAMESPACE
299
300#include <QtCore/qcoreapplication_platform.h>
301
302#endif // QCOREAPPLICATION_H
#define ARCH_FULL
\inmodule QtCore
\inmodule QtCore
Definition qeventloop.h:60
Definition qfile.h:71
\inmodule QtCore
\inmodule QtCore \inheaderfile QPermissions
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:177
Combined button and popup list for selecting options.
Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction)
void(* QtCleanUpFunction)()
void(* QtStartUpFunction)()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
#define QFILE_MAYBE_EXPLICIT
Definition qfile.h:65
static QString fromRawStringView(QStringView string)
static bool pathIsRelative(const QString &path)
void qDumpCPUFeatures()
Definition qsimd.cpp:683
#define SHARED_STRING
#define COMPILER_STRING
#define DEBUG_STRING
static QString qtPrefix()
static QString appPrefix()
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
qsizetype indexOf(const QString &str, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QStringList filter(const QLatin1StringMatcher &matcher) const
void sort(Qt::CaseSensitivity cs=Qt::CaseSensitive)
Definition qstringlist.h:88
QString join(const QString &sep) const
qsizetype indexOf(QStringView needle, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QStringList & replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QString join(QStringView sep) const
Definition qstringlist.h:93
qsizetype lastIndexOf(QStringView str, qsizetype from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
QStringList filter(QLatin1StringView needle, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
QStringList filter(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
QStringList & replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QStringList & replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QStringList & replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
QStringList filter(const QStringMatcher &matcher) const
qsizetype lastIndexOf(const QString &str, qsizetype from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept