16#include <private/qproperty_p.h>
17#include <qdatastream.h>
22#include <private/qfilesystementry_p.h>
24#include <private/qloggingregistry_p.h>
25#include <qscopeguard.h>
26#include <qstandardpaths.h>
29#include <qthreadstorage.h>
31#include <QtCore/qpromise.h>
33#include <private/qthread_p.h>
35#include <qthreadpool.h>
36#include <private/qthreadpool_p.h>
39#include <qlibraryinfo.h>
41#include <qvarlengtharray.h>
42#include <private/qfactoryloader_p.h>
43#include <private/qfunctions_p.h>
44#include <private/qlocale_p.h>
45#include <private/qlocking_p.h>
46#include <private/qhooks_p.h>
47#include <private/qnativeinterface_p.h>
50#include <private/qstdweb_p.h>
53#if QT_CONFIG(permissions)
54#include <private/qpermissions_p.h>
59# if defined(Q_OS_DARWIN)
60# include "qeventdispatcher_cf_p.h"
62# if !defined(QT_NO_GLIB)
63# include "qeventdispatcher_glib_p.h"
66# if !defined(Q_OS_WASM)
67# include "qeventdispatcher_unix_p.h"
71#include "qeventdispatcher_win_p.h"
75#if defined(Q_OS_ANDROID)
76#include <QtCore/qjniobject.h>
80# include "qcore_mac_p.h"
87# ifndef Q_OS_INTEGRITY
91# include <sys/types.h>
93# include "qcore_unix_p.h"
98# if defined(Q_OS_LINUX) && !defined(AT_EXECFN
)
108#include <emscripten/val.h>
111#ifdef QT_BOOTSTRAPPED
112#include <private/qtrace_p.h>
114#include <qtcore_tracepoints_p.h>
122# include <qt_windows.h>
127using namespace Qt::StringLiterals;
130 "#include <qcoreevent.h>"
142#if (defined(Q_OS_WIN) || defined(Q_OS_DARWIN)) && !defined(QT_BOOTSTRAPPED)
143extern QString qAppFileName();
146Q_CONSTINIT
bool QCoreApplicationPrivate::setuidAllowed =
false;
148#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
149# warning "Audit remaining direct usages of this variable for memory ordering semantics"
150Q_CONSTINIT QBasicAtomicPointer<QCoreApplication> QCoreApplication::self =
nullptr;
153Q_CONSTINIT
static QBasicAtomicPointer<QCoreApplication> g_self =
nullptr;
155# define qApp g_self.loadRelaxed()
158
159
160
161
162
163
164
165
166
167bool QCoreApplication::instanceExists()
noexcept
169 return qApp !=
nullptr;
173#if !defined(Q_OS_WIN) || defined(QT_BOOTSTRAPPED)
175QString QCoreApplicationPrivate::infoDictionaryStringProperty(
const QString &propertyName)
178 QCFString cfPropertyName = propertyName.toCFString();
179 CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(),
182 bundleName = QString::fromCFString(
static_cast<CFStringRef>(string));
186QString QCoreApplicationPrivate::appName()
const
188 QString applicationName;
190 applicationName = infoDictionaryStringProperty(QStringLiteral(
"CFBundleName"));
192 if (applicationName.isEmpty() && argv[0]) {
193 char *p = strrchr(argv[0],
'/');
194 applicationName = QString::fromLocal8Bit(p ? p + 1 : argv[0]);
197 return applicationName;
199QString QCoreApplicationPrivate::appVersion()
const
201 QString applicationVersion;
202#if defined(Q_OS_DARWIN)
203 applicationVersion = infoDictionaryStringProperty(QStringLiteral(
"CFBundleVersion"));
204#elif defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
205 QJniObject context(QNativeInterface::QAndroidApplication::context());
206 if (context.isValid()) {
207 QJniObject pm = context.callObjectMethod(
208 "getPackageManager",
"()Landroid/content/pm/PackageManager;");
209 QJniObject pn = context.callObjectMethod<jstring>(
"getPackageName");
210 if (pm.isValid() && pn.isValid()) {
211 QJniObject packageInfo = pm.callObjectMethod(
212 "getPackageInfo",
"(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;",
214 if (packageInfo.isValid()) {
215 QJniObject versionName = packageInfo.getObjectField(
216 "versionName",
"Ljava/lang/String;");
217 if (versionName.isValid())
218 return versionName.toString();
223 return applicationVersion;
227bool QCoreApplicationPrivate::checkInstance(
const char *function)
229 bool b = (
qApp !=
nullptr);
231 qWarning(
"QApplication::%s: Please instantiate the QApplication object first", function);
235#if QT_CONFIG(commandlineparser)
236void QCoreApplicationPrivate::addQtOptions(QList<QCommandLineOption> *options)
238 options->append(QCommandLineOption(QStringLiteral(
"qmljsdebugger"),
239 QStringLiteral(
"Activates the QML/JS debugger with a specified port. The value must be of format port:1234[,block]. \"block\" makes the application wait for a connection."),
240 QStringLiteral(
"value")));
244void QCoreApplicationPrivate::processCommandLineArguments()
246 int j = argc ? 1 : 0;
247 for (
int i = 1; i < argc; ++i) {
250 if (*argv[i] !=
'-') {
254 const char *arg = argv[i];
257 if (strncmp(arg,
"-qmljsdebugger=", 15) == 0) {
258 qmljs_debug_arguments = QString::fromLocal8Bit(arg + 15);
259 }
else if (strcmp(arg,
"-qmljsdebugger") == 0 && i < argc - 1) {
261 qmljs_debug_arguments = QString::fromLocal8Bit(argv[i]);
286Q_GLOBAL_STATIC(QVFuncList, postRList)
287Q_CONSTINIT
static QBasicMutex globalRoutinesMutex;
291
292
293
294
295
309 const auto locker = qt_scoped_lock(globalRoutinesMutex);
318 const auto locker = qt_scoped_lock(globalRoutinesMutex);
327 const auto locker = qt_scoped_lock(globalRoutinesMutex);
336 if (!preRList.exists())
340 const auto locker = qt_scoped_lock(globalRoutinesMutex);
347 for (QtStartUpFunction f : list)
351void Q_CORE_EXPORT qt_call_post_routines()
353 if (!postRList.exists())
360 const auto locker = qt_scoped_lock(globalRoutinesMutex);
361 qSwap(*postRList, list);
366 for (QtCleanUpFunction f : std::as_const(list))
375Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_running =
false;
377Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_closing =
false;
381 const QPostEventList &l = QThreadData::current()->postEventList;
382 return l.size() - l.startOffset;
389Q_CONSTINIT uint QCoreApplicationPrivate::attribs =
390 (1 << Qt::AA_SynthesizeMouseForUnhandledTouchEvents) |
391 (1 << Qt::AA_SynthesizeMouseForUnhandledTabletEvents);
406#if QT_CONFIG(library)
419Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata)
422Q_CONSTINIT
static bool quitLockEnabled =
true;
431static inline bool isArgvModified(
int argc,
char **argv)
433 if (__argc != argc || !__argv )
437 for (
int a = 0; a < argc; ++a) {
438 if (argv[a] != __argv[a] && strcmp(argv[a], __argv[a]))
444static inline bool contains(
int argc,
char **argv,
const char *needle)
446 for (
int a = 0; a < argc; ++a) {
447 if (!strcmp(argv[a], needle))
454QCoreApplicationPrivate::QCoreApplicationPrivate(
int &aargc,
char **aargv)
455 : argc(aargc), argv(aargv)
457 static const char *
const empty =
"";
458 if (argc == 0 || argv ==
nullptr) {
460 argv =
const_cast<
char **>(&empty);
463 if (!isArgvModified(argc, argv)) {
465 origArgv = q20::make_unique_for_overwrite<
char *[]>(argc);
466 std::copy(argv, argv + argc, origArgv.get());
471 QCoreApplicationPrivate::is_app_closing =
false;
473# if defined(Q_OS_UNIX)
474 if (Q_UNLIKELY(!setuidAllowed && (geteuid() != getuid())))
475 qFatal(
"FATAL: The application binary appears to be running setuid, this is a security hole.");
478 QThread *cur = QThread::currentThread();
479 if (cur != theMainThread.loadAcquire())
480 qWarning(
"WARNING: QApplication was not created in the main() thread.");
484QCoreApplicationPrivate::~QCoreApplicationPrivate()
489#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
490 cleanupDebuggingConsole();
496void QCoreApplicationPrivate::cleanupThreadData()
498 auto thisThreadData = threadData.loadRelaxed();
500 if (thisThreadData && !threadData_clean) {
502 QThreadStoragePrivate::finish(&thisThreadData->tls);
506 const auto locker = qt_scoped_lock(thisThreadData->postEventList.mutex);
507 for (
const QPostEvent &pe : std::as_const(thisThreadData->postEventList)) {
509 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
510 pe.event->m_posted =
false;
514 thisThreadData->postEventList.clear();
515 thisThreadData->postEventList.recursion = 0;
516 thisThreadData->quitNow =
false;
517 threadData_clean =
true;
521void QCoreApplicationPrivate::createEventDispatcher()
523 Q_Q(QCoreApplication);
524 QThreadData *data = QThreadData::current();
525 Q_ASSERT(!data->hasEventDispatcher());
526 eventDispatcher = data->createEventDispatcher();
527 eventDispatcher->setParent(q);
530void QCoreApplicationPrivate::eventDispatcherReady()
534Q_CONSTINIT QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
535Q_CONSTINIT QBasicAtomicPointer<
void> QCoreApplicationPrivate::theMainThreadId = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
536QThread *QCoreApplicationPrivate::mainThread()
538 Q_ASSERT(theMainThread.loadRelaxed() !=
nullptr);
539 return theMainThread.loadRelaxed();
542void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
544 QThread *currentThread = QThread::currentThread();
545 QThread *thr = receiver->thread();
546 Q_ASSERT_X(currentThread == thr || !thr,
547 "QCoreApplication::sendEvent",
548 qPrintable(QString::fromLatin1(
"Cannot send events to objects owned by a different thread. "
549 "Current thread %1. Receiver '%2' was created in thread %3").arg(
550 QDebug::toString(currentThread), QDebug::toString(receiver), QDebug::toString(thr))));
551 Q_UNUSED(currentThread);
559 if (!QCoreApplicationPrivate::checkInstance(
"qAppName"))
561 return QCoreApplication::instance()->d_func()->appName();
564void QCoreApplicationPrivate::initLocale()
566#if defined(QT_BOOTSTRAPPED)
568#elif defined(Q_OS_UNIX)
569 Q_CONSTINIT
static bool qt_locale_initialized =
false;
570 if (qt_locale_initialized)
572 qt_locale_initialized =
true;
577 setlocale(LC_ALL,
"");
581# if defined(Q_OS_INTEGRITY)
582 setlocale(LC_CTYPE,
"UTF-8");
583# elif defined(Q_OS_QNX)
586# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__
589# elif defined(Q_OS_VXWORKS)
593 const std::string oldEncoding = nl_langinfo(CODESET);
594 if (!Q_LIKELY(qstricmp(oldEncoding.data(),
"UTF-8") == 0
595 || qstricmp(oldEncoding.data(),
"utf8") == 0)) {
596 const QByteArray oldLocale = setlocale(LC_ALL,
nullptr);
597 QByteArray newLocale;
598 bool warnOnOverride =
true;
599# if defined(Q_OS_DARWIN)
603 warnOnOverride = qstrcmp(setlocale(LC_CTYPE,
nullptr),
"C") != 0
604 || getenv(
"LC_ALL") || getenv(
"LC_CTYPE") || getenv(
"LANG");
608 newLocale = setlocale(LC_CTYPE,
"UTF-8");
610 newLocale = setlocale(LC_CTYPE,
nullptr);
611 if (qsizetype dot = newLocale.indexOf(
'.'); dot != -1)
612 newLocale.truncate(dot);
613 if (qsizetype at = newLocale.indexOf(
'@'); at != -1)
614 newLocale.truncate(at);
615 newLocale +=
".UTF-8";
616 newLocale = setlocale(LC_CTYPE, newLocale);
619 if (newLocale.isEmpty())
620 newLocale = setlocale(LC_CTYPE,
"C.UTF-8");
621 if (newLocale.isEmpty())
622 newLocale = setlocale(LC_CTYPE,
"C.utf8");
625 if (newLocale.isEmpty()) {
627 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
628 "Qt depends on a UTF-8 locale, but has failed to switch to one.\n"
629 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
630 "for more information.", oldLocale.constData(), oldEncoding.data());
631 }
else if (warnOnOverride) {
633 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
634 "Qt depends on a UTF-8 locale, and has switched to \"%s\" instead.\n"
635 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
636 "for more information.",
637 oldLocale.constData(), oldEncoding.data(), newLocale.constData());
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
722
723
724
725
726
727
728
731
732
733QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
737 : QObject(p,
nullptr)
740 d_func()->q_ptr =
this;
746
747
748
749
750
751
752
753
754
755
756
757
758
759QCoreApplication::QCoreApplication(
int &argc,
char **argv
765 : d_ptr(
new QCoreApplicationPrivate(argc, argv))
767 : QObject(*
new QCoreApplicationPrivate(argc, argv))
770 d_func()->q_ptr =
this;
773 QCoreApplicationPrivate::eventDispatcher->startingUp();
778
779
780
781
782
784void Q_TRACE_INSTRUMENT(qtcore) QCoreApplicationPrivate::init()
786 Q_TRACE_SCOPE(QCoreApplicationPrivate_init);
788#if defined(Q_OS_MACOS)
789 QMacAutoReleasePool pool;
792 Q_Q(QCoreApplication);
794#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
795 initDebuggingConsole();
800 Q_ASSERT_X(!QCoreApplication::self,
"QCoreApplication",
"there should be only one application object");
801#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
802 QCoreApplication::self = q;
803 g_self.storeRelaxed(q);
805 QCoreApplication::self.storeRelaxed(q);
810 emscripten::val hardwareConcurrency = emscripten::val::global(
"navigator")[
"hardwareConcurrency"];
811 if (hardwareConcurrency.isUndefined())
812 QThreadPrivate::idealThreadCount = 2;
814 QThreadPrivate::idealThreadCount = hardwareConcurrency.as<
int>();
819 if (!coreappdata()->applicationNameSet)
820 coreappdata()->application = appName();
822 if (!coreappdata()->applicationVersionSet)
823 coreappdata()->applicationVersion = appVersion();
825#if defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
830 QLoggingRegistry::instance()->initializeRules();
833#if QT_CONFIG(library)
837 if (coreappdata->libPathsInitialized()) {
838 const QStringList appPaths = std::exchange(coreappdata->app_libpaths, {});
839 Q_ASSERT(!coreappdata->libPathsInitialized());
841 if (coreappdata->libPathsManuallySet()) {
842 const QStringList manualPaths = std::exchange(coreappdata->manual_libpaths, {});
843 Q_ASSERT(!coreappdata->libPathsManuallySet());
849 QStringList newPaths(q->libraryPaths());
850 for (qsizetype i = manualPaths.size(), j = appPaths.size(); i > 0 || j > 0; qt_noop()) {
852 newPaths.prepend(manualPaths[--i]);
853 }
else if (--i < 0) {
854 newPaths.removeAll(appPaths[j]);
855 }
else if (manualPaths[i] != appPaths[j]) {
856 newPaths.removeAll(appPaths[j]);
860 coreappdata->manual_libpaths.swap(newPaths);
867 Q_ASSERT(!eventDispatcher);
868 auto thisThreadData = threadData.loadRelaxed();
869 eventDispatcher = thisThreadData->eventDispatcher.loadRelaxed();
872 if (!eventDispatcher)
873 createEventDispatcher();
874 Q_ASSERT(eventDispatcher);
876 if (!eventDispatcher->parent()) {
877 eventDispatcher->moveToThread(thisThreadData->thread.loadAcquire());
878 eventDispatcher->setParent(q);
881 thisThreadData->eventDispatcher = eventDispatcher;
882 eventDispatcherReady();
885 processCommandLineArguments();
887 qt_call_pre_routines();
888 QT_MANGLE_NAMESPACE(qt_startup_hook)();
889#ifndef QT_BOOTSTRAPPED
890 QtPrivate::initBindingStatusThreadId();
891 if (Q_UNLIKELY(qtHookData[QHooks::Startup]))
892 reinterpret_cast<QHooks::StartupCallback>(qtHookData[QHooks::Startup])();
896 is_app_running =
true;
901
902
903QCoreApplication::~QCoreApplication()
905 preRoutinesCalled =
false;
907 qt_call_post_routines();
909#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
911 g_self.storeRelaxed(
nullptr);
913 self.storeRelaxed(
nullptr);
917 QCoreApplicationPrivate::is_app_closing =
true;
918 QCoreApplicationPrivate::is_app_running =
false;
923 QThreadPool *globalThreadPool =
nullptr;
925 globalThreadPool = QThreadPool::globalInstance();
929 if (globalThreadPool) {
930 globalThreadPool->waitForDone();
931 delete globalThreadPool;
936 d_func()->threadData.loadRelaxed()->eventDispatcher =
nullptr;
937 if (QCoreApplicationPrivate::eventDispatcher)
938 QCoreApplicationPrivate::eventDispatcher->closingDown();
939 QCoreApplicationPrivate::eventDispatcher =
nullptr;
942#if QT_CONFIG(library)
943 if (coreappdata.exists()) {
945 coreappdata->app_libpaths = QStringList();
946 coreappdata->manual_libpaths = QStringList();
947 Q_ASSERT(!coreappdata->libPathsManuallySet());
948 Q_ASSERT(!coreappdata->libPathsInitialized());
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976void QCoreApplication::setSetuidAllowed(
bool allow)
978 QCoreApplicationPrivate::setuidAllowed = allow;
982
983
984
985
986
987
988
989bool QCoreApplication::isSetuidAllowed()
991 return QCoreApplicationPrivate::setuidAllowed;
995
996
997
998
999
1000
1001
1002
1003
1004void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute,
bool on)
1008 static_assert(Qt::AA_AttributeCount <=
sizeof(QCoreApplicationPrivate::attribs) * CHAR_BIT);
1011 QCoreApplicationPrivate::attribs |= 1 << attribute;
1013 QCoreApplicationPrivate::attribs &= ~(1 << attribute);
1014#if defined(QT_NO_QOBJECT)
1015 if (Q_UNLIKELY(qApp)) {
1017 if (Q_UNLIKELY(QCoreApplicationPrivate::is_app_running)) {
1019 switch (attribute) {
1020 case Qt::AA_PluginApplication:
1021 case Qt::AA_UseDesktopOpenGL:
1022 case Qt::AA_UseOpenGLES:
1023 case Qt::AA_UseSoftwareOpenGL:
1024#ifdef QT_BOOTSTRAPPED
1025 qWarning(
"Attribute %d must be set before QCoreApplication is created.",
1028 qWarning(
"Attribute Qt::%s must be set before QCoreApplication is created.",
1029 QMetaEnum::fromType<Qt::ApplicationAttribute>().valueToKey(attribute));
1039
1040
1041
1042
1043
1044bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute)
1046 return QCoreApplicationPrivate::testAttribute(attribute);
1049#ifndef QT_NO_QOBJECT
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1070bool QCoreApplication::isQuitLockEnabled()
1072 return quitLockEnabled;
1075static bool doNotify(QObject *, QEvent *);
1077void QCoreApplication::setQuitLockEnabled(
bool enabled)
1079 quitLockEnabled = enabled;
1083
1084
1085
1086
1087
1088
1089bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
1095 QObjectPrivate *d = receiver->d_func();
1096 QThreadData *threadData = d->threadData.loadAcquire();
1097 bool selfRequired = threadData->requiresCoreApplication;
1098 if (selfRequired && !
qApp)
1103 bool result =
false;
1104 void *cbdata[] = { receiver, event, &result };
1105 if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) {
1109 QScopedScopeLevelCounter scopeLevelCounter(threadData);
1111 return doNotify(receiver, event);
1113#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1114 if (!QThread::isMainThread())
1117 return qApp->notify(receiver, event);
1121
1122
1123
1124
1125
1126
1127bool QCoreApplication::forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent)
1129 if (event && originatingEvent)
1130 event->m_spont = originatingEvent->m_spont;
1132 return notifyInternal2(receiver, event);
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1188bool QCoreApplication::notify(QObject *receiver, QEvent *event)
1193#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1194 Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed()
1195 == QCoreApplicationPrivate::mainThread());
1199 if (QCoreApplicationPrivate::is_app_closing)
1201 return doNotify(receiver, event);
1204static bool doNotify(QObject *receiver, QEvent *event)
1209 if (receiver ==
nullptr) {
1210 qWarning(
"QCoreApplication::notify: Unexpected null receiver");
1215 QCoreApplicationPrivate::checkReceiverThread(receiver);
1218 return receiver->isWidgetType() ?
false : QCoreApplicationPrivate::notify_helper(receiver, event);
1221bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
1224 Q_ASSERT(QThread::isMainThread());
1228 for (qsizetype i = 0; i < extraData->eventFilters.size(); ++i) {
1229 QObject *obj = extraData->eventFilters.at(i);
1232 if (obj->d_func()->threadData.loadRelaxed() != threadData.loadRelaxed()) {
1233 qWarning(
"QCoreApplication: Application event filter cannot be in a different thread.");
1236 if (obj->eventFilter(receiver, event))
1243bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
1245 if (receiver !=
qApp && receiver->d_func()->extraData) {
1246 for (qsizetype i = 0; i < receiver->d_func()->extraData->eventFilters.size(); ++i) {
1247 QObject *obj = receiver->d_func()->extraData->eventFilters.at(i);
1250 if (obj->d_func()->threadData.loadRelaxed() != receiver->d_func()->threadData.loadRelaxed()) {
1251 qWarning(
"QCoreApplication: Object event filter cannot be in a different thread.");
1254 if (obj->eventFilter(receiver, event))
1262
1263
1264
1265
1266bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
1270 Q_TRACE(QCoreApplication_notify_entry, receiver, event, event->type());
1271 bool consumed =
false;
1272 bool filtered =
false;
1273 Q_TRACE_EXIT(QCoreApplication_notify_exit, consumed, filtered);
1276 if (QThread::isMainThread()
1277 && QCoreApplication::self
1278 && QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event)) {
1283 if (sendThroughObjectEventFilters(receiver, event)) {
1289 consumed = receiver->event(event);
1294
1295
1296
1297
1298
1300bool QCoreApplication::startingUp()
1302 return !QCoreApplicationPrivate::is_app_running;
1306
1307
1308
1309
1310
1312bool QCoreApplication::closingDown()
1314 return QCoreApplicationPrivate::is_app_closing;
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags)
1349 QThreadData *data = QThreadData::current();
1350 if (!data->hasEventDispatcher())
1352 data->eventDispatcher.loadRelaxed()->processEvents(flags);
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags,
int ms)
1369 QCoreApplication::processEvents(flags, QDeadlineTimer(ms));
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline)
1401 QThreadData *data = QThreadData::current();
1402 if (!data->hasEventDispatcher())
1405 while (data->eventDispatcher.loadRelaxed()->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) {
1406 if (deadline.hasExpired())
1412
1413
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441int QCoreApplication::exec()
1443 if (!QCoreApplicationPrivate::checkInstance(
"exec"))
1446 QThreadData *threadData = self->d_func()->threadData.loadAcquire();
1447 if (threadData != QThreadData::current()) {
1448 qWarning(
"%s::exec: Must be called from the main thread", self->metaObject()->className());
1451 if (!threadData->eventLoops.isEmpty()) {
1452 qWarning(
"QCoreApplication::exec: The event loop is already running");
1456 threadData->quitNow =
false;
1457 QEventLoop eventLoop;
1458 self->d_func()->in_exec =
true;
1459 self->d_func()->aboutToQuitEmitted =
false;
1460 int returnCode = eventLoop.exec(QEventLoop::ApplicationExec);
1461 threadData->quitNow =
false;
1464 self->d_func()->execCleanup();
1475void QCoreApplicationPrivate::execCleanup()
1477 threadData.loadRelaxed()->quitNow =
false;
1479 QCoreApplication::sendPostedEvents(
nullptr, QEvent::DeferredDelete);
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513void QCoreApplication::exit(
int returnCode)
1519 if (!qstdweb::haveAsyncify())
1520 qFatal(
"Terminating since we don't have asyncify");
1522 QCoreApplicationPrivate *d = self->d_func();
1523 if (!d->aboutToQuitEmitted) {
1524 emit self->aboutToQuit(QCoreApplication::QPrivateSignal());
1525 d->aboutToQuitEmitted =
true;
1527 QThreadData *data = d->threadData.loadRelaxed();
1528 data->quitNow =
true;
1529 for (qsizetype i = 0; i < data->eventLoops.size(); ++i) {
1530 QEventLoop *eventLoop = data->eventLoops.at(i);
1531 eventLoop->exit(returnCode);
1536
1537
1539#ifndef QT_NO_QOBJECT
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
1556 Q_ASSERT_X(receiver,
"QCoreApplication::sendEvent",
"Unexpected null receiver");
1557 Q_ASSERT_X(event,
"QCoreApplication::sendEvent",
"Unexpected null event");
1559 Q_TRACE(QCoreApplication_sendEvent, receiver, event, event->type());
1561 event->m_spont =
false;
1562 return notifyInternal2(receiver, event);
1566
1567
1568bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
1570 Q_ASSERT_X(receiver,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null receiver");
1571 Q_ASSERT_X(event,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null event");
1573 Q_TRACE(QCoreApplication_sendSpontaneousEvent, receiver, event, event->type());
1575 event->m_spont =
true;
1576 return notifyInternal2(receiver, event);
1581QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThreadPostEventList(QObject *object)
1583 QPostEventListLocker locker;
1586 locker.threadData = QThreadData::current();
1587 locker.locker = qt_unique_lock(locker.threadData->postEventList.mutex);
1591 auto &threadData = QObjectPrivate::get(object)->threadData;
1596 locker.threadData = threadData.loadAcquire();
1597 if (!locker.threadData) {
1602 auto temporaryLocker = qt_unique_lock(locker.threadData->postEventList.mutex);
1603 if (locker.threadData == threadData.loadAcquire()) {
1604 locker.locker = std::move(temporaryLocker);
1609 Q_ASSERT(locker.threadData);
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649void QCoreApplication::postEvent(QObject *receiver, QEvent *event,
int priority)
1651 Q_ASSERT_X(event,
"QCoreApplication::postEvent",
"Unexpected null event");
1653 Q_TRACE_SCOPE(QCoreApplication_postEvent, receiver, event, event->type());
1656 if (receiver ==
nullptr) {
1657 qWarning(
"QCoreApplication::postEvent: Unexpected null receiver");
1662 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1663 if (!locker.threadData) {
1669 QThreadData *data = locker.threadData;
1672 QT_WARNING_DISABLE_DEPRECATED
1674 if (receiver->d_func()->postedEvents.loadAcquire()
1675 && self && self->compressEvent(event, receiver, &data->postEventList)) {
1676 Q_TRACE(QCoreApplication_postEvent_event_compressed, receiver, event);
1683 std::unique_ptr<QEvent> eventDeleter(event);
1684 Q_TRACE(QCoreApplication_postEvent_event_posted, receiver, event, event->type());
1685 data->postEventList.addEvent(QPostEvent(receiver, event, priority));
1686 Q_UNUSED(eventDeleter.release());
1687 event->m_posted =
true;
1688 receiver->d_func()->postedEvents.fetchAndAddRelease(1);
1689 data->canWait =
false;
1692 QAbstractEventDispatcher* dispatcher = data->eventDispatcher.loadAcquire();
1694 dispatcher->wakeUp();
1698
1699
1700
1701#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1702bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1704 return d_func()->compressEvent(event, receiver, postedEvents);
1708bool QCoreApplicationPrivate::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1712 Q_ASSERT(postedEvents);
1715 if (event->type() == QEvent::Timer) {
1716 const int timerId =
static_cast<QTimerEvent *>(event)->timerId();
1717 auto it = postedEvents->cbegin();
1718 const auto end = postedEvents->cend();
1720 if (it->event && it->event->type() == QEvent::Timer && it->receiver == receiver) {
1721 if (
static_cast<QTimerEvent *>(it->event)->timerId() == timerId) {
1731 if (event->type() == QEvent::Quit) {
1732 for (
const QPostEvent &cur : std::as_const(*postedEvents)) {
1733 if (cur.receiver != receiver
1734 || cur.event ==
nullptr
1735 || cur.event->type() != event->type())
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763void QCoreApplication::sendPostedEvents(QObject *receiver,
int event_type)
1768 QThreadData *data = QThreadData::current();
1770 QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data);
1773void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver,
int event_type,
1776 if (event_type == -1) {
1781 if (receiver && receiver->d_func()->threadData.loadRelaxed() != data) {
1782 qWarning(
"QCoreApplication::sendPostedEvents: Cannot send "
1783 "posted events for objects in another thread");
1787 ++data->postEventList.recursion;
1789 auto locker = qt_unique_lock(data->postEventList.mutex);
1794 data->canWait = (data->postEventList.size() == 0);
1796 if (data->postEventList.size() == 0
1797 || (receiver && !receiver->d_func()->postedEvents.loadAcquire())) {
1798 --data->postEventList.recursion;
1802 data->canWait =
true;
1806 qsizetype startOffset = data->postEventList.startOffset;
1807 qsizetype &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset;
1808 data->postEventList.insertionOffset = data->postEventList.size();
1812 Q_DISABLE_COPY_MOVE(CleanUp)
1817 bool exceptionCaught;
1819 inline CleanUp(QObject *receiver,
int event_type, QThreadData *data) :
1820 receiver(receiver), event_type(event_type), data(data), exceptionCaught(
true)
1824 if (exceptionCaught) {
1826 data->canWait =
false;
1829 --data->postEventList.recursion;
1830 if (!data->postEventList.recursion && !data->canWait && data->hasEventDispatcher())
1831 data->eventDispatcher.loadRelaxed()->wakeUp();
1835 if (!event_type && !receiver && data->postEventList.startOffset >= 0) {
1836 const QPostEventList::iterator it = data->postEventList.begin();
1837 data->postEventList.erase(it, it + data->postEventList.startOffset);
1838 data->postEventList.insertionOffset -= data->postEventList.startOffset;
1839 Q_ASSERT(data->postEventList.insertionOffset >= 0);
1840 data->postEventList.startOffset = 0;
1844 CleanUp cleanup(receiver, event_type, data);
1846 while (i < data->postEventList.size()) {
1848 if (i >= data->postEventList.insertionOffset)
1851 const QPostEvent &pe = data->postEventList.at(i);
1856 if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) {
1857 data->canWait =
false;
1861 if (pe.event->type() == QEvent::DeferredDelete) {
1868 const int eventLoopLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->loopLevel();
1869 const int eventScopeLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->scopeLevel();
1871 const bool postedBeforeOutermostLoop = eventLoopLevel == 0;
1872 const bool allowDeferredDelete =
1873 (eventLoopLevel + eventScopeLevel > data->loopLevel + data->scopeLevel
1874 || (postedBeforeOutermostLoop && data->loopLevel > 0)
1875 || (event_type == QEvent::DeferredDelete
1876 && eventLoopLevel + eventScopeLevel == data->loopLevel + data->scopeLevel));
1877 if (!allowDeferredDelete) {
1879 if (!event_type && !receiver) {
1884 QPostEvent pe_copy = pe;
1888 const_cast<QPostEvent &>(pe).event =
nullptr;
1891 data->postEventList.addEvent(pe_copy);
1899 pe.event->m_posted =
false;
1900 QEvent *e = pe.event;
1901 QObject * r = pe.receiver;
1904 const auto previous = r->d_func()->postedEvents.fetchAndSubAcquire(1);
1905 Q_ASSERT(previous > 0);
1909 const_cast<QPostEvent &>(pe).event =
nullptr;
1912 const auto relocker = qScopeGuard([&locker] { locker.lock(); });
1914 const std::unique_ptr<QEvent> event_deleter(e);
1917 QCoreApplication::sendEvent(r, e);
1924 cleanup.exceptionCaught =
false;
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1946void QCoreApplication::removePostedEvents(QObject *receiver,
int eventType)
1948 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1949 QThreadData *data = locker.threadData;
1955 if (receiver && !receiver->d_func()->postedEvents.loadAcquire())
1960 QVarLengthArray<QEvent*> events;
1961 qsizetype n = data->postEventList.size();
1964 for (qsizetype i = 0; i < n; ++i) {
1965 const QPostEvent &pe = data->postEventList.at(i);
1967 if ((!receiver || pe.receiver == receiver)
1968 && (pe.event && (eventType == 0 || pe.event->type() == eventType))) {
1969 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
1970 pe.event->m_posted =
false;
1971 events.append(pe.event);
1972 const_cast<QPostEvent &>(pe).event =
nullptr;
1973 }
else if (!data->postEventList.recursion) {
1975 qSwap(data->postEventList[i], data->postEventList[j]);
1981 if (receiver && eventType == 0) {
1982 Q_ASSERT(!receiver->d_func()->postedEvents.loadRelaxed());
1986 if (!data->postEventList.recursion) {
1988 data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end());
1996
1997
1998
1999
2000
2001
2002
2003
2004
2006void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
2008 if (!event || !event->m_posted)
2011 QThreadData *data = QThreadData::current();
2013 const auto locker = qt_scoped_lock(data->postEventList.mutex);
2015 if (data->postEventList.size() == 0) {
2016#if defined(QT_DEBUG)
2017 qDebug(
"QCoreApplication::removePostedEvent: Internal error: %p %d is posted",
2018 (
void*)event, event->type());
2023 for (
const QPostEvent &pe : std::as_const(data->postEventList)) {
2024 if (pe.event == event) {
2026 qWarning(
"QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s",
2028 pe.receiver->metaObject()->className(),
2029 pe.receiver->objectName().toLocal8Bit().data());
2031 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
2032 pe.event->m_posted =
false;
2034 const_cast<QPostEvent &>(pe).event =
nullptr;
2041
2042
2043bool QCoreApplication::event(QEvent *e)
2045 if (e->type() == QEvent::Quit) {
2049 return QObject::event(e);
2052void QCoreApplicationPrivate::ref()
2057void QCoreApplicationPrivate::deref()
2059 quitLockRef.deref();
2061 if (quitLockEnabled && canQuitAutomatically())
2062 quitAutomatically();
2065bool QCoreApplicationPrivate::canQuitAutomatically()
2076 if (quitLockRef.loadRelaxed())
2082void QCoreApplicationPrivate::quitAutomatically()
2084 Q_Q(QCoreApplication);
2092 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129void QCoreApplication::quit()
2134 if (!self->d_func()->in_exec)
2137 self->d_func()->quit();
2140void QCoreApplicationPrivate::quit()
2142 Q_Q(QCoreApplication);
2144 if (QThread::isMainThread()) {
2145 QEvent quitEvent(QEvent::Quit);
2146 QCoreApplication::sendEvent(q, &quitEvent);
2148 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2175#ifndef QT_NO_TRANSLATION
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2207bool QCoreApplication::installTranslator(QTranslator *translationFile)
2209 if (!translationFile)
2212 if (!QCoreApplicationPrivate::checkInstance(
"installTranslator"))
2215 QCoreApplicationPrivate *d = self->d_func();
2217 QWriteLocker locker(&d->translateMutex);
2218 d->translators.prepend(translationFile);
2221 if (translationFile->isEmpty())
2224#ifndef QT_NO_QOBJECT
2225 QEvent ev(QEvent::LanguageChange);
2226 QCoreApplication::sendEvent(self, &ev);
2233
2234
2235
2236
2237
2238
2239
2240
2242bool QCoreApplication::removeTranslator(QTranslator *translationFile)
2244 if (!translationFile)
2246 if (!QCoreApplicationPrivate::checkInstance(
"removeTranslator"))
2248 QCoreApplicationPrivate *d = self->d_func();
2249 QWriteLocker locker(&d->translateMutex);
2250 if (d->translators.removeAll(translationFile)) {
2251#ifndef QT_NO_QOBJECT
2253 if (!self->closingDown()) {
2254 QEvent ev(QEvent::LanguageChange);
2255 QCoreApplication::sendEvent(self, &ev);
2266 qsizetype percentPos = 0;
2268 while ((percentPos = result->indexOf(u'%', percentPos + len)) != -1) {
2270 if (percentPos + len == result->size())
2273 if (result->at(percentPos + len) == u'L') {
2275 if (percentPos + len == result->size())
2281 if (result->at(percentPos + len) == u'n') {
2284 result->replace(percentPos, len, fmt);
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2325 const char *disambiguation,
int n)
2333 QCoreApplicationPrivate *d = self->d_func();
2334 QReadLocker locker(&d->translateMutex);
2335 if (!d->translators.isEmpty()) {
2336 QList<QTranslator*>::ConstIterator it;
2337 QTranslator *translationFile;
2338 for (it = d->translators.constBegin(); it != d->translators.constEnd(); ++it) {
2339 translationFile = *it;
2340 result = translationFile->translate(context, sourceText, disambiguation, n);
2341 if (!result.isNull())
2347 if (result.isNull())
2348 result = QString::fromUtf8(sourceText);
2350 replacePercentN(&result, n);
2357 return QCoreApplication::translate(
nullptr, id,
nullptr, n);
2360bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
2362 if (!QCoreApplication::self)
2364 QCoreApplicationPrivate *d = QCoreApplication::self->d_func();
2365 QReadLocker locker(&d->translateMutex);
2366 return d->translators.contains(translator);
2371QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2372 const char *disambiguation,
int n)
2375 Q_UNUSED(disambiguation);
2376 QString ret = QString::fromUtf8(sourceText);
2378 ret.replace(
"%n"_L1, QString::number(n));
2384#ifndef QT_BOOTSTRAPPED
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408QString QCoreApplication::applicationDirPath()
2411 qWarning(
"QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
2415 QFileSystemEntry appFilePath(applicationFilePath(), QFileSystemEntry::FromInternalPath{});
2416 return appFilePath.isEmpty() ? QString() : appFilePath.path();
2419#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN)
2423# if defined(Q_OS_ANDROID)
2426# elif defined(Q_OS_LINUX)
2429# if QT_CONFIG(getauxval)
2431 if (
auto ptr =
reinterpret_cast<
const char *>(getauxval(AT_EXECFN)); ptr && *ptr ==
'/')
2432 return QFile::decodeName(ptr);
2436 return QFile::decodeName(qt_readlink(
"/proc/self/exe"));
2437# elif defined(AT_EXECPATH)
2439 char execfn[PATH_MAX];
2440 if (elf_aux_info(AT_EXECPATH, execfn,
sizeof(execfn)) != 0)
2443 qsizetype len = qstrlen(execfn);
2444 return QFile::decodeName(QByteArray::fromRawData(execfn, len));
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467QString QCoreApplication::applicationFilePath()
2470 qWarning(
"QCoreApplication::applicationFilePath: Please instantiate the QApplication object first");
2474 QCoreApplicationPrivate *d = self->d_func();
2477 static QByteArray procName = QByteArray(d->argv[0]);
2478 if (procName != QByteArrayView(d->argv[0])) {
2480 d->cachedApplicationFilePath = QString();
2481 procName.assign(d->argv[0]);
2485 if (!d->cachedApplicationFilePath.isNull())
2486 return d->cachedApplicationFilePath;
2488 QString absPath = qAppFileName();
2489 if (Q_LIKELY(!absPath.isEmpty())) {
2491 return d->cachedApplicationFilePath = std::move(absPath);
2494 if (
const QStringList args = arguments(); !args.isEmpty()) {
2495 const QString &argv0 = args[0];
2497 if (!argv0.isEmpty() && argv0.at(0) == u'/') {
2499
2500
2501
2503 }
else if (argv0.contains(u'/')) {
2505
2506
2507
2508 absPath = QDir::current().absoluteFilePath(argv0);
2511
2512
2513
2514 absPath = QStandardPaths::findExecutable(argv0);
2518 absPath = QFileInfo(absPath).canonicalFilePath();
2519 if (!absPath.isEmpty()) {
2520 return d->cachedApplicationFilePath = std::move(absPath);
2527
2528
2529
2530
2531qint64 QCoreApplication::applicationPid()
noexcept
2533#if defined(Q_OS_WIN)
2534 return GetCurrentProcessId();
2535#elif defined(Q_OS_VXWORKS)
2536 return (pid_t) taskIdCurrent;
2543static QStringList winCmdArgs()
2552 if (
wchar_t **argv = CommandLineToArgvW(GetCommandLine(), &size)) {
2553 result.reserve(size);
2554 wchar_t **argvEnd = argv + size;
2555 for (
wchar_t **a = argv; a < argvEnd; ++a)
2556 result.append(QString::fromWCharArray(*a));
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2595QStringList QCoreApplication::arguments()
2600 qWarning(
"QCoreApplication::arguments: Please instantiate the QApplication object first");
2604 const QCoreApplicationPrivate *d = self->d_func();
2606 const int argc = d->argc;
2607 char **
const argv = d->argv;
2610#if defined(Q_OS_WIN)
2611 const bool argsModifiedByUser = d->origArgv ==
nullptr;
2612 if (!argsModifiedByUser) {
2613 QStringList commandLineArguments = winCmdArgs();
2618 if (argc != d->origArgc) {
2625 for (
int i = 0; i < d->origArgc; ++i) {
2626 if (!contains(argc, argv, d->origArgv[i]))
2627 commandLineArguments.removeAll(QString::fromLocal8Bit(d->origArgv[i]));
2631 return commandLineArguments;
2635 for (
int a = 0; a < argc; ++a)
2636 list << QString::fromLocal8Bit(argv[a]);
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2658
2659
2660
2661
2662
2663
2664
2665void QCoreApplication::setOrganizationName(
const QString &orgName)
2667 if (coreappdata()->orgName == orgName)
2669 coreappdata()->orgName = orgName;
2670#ifndef QT_NO_QOBJECT
2671 if (QCoreApplication::self)
2672 emit QCoreApplication::self->organizationNameChanged();
2676QString QCoreApplication::organizationName()
2678 return coreappdata()->orgName;
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2697
2698
2699
2700
2701
2702void QCoreApplication::setOrganizationDomain(
const QString &orgDomain)
2704 if (coreappdata()->orgDomain == orgDomain)
2706 coreappdata()->orgDomain = orgDomain;
2707#ifndef QT_NO_QOBJECT
2708 if (QCoreApplication::self)
2709 emit QCoreApplication::self->organizationDomainChanged();
2713QString QCoreApplication::organizationDomain()
2715 return coreappdata()->orgDomain;
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2735
2736
2737
2738
2739
2740void QCoreApplication::setApplicationName(
const QString &application)
2742 coreappdata()->applicationNameSet = !application.isEmpty();
2743 QString newAppName = application;
2744 if (newAppName.isEmpty() && QCoreApplication::self)
2745 newAppName = QCoreApplication::self->d_func()->appName();
2746 if (coreappdata()->application == newAppName)
2748 coreappdata()->application = newAppName;
2749#ifndef QT_NO_QOBJECT
2750 if (QCoreApplication::self)
2751 emit QCoreApplication::self->applicationNameChanged();
2755QString QCoreApplication::applicationName()
2757 return coreappdata() ? coreappdata()->application : QString();
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2788
2789
2790
2791
2792
2793void QCoreApplication::setApplicationVersion(
const QString &version)
2795 coreappdata()->applicationVersionSet = !version.isEmpty();
2796 QString newVersion = version;
2797 if (newVersion.isEmpty() && QCoreApplication::self)
2798 newVersion = QCoreApplication::self->d_func()->appVersion();
2799 if (coreappdata()->applicationVersion == newVersion)
2801 coreappdata()->applicationVersion = newVersion;
2802#ifndef QT_NO_QOBJECT
2803 if (QCoreApplication::self)
2804 emit QCoreApplication::self->applicationVersionChanged();
2808QString QCoreApplication::applicationVersion()
2810 return coreappdata() ? coreappdata()->applicationVersion : QString();
2813#if QT_CONFIG(permissions) || defined(Q_QDOC)
2816
2817
2818
2819
2820
2821
2822
2823
2824Qt::PermissionStatus QCoreApplication::checkPermission(
const QPermission &permission)
2826 return QPermissions::Private::checkPermission(permission);
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2896
2897
2898
2899
2900
2901
2902
2903void QCoreApplication::requestPermissionImpl(
const QPermission &requestedPermission,
2904 QtPrivate::QSlotObjectBase *slotObjRaw,
const QObject *context)
2906 QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw};
2909 if (!QThread::isMainThread()) {
2910 qCWarning(lcPermissions,
"Permissions can only be requested from the GUI (main) thread");
2914 class PermissionReceiver :
public QObject
2917 explicit PermissionReceiver(QtPrivate::SlotObjUniquePtr &&slotObject,
const QObject *context)
2918 : slotObject(std::move(slotObject)), context(context ? context :
this)
2920 Q_ASSERT(
this->context);
2921 moveToThread(
this->context->thread());
2924 void finalizePermissionRequest(
const QPermission &permission)
2926 Q_ASSERT(slotObject);
2929 void *args[] = {
nullptr,
const_cast<QPermission *>(&permission) };
2930 slotObject->call(
const_cast<QObject *>(context.data()), args);
2935 QtPrivate::SlotObjUniquePtr slotObject;
2936 QPointer<
const QObject> context;
2940 auto receiver = std::make_shared<PermissionReceiver>(std::move(slotObj), context);
2942 QPermissions::Private::requestPermission(requestedPermission,
2943 [=, receiver = std::move(receiver)](Qt::PermissionStatus status)
mutable {
2944 if (status == Qt::PermissionStatus::Undetermined) {
2945 Q_ASSERT_X(
false,
"QPermission",
2946 "Internal error: requestPermission() should never return Undetermined");
2947 status = Qt::PermissionStatus::Denied;
2950 if (QCoreApplication::self) {
2951 QPermission permission = requestedPermission;
2952 permission.m_status = status;
2953 auto receiverObject = receiver.get();
2954 QMetaObject::invokeMethod(receiverObject,
2955 [receiver = std::move(receiver), permission] {
2956 receiver->finalizePermissionRequest(permission);
2957 }, Qt::QueuedConnection);
2964#if QT_CONFIG(library)
2965static QStringList libraryPathsLocked();
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996QStringList QCoreApplication::libraryPaths()
2998 QMutexLocker locker(&coreappdata->libraryPathMutex);
2999 return libraryPathsLocked();
3003
3004
3005static QStringList libraryPathsLocked()
3007 QCoreApplicationData *d = coreappdata;
3008 if (d->libPathsManuallySet())
3009 return d->manual_libpaths;
3011 QStringList *app_libpaths = &d->app_libpaths;
3012 if (!d->libPathsInitialized()) {
3014 auto setPathsFromEnv = [&](QString libPathEnv) {
3015 if (!libPathEnv.isEmpty()) {
3016 QStringList paths = libPathEnv.split(QDir::listSeparator(), Qt::SkipEmptyParts);
3017 for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
3018 QString canonicalPath = QDir(*it).canonicalPath();
3019 if (!canonicalPath.isEmpty()
3020 && !app_libpaths->contains(canonicalPath)) {
3021 app_libpaths->append(canonicalPath);
3026 setPathsFromEnv(qEnvironmentVariable(
"QT_PLUGIN_PATH"));
3032 if (CFBundleRef bundleRef = CFBundleGetMainBundle()) {
3033 if (QCFType<CFURLRef> urlRef = CFBundleCopyBuiltInPlugInsURL(bundleRef)) {
3034 if (QCFType<CFURLRef> absoluteUrlRef = CFURLCopyAbsoluteURL(urlRef)) {
3035 if (QCFString path = CFURLCopyFileSystemPath(absoluteUrlRef, kCFURLPOSIXPathStyle)) {
3036 if (QFile::exists(path)) {
3037 path = QDir(path).canonicalPath();
3038 if (!app_libpaths->contains(path))
3039 app_libpaths->append(path);
3047 QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
3048 if (QFile::exists(installPathPlugins)) {
3050 installPathPlugins = QDir(installPathPlugins).canonicalPath();
3051 if (!app_libpaths->contains(installPathPlugins))
3052 app_libpaths->append(installPathPlugins);
3058 QString app_location = QCoreApplication::applicationFilePath();
3059 app_location.truncate(app_location.lastIndexOf(u'/'));
3060 app_location = QDir(app_location).canonicalPath();
3061 if (QFile::exists(app_location) && !app_libpaths->contains(app_location))
3062 app_libpaths->append(app_location);
3064 if (app_libpaths->isEmpty())
3065 app_libpaths->reserve(1);
3066 Q_ASSERT(d->libPathsInitialized());
3068 return *app_libpaths;
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085void QCoreApplication::setLibraryPaths(
const QStringList &paths)
3087 QCoreApplicationData *d = coreappdata;
3088 QMutexLocker locker(&d->libraryPathMutex);
3093 if (!d->libPathsInitialized())
3094 libraryPathsLocked();
3096 d->manual_libpaths = paths;
3097 if (d->manual_libpaths.isEmpty())
3098 d->manual_libpaths.reserve(1);
3099 Q_ASSERT(d->libPathsManuallySet());
3102 QFactoryLoader::refreshAll();
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121void QCoreApplication::addLibraryPath(
const QString &path)
3126 QString canonicalPath = QDir(path).canonicalPath();
3127 if (canonicalPath.isEmpty())
3130 QCoreApplicationData *d = coreappdata;
3131 QMutexLocker locker(&d->libraryPathMutex);
3133 QStringList *libpaths = &d->manual_libpaths;
3134 if (d->libPathsManuallySet()) {
3135 if (d->manual_libpaths.contains(canonicalPath))
3139 libraryPathsLocked();
3140 QStringList *app_libpaths = &d->app_libpaths;
3141 if (app_libpaths->contains(canonicalPath))
3144 *libpaths = *app_libpaths;
3147 libpaths->prepend(canonicalPath);
3148 Q_ASSERT(d->libPathsManuallySet());
3150 QFactoryLoader::refreshAll();
3154
3155
3156
3157
3158
3159
3160
3161
3162void QCoreApplication::removeLibraryPath(
const QString &path)
3167 QString canonicalPath = QDir(path).canonicalPath();
3168 if (canonicalPath.isEmpty())
3171 QCoreApplicationData *d = coreappdata;
3172 QMutexLocker locker(&d->libraryPathMutex);
3174 QStringList *libpaths = &d->manual_libpaths;
3175 if (d->libPathsManuallySet()) {
3176 if (libpaths->removeAll(canonicalPath) == 0)
3180 libraryPathsLocked();
3181 QStringList *app_libpaths = &d->app_libpaths;
3182 if (!app_libpaths->contains(canonicalPath))
3185 *libpaths = *app_libpaths;
3186 libpaths->removeAll(canonicalPath);
3187 Q_ASSERT(d->libPathsManuallySet());
3191 QFactoryLoader::refreshAll();
3196#ifndef QT_NO_QOBJECT
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
3229 if (QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
3230 qWarning(
"Native event filters are not applied when the Qt::AA_PluginApplication attribute is set");
3234 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3235 if (!filterObj || !eventDispatcher)
3237 eventDispatcher->installNativeEventFilter(filterObj);
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253void QCoreApplication::removeNativeEventFilter(QAbstractNativeEventFilter *filterObject)
3255 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3256 if (!filterObject || !eventDispatcher)
3258 eventDispatcher->removeNativeEventFilter(filterObject);
3262
3263
3264
3265QAbstractEventDispatcher *QCoreApplication::eventDispatcher()
3267 if (QCoreApplicationPrivate::theMainThread.loadAcquire())
3268 return QCoreApplicationPrivate::theMainThread.loadRelaxed()->eventDispatcher();
3273
3274
3275
3276
3277
3278void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
3280 QThread *mainThread = QCoreApplicationPrivate::theMainThread.loadAcquire();
3282 mainThread = QThread::currentThread();
3283 mainThread->setEventDispatcher(eventDispatcher);
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3397void *QCoreApplication::resolveInterface(
const char *name,
int revision)
const
3399#if defined(Q_OS_ANDROID)
3404 using namespace QNativeInterface;
3405 struct AndroidApplication :
public QAndroidApplication {};
3406 static AndroidApplication androidApplication;
3407 QT_NATIVE_INTERFACE_RETURN_IF(QAndroidApplication, &androidApplication);
3409 Q_UNUSED(name); Q_UNUSED(revision);
3415#ifndef QT_NO_QOBJECT
3416#include "moc_qcoreapplication.cpp"
Q_TRACE_POINT(qtcore, QCoreApplication_notify_exit, bool consumed, bool filtered)
void QT_MANGLE_NAMESPACE qt_startup_hook()
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
QString qtTrId(const char *id, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_exit)
static void qt_call_pre_routines()
qsizetype qGlobalPostedEventsCount()
static Q_CONSTINIT bool preRoutinesCalled
QList< QtCleanUpFunction > QVFuncList
static void replacePercentN(QString *result, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_event_compressed, QObject *receiver, QEvent *event)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_entry, QObject *receiver, QEvent *event, QEvent::Type type)
Q_TRACE_PREFIX(qtcore, "#include <qcoreevent.h>")
Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction)
void(* QtCleanUpFunction)()
void(* QtStartUpFunction)()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QString applicationVersion
QCoreApplicationData() noexcept
bool applicationVersionSet