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