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 static QCoreApplication *instance() noexcept { return self; }
99 static bool instanceExists() noexcept;
100#endif
101
102#ifndef QT_NO_QOBJECT
103 static int exec();
104 static void processEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents);
105 static void processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime);
106 static void processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline);
107
108 static bool sendEvent(QObject *receiver, QEvent *event);
109 static void postEvent(QObject *receiver, QEvent *event, int priority = Qt::NormalEventPriority);
110 static void sendPostedEvents(QObject *receiver = nullptr, int event_type = 0);
111 static void removePostedEvents(QObject *receiver, int eventType = 0);
112 static QAbstractEventDispatcher *eventDispatcher();
113 static void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher);
114
115 virtual bool notify(QObject *, QEvent *);
116
117 static bool startingUp();
118 static bool closingDown();
119#endif
120
121 static QString applicationDirPath();
122 static QString applicationFilePath();
123 Q_DECL_CONST_FUNCTION static qint64 applicationPid() noexcept;
124
125#if QT_CONFIG(permissions) || defined(Q_QDOC)
126 Qt::PermissionStatus checkPermission(const QPermission &permission);
127
128# ifdef Q_QDOC
129 template <typename Functor>
130 void requestPermission(const QPermission &permission, const QObject *context, Functor functor);
131# else
132 // requestPermission with context or receiver object; need to require here that receiver is the
133 // right type to avoid ambiguity with the private implementation function.
134 template <typename Functor,
135 std::enable_if_t<
136 QtPrivate::AreFunctionsCompatible<RequestPermissionPrototype, Functor>::value,
137 bool> = true>
138 void requestPermission(const QPermission &permission,
139 const typename QtPrivate::ContextTypeForFunctor<Functor>::ContextType *receiver,
140 Functor &&func)
141 {
142 requestPermissionImpl(permission,
143 QtPrivate::makeCallableObject<RequestPermissionPrototype>(std::forward<Functor>(func)),
144 receiver);
145 }
146# endif // Q_QDOC
147
148#ifndef QT_NO_CONTEXTLESS_CONNECT
149 #ifdef Q_QDOC
150 template <typename Functor>
151 #else
152 // requestPermission to a functor or function pointer (without context)
153 template <typename Functor,
154 std::enable_if_t<
155 QtPrivate::AreFunctionsCompatible<RequestPermissionPrototype, Functor>::value,
156 bool> = true>
157 #endif
158 void requestPermission(const QPermission &permission, Functor &&func)
159 {
160 requestPermission(permission, nullptr, std::forward<Functor>(func));
161 }
162#endif // QT_NO_CONTEXTLESS_CONNECT
163
164#if QT_CORE_REMOVED_SINCE(6, 10)
165private:
166 void requestPermission(const QPermission &permission,
167 QtPrivate::QSlotObjectBase *slotObj, const QObject *context);
168public:
169#endif
170
171#endif // QT_CONFIG(permission)
172
173#if QT_CONFIG(library)
174 static void setLibraryPaths(const QStringList &);
175 static QStringList libraryPaths();
176 static void addLibraryPath(const QString &);
177 static void removeLibraryPath(const QString &);
178#endif // QT_CONFIG(library)
179
180#ifndef QT_NO_TRANSLATION
181 static bool installTranslator(QTranslator * messageFile);
182 static bool removeTranslator(QTranslator * messageFile);
183#endif
184
185 static QString translate(const char * context,
186 const char * key,
187 const char * disambiguation = nullptr,
188 int n = -1);
189
190 QT_DECLARE_NATIVE_INTERFACE_ACCESSOR(QCoreApplication)
191
192#ifndef QT_NO_QOBJECT
193 void installNativeEventFilter(QAbstractNativeEventFilter *filterObj);
194 void removeNativeEventFilter(QAbstractNativeEventFilter *filterObj);
195
196 static bool isQuitLockEnabled();
197 static void setQuitLockEnabled(bool enabled);
198
199public Q_SLOTS:
200 static void quit();
201 static void exit(int retcode = 0);
203Q_SIGNALS:
204 void aboutToQuit(QPrivateSignal);
206 void organizationNameChanged();
207 void organizationDomainChanged();
208 void applicationNameChanged();
209 void applicationVersionChanged();
210
211protected:
212 bool event(QEvent *) override;
213
214# if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
215 QT_DEPRECATED_VERSION_X_6_10("This feature will be removed in Qt 7")
216 virtual bool compressEvent(QEvent *, QObject *receiver, QPostEventList *);
217# endif
218#endif // QT_NO_QOBJECT
219
220protected:
221 QCoreApplication(QCoreApplicationPrivate &p);
222
223#ifdef QT_NO_QOBJECT
224 std::unique_ptr<QCoreApplicationPrivate> d_ptr;
225#endif
226
227private:
228#ifndef QT_NO_QOBJECT
229 static bool sendSpontaneousEvent(QObject *receiver, QEvent *event);
230 static bool notifyInternal2(QObject *receiver, QEvent *);
231 static bool forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent = nullptr);
232
233 void requestPermissionImpl(const QPermission &permission, QtPrivate::QSlotObjectBase *slotObj,
234 const QObject *context);
235#endif
236
237#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
238 static QBasicAtomicPointer<QCoreApplication> self;
239#else
240 static QCoreApplication *self;
241#endif
243 Q_DISABLE_COPY(QCoreApplication)
245 friend class QApplication;
246 friend class QApplicationPrivate;
247 friend class QGuiApplication;
248 friend class QGuiApplicationPrivate;
249 friend class QWidget;
250 friend class QWidgetWindow;
251 friend class QWidgetPrivate;
252 friend class QWindowPrivate;
253#ifndef QT_NO_QOBJECT
254 friend class QEventDispatcherUNIXPrivate;
255 friend class QCocoaEventDispatcherPrivate;
256 friend bool qt_sendSpontaneousEvent(QObject *, QEvent *);
257#endif
258 friend Q_CORE_EXPORT QString qAppName();
259 friend class QCommandLineParserPrivate;
260};
261
262#define Q_DECLARE_TR_FUNCTIONS(context) public
263 :
264 static inline QString tr(const char *sourceText, const char *disambiguation = nullptr, int n = -1)
265 { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } private
266 :
267
268typedef void (*QtStartUpFunction)();
269typedef void (*QtCleanUpFunction)();
270
271Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction);
274Q_CORE_EXPORT QString qAppName(); // get application name
275
276#define Q_COREAPP_STARTUP_FUNCTION(AFUNC)
277 static void AFUNC ## _ctor_function() {
278 qAddPreRoutine(AFUNC);
279 }
280 Q_CONSTRUCTOR_FUNCTION(AFUNC ## _ctor_function)
281
282#ifndef QT_NO_QOBJECT
283#if defined(Q_OS_WIN) && !defined(QT_NO_DEBUG_STREAM)
284Q_CORE_EXPORT QString decodeMSG(const MSG &);
285Q_CORE_EXPORT QDebug operator<<(QDebug, const MSG &);
286#endif
287#endif
288
289QT_END_NAMESPACE
290
291#include <QtCore/qcoreapplication_platform.h>
292
293#endif // QCOREAPPLICATION_H
#define ARCH_FULL
\inmodule QtCore
\inmodule QtCore
Definition qeventloop.h:60
\inmodule QtCore
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 const char * qt_build_string() noexcept
static bool pathIsRelative(const QString &path)
static QString getPrefix(QLibraryInfoPrivate::UsageMode usageMode)
void qDumpCPUFeatures()
Definition qsimd.cpp:682
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