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 <qscopedvaluerollback.h>
28#include <qstandardpaths.h>
31#include <qthreadstorage.h>
33#include <QtCore/qpromise.h>
35#include <private/qthread_p.h>
37#include <qthreadpool.h>
38#include <private/qthreadpool_p.h>
41#include <qlibraryinfo.h>
43#include <qvarlengtharray.h>
44#include <private/qfactoryloader_p.h>
45#include <private/qfunctions_p.h>
46#include <private/qlocale_p.h>
47#include <private/qlocking_p.h>
48#include <private/qhooks_p.h>
49#include <private/qnativeinterface_p.h>
52#include <private/qstdweb_p.h>
55#if QT_CONFIG(permissions)
56#include <private/qpermissions_p.h>
61# if defined(Q_OS_DARWIN)
62# include "qeventdispatcher_cf_p.h"
64# if !defined(QT_NO_GLIB)
65# include "qeventdispatcher_glib_p.h"
68# if !defined(Q_OS_WASM)
69# include "qeventdispatcher_unix_p.h"
73#include "qeventdispatcher_win_p.h"
77#if defined(Q_OS_ANDROID)
78#include <QtCore/qjniobject.h>
81#if defined(Q_OS_HARMONY) && !defined(QT_BOOTSTRAPPED)
82#include <QtCore/private/qcore_ohos_p.h>
83#include <QtCore/private/qnapi_p.h>
84#include <QtCore/private/qohosmemoizingjsthreadfetcher_p.h>
88# include "qcore_mac_p.h"
95# ifndef Q_OS_INTEGRITY
99# include <sys/types.h>
101# include "qcore_unix_p.h"
105# include <sys/auxv.h>
113#include <emscripten/val.h>
116#ifdef QT_BOOTSTRAPPED
117#include <private/qtrace_p.h>
119#include <qtcore_tracepoints_p.h>
127# include <qt_windows.h>
132using namespace Qt::StringLiterals;
135 "#include <qcoreevent.h>"
147#if (defined(Q_OS_WIN) || defined(Q_OS_DARWIN)) && !defined(QT_BOOTSTRAPPED)
148extern QString qAppFileName();
151Q_CONSTINIT
bool QCoreApplicationPrivate::setuidAllowed =
false;
153#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
154# warning "Audit remaining direct usages of this variable for memory ordering semantics"
155Q_CONSTINIT QBasicAtomicPointer<QCoreApplication> QCoreApplication::self =
nullptr;
158Q_CONSTINIT
static QBasicAtomicPointer<QCoreApplication> g_self =
nullptr;
160# define qApp g_self.loadRelaxed()
163
164
165
166
167
168
169
170
171
172bool QCoreApplication::instanceExists()
noexcept
174 return qApp !=
nullptr;
178#if !defined(Q_OS_WIN) || defined(QT_BOOTSTRAPPED)
180QString QCoreApplicationPrivate::infoDictionaryStringProperty(
const QString &propertyName)
183 QCFString cfPropertyName = propertyName.toCFString();
184 CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(),
187 bundleName = QString::fromCFString(
static_cast<CFStringRef>(string));
191QString QCoreApplicationPrivate::appName()
const
193 QString applicationName;
195 applicationName = infoDictionaryStringProperty(QStringLiteral(
"CFBundleName"));
197 if (applicationName.isEmpty() && argv[0]) {
198 char *p = strrchr(argv[0],
'/');
199 applicationName = QString::fromLocal8Bit(p ? p + 1 : argv[0]);
202 return applicationName;
205#if defined(Q_OS_HARMONY) && !defined(QT_BOOTSTRAPPED)
206static QOhosMemoizingJsThreadFetcher<QString> bundleVersionNameFetcher(
207 [](QOhosJsState &jsState) -> std::optional<QString> {
208 const auto flag = jsState.eval<QNapi::Number>(
209 "@ohos.bundle.bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION");
210 return QString::fromStdString(
211 jsState.eval<QNapi::String>(
212 "@ohos.bundle.bundleManager.getBundleInfoForSelfSync(*).versionName",
215 "BundleInfo.versionName");
218QString QCoreApplicationPrivate::appVersion()
const
220 QString applicationVersion;
221#if defined(Q_OS_DARWIN)
222 applicationVersion = infoDictionaryStringProperty(QStringLiteral(
"CFBundleVersion"));
223#elif defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
224 QJniObject context(QNativeInterface::QAndroidApplication::context());
225 if (context.isValid()) {
226 QJniObject pm = context.callObjectMethod(
227 "getPackageManager",
"()Landroid/content/pm/PackageManager;");
228 QJniObject pn = context.callObjectMethod<jstring>(
"getPackageName");
229 if (pm.isValid() && pn.isValid()) {
230 QJniObject packageInfo = pm.callObjectMethod(
231 "getPackageInfo",
"(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;",
233 if (packageInfo.isValid()) {
234 QJniObject versionName = packageInfo.getObjectField(
235 "versionName",
"Ljava/lang/String;");
236 if (versionName.isValid())
237 return versionName.toString();
241#elif defined(Q_OS_HARMONY) && !defined(QT_BOOTSTRAPPED)
242 applicationVersion = bundleVersionNameFetcher.optValue().value_or(QString());
244 return applicationVersion;
248bool QCoreApplicationPrivate::checkInstance(
const char *function)
250 bool b = (
qApp !=
nullptr);
252 qWarning(
"QApplication::%s: Please instantiate the QApplication object first", function);
256#if QT_CONFIG(commandlineparser)
257void QCoreApplicationPrivate::addQtOptions(QList<QCommandLineOption> *options)
259 options->append(QCommandLineOption(QStringLiteral(
"qmljsdebugger"),
260 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."),
261 QStringLiteral(
"value")));
265void QCoreApplicationPrivate::processCommandLineArguments()
267 int j = argc ? 1 : 0;
268 for (
int i = 1; i < argc; ++i) {
271 if (*argv[i] !=
'-') {
275 const char *arg = argv[i];
278 if (strncmp(arg,
"-qmljsdebugger=", 15) == 0) {
279 qmljs_debug_arguments = QString::fromLocal8Bit(arg + 15);
280 }
else if (strcmp(arg,
"-qmljsdebugger") == 0 && i < argc - 1) {
282 qmljs_debug_arguments = QString::fromLocal8Bit(argv[i]);
307Q_GLOBAL_STATIC(QVFuncList, postRList)
308Q_CONSTINIT
static QBasicMutex globalRoutinesMutex;
312
313
314
315
316
330 const auto locker = qt_scoped_lock(globalRoutinesMutex);
339 const auto locker = qt_scoped_lock(globalRoutinesMutex);
348 const auto locker = qt_scoped_lock(globalRoutinesMutex);
357 if (!preRList.exists())
361 const auto locker = qt_scoped_lock(globalRoutinesMutex);
368 for (QtStartUpFunction f : list)
372void Q_CORE_EXPORT qt_call_post_routines()
374 if (!postRList.exists())
381 const auto locker = qt_scoped_lock(globalRoutinesMutex);
382 qSwap(*postRList, list);
387 for (QtCleanUpFunction f : std::as_const(list))
396Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_running =
false;
398Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_closing =
false;
402 const QPostEventList &l = QThreadData::current()->postEventList;
403 return l.size() - l.startOffset;
410Q_CONSTINIT uint QCoreApplicationPrivate::attribs =
411 (1 << Qt::AA_SynthesizeMouseForUnhandledTouchEvents) |
412 (1 << Qt::AA_SynthesizeMouseForUnhandledTabletEvents);
427#if QT_CONFIG(library)
440Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata)
443Q_CONSTINIT
static bool quitLockEnabled =
true;
452static inline bool isArgvModified(
int argc,
char **argv)
454 if (__argc != argc || !__argv )
458 for (
int a = 0; a < argc; ++a) {
459 if (argv[a] != __argv[a] && strcmp(argv[a], __argv[a]))
465static inline bool contains(
int argc,
char **argv,
const char *needle)
467 for (
int a = 0; a < argc; ++a) {
468 if (!strcmp(argv[a], needle))
475QCoreApplicationPrivate::QCoreApplicationPrivate(
int &aargc,
char **aargv)
476 : argc(aargc), argv(aargv)
478 static const char *
const empty =
"";
479 if (argc == 0 || argv ==
nullptr) {
481 argv =
const_cast<
char **>(&empty);
484 if (!isArgvModified(argc, argv)) {
486 origArgv = q20::make_unique_for_overwrite<
char *[]>(argc);
487 std::copy(argv, argv + argc, origArgv.get());
492 QCoreApplicationPrivate::is_app_closing =
false;
494# if defined(Q_OS_UNIX)
495 if (Q_UNLIKELY(!setuidAllowed && (geteuid() != getuid())))
496 qFatal(
"FATAL: The application binary appears to be running setuid, this is a security hole.");
499 QThread *cur = QThread::currentThread();
500 if (cur != theMainThread.loadAcquire())
501 qWarning(
"WARNING: QApplication was not created in the main() thread.");
505QCoreApplicationPrivate::~QCoreApplicationPrivate()
510#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
511 cleanupDebuggingConsole();
517void QCoreApplicationPrivate::cleanupThreadData()
519 auto thisThreadData = threadData.loadRelaxed();
521 if (thisThreadData && !threadData_clean) {
523 QThreadStoragePrivate::finish(&thisThreadData->tls);
527 thisThreadData->clearEvents();
528 thisThreadData->quitNow =
false;
529 threadData_clean =
true;
533void QCoreApplicationPrivate::createEventDispatcher()
535 Q_Q(QCoreApplication);
536 QThreadData *data = QThreadData::current();
537 Q_ASSERT(!data->hasEventDispatcher());
538 eventDispatcher = data->createEventDispatcher();
539 eventDispatcher->setParent(q);
542void QCoreApplicationPrivate::eventDispatcherReady()
546Q_CONSTINIT QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
547Q_CONSTINIT QBasicAtomicPointer<
void> QCoreApplicationPrivate::theMainThreadId = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
548QThread *QCoreApplicationPrivate::mainThread()
550 Q_ASSERT(theMainThread.loadRelaxed() !=
nullptr);
551 return theMainThread.loadRelaxed();
558 if (!QCoreApplicationPrivate::checkInstance(
"qAppName"))
560 return QCoreApplication::instance()->d_func()->appName();
563void QCoreApplicationPrivate::initLocale()
565#if defined(QT_BOOTSTRAPPED)
567#elif defined(Q_OS_UNIX)
568 Q_CONSTINIT
static bool qt_locale_initialized =
false;
569 if (qt_locale_initialized)
571 qt_locale_initialized =
true;
576 setlocale(LC_ALL,
"");
580# if defined(Q_OS_INTEGRITY)
581 setlocale(LC_CTYPE,
"UTF-8");
582# elif defined(Q_OS_QNX)
585# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__
588# elif defined(Q_OS_VXWORKS)
592 const std::string oldEncoding = nl_langinfo(CODESET);
593 if (!Q_LIKELY(qstricmp(oldEncoding.data(),
"UTF-8") == 0
594 || qstricmp(oldEncoding.data(),
"utf8") == 0)) {
595 const QByteArray oldLocale = setlocale(LC_ALL,
nullptr);
596 QByteArray newLocale;
597 bool warnOnOverride =
true;
598# if defined(Q_OS_DARWIN)
602 warnOnOverride = qstrcmp(setlocale(LC_CTYPE,
nullptr),
"C") != 0
603 || getenv(
"LC_ALL") || getenv(
"LC_CTYPE") || getenv(
"LANG");
607 newLocale = setlocale(LC_CTYPE,
"UTF-8");
609 newLocale = setlocale(LC_CTYPE,
nullptr);
610 if (qsizetype dot = newLocale.indexOf(
'.'); dot != -1)
611 newLocale.truncate(dot);
612 if (qsizetype at = newLocale.indexOf(
'@'); at != -1)
613 newLocale.truncate(at);
614 newLocale +=
".UTF-8";
615 newLocale = setlocale(LC_CTYPE, newLocale);
618 if (newLocale.isEmpty())
619 newLocale = setlocale(LC_CTYPE,
"C.UTF-8");
620 if (newLocale.isEmpty())
621 newLocale = setlocale(LC_CTYPE,
"C.utf8");
624 if (newLocale.isEmpty()) {
626 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
627 "Qt depends on a UTF-8 locale, but has failed to switch to one.\n"
628 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
629 "for more information.", oldLocale.constData(), oldEncoding.data());
630 }
else if (warnOnOverride) {
632 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
633 "Qt depends on a UTF-8 locale, and has switched to \"%s\" instead.\n"
634 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
635 "for more information.",
636 oldLocale.constData(), oldEncoding.data(), newLocale.constData());
645
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
721
722
723
724
725
726
727
728
729
730
731
734
735
736QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
740 : QObject(p,
nullptr)
743 d_func()->q_ptr =
this;
749
750
751
752
753
754
755
756
757
758
759
760
761
762QCoreApplication::QCoreApplication(
int &argc,
char **argv
768 : d_ptr(
new QCoreApplicationPrivate(argc, argv))
770 : QObject(*
new QCoreApplicationPrivate(argc, argv))
773 d_func()->q_ptr =
this;
776 QCoreApplicationPrivate::eventDispatcher->startingUp();
781
782
783
784
785
787void Q_TRACE_INSTRUMENT(qtcore) QCoreApplicationPrivate::init()
789 Q_TRACE_SCOPE(QCoreApplicationPrivate_init);
791#if defined(Q_OS_MACOS)
792 QMacAutoReleasePool pool;
795 Q_Q(QCoreApplication);
797#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
798 initDebuggingConsole();
803 Q_ASSERT_X(!QCoreApplication::self,
"QCoreApplication",
"there should be only one application object");
804#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
805# ifdef __cpp_lib_atomic_ref
806 std::atomic_ref(QCoreApplication::self).store(q, std::memory_order_relaxed);
808 QCoreApplication::self = q;
810 g_self.storeRelaxed(q);
812 QCoreApplication::self.storeRelaxed(q);
817 emscripten::val hardwareConcurrency = emscripten::val::global(
"navigator")[
"hardwareConcurrency"];
818 if (hardwareConcurrency.isUndefined())
819 QThreadPrivate::idealThreadCount = 2;
821 QThreadPrivate::idealThreadCount = hardwareConcurrency.as<
int>();
826 if (!coreappdata()->applicationNameSet)
827 coreappdata()->application = appName();
829 if (!coreappdata()->applicationVersionSet)
830 coreappdata()->applicationVersion = appVersion();
832#if (defined(Q_OS_ANDROID) || defined(Q_OS_HARMONY)) && !defined(QT_BOOTSTRAPPED)
837 QLoggingRegistry::instance()->initializeRules();
840#if QT_CONFIG(library)
844 if (coreappdata->libPathsInitialized()) {
845 const QStringList appPaths = std::exchange(coreappdata->app_libpaths, {});
846 Q_ASSERT(!coreappdata->libPathsInitialized());
848 if (coreappdata->libPathsManuallySet()) {
849 const QStringList manualPaths = std::exchange(coreappdata->manual_libpaths, {});
850 Q_ASSERT(!coreappdata->libPathsManuallySet());
856 QStringList newPaths(q->libraryPaths());
857 for (qsizetype i = manualPaths.size(), j = appPaths.size(); i > 0 || j > 0; qt_noop()) {
859 newPaths.prepend(manualPaths[--i]);
860 }
else if (--i < 0) {
861 newPaths.removeAll(appPaths[j]);
862 }
else if (manualPaths[i] != appPaths[j]) {
863 newPaths.removeAll(appPaths[j]);
867 coreappdata->manual_libpaths.swap(newPaths);
874 Q_ASSERT(!eventDispatcher);
875 auto thisThreadData = threadData.loadRelaxed();
876 eventDispatcher = thisThreadData->eventDispatcher.loadRelaxed();
879 if (!eventDispatcher)
880 createEventDispatcher();
881 Q_ASSERT(eventDispatcher);
883 if (!eventDispatcher->parent()) {
884 eventDispatcher->moveToThread(thisThreadData->thread.loadAcquire());
885 eventDispatcher->setParent(q);
888 thisThreadData->eventDispatcher = eventDispatcher;
889 eventDispatcherReady();
892 processCommandLineArguments();
894 qt_call_pre_routines();
895 QT_MANGLE_NAMESPACE(qt_startup_hook)();
896#ifndef QT_BOOTSTRAPPED
897 QtPrivate::initBindingStatusThreadId();
898 if (Q_UNLIKELY(qtHookData[QHooks::Startup]))
899 reinterpret_cast<QHooks::StartupCallback>(qtHookData[QHooks::Startup])();
903 is_app_running =
true;
908
909
910QCoreApplication::~QCoreApplication()
912 preRoutinesCalled =
false;
914 qt_call_post_routines();
917 QCoreApplicationPrivate::is_app_closing =
true;
918 QCoreApplicationPrivate::is_app_running =
false;
921#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
922# ifdef __cpp_lib_atomic_ref
923 std::atomic_ref(self).store(
nullptr, std::memory_order_relaxed);
927 g_self.storeRelaxed(
nullptr);
929 self.storeRelaxed(
nullptr);
934 QThreadPoolPrivate::destroyGlobalThreadPool();
938 QCoreApplicationPrivate::destroyEventDispatcher();
941#if QT_CONFIG(library)
942 if (coreappdata.exists()) {
944 coreappdata->app_libpaths = QStringList();
945 coreappdata->manual_libpaths = QStringList();
946 Q_ASSERT(!coreappdata->libPathsManuallySet());
947 Q_ASSERT(!coreappdata->libPathsInitialized());
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975void QCoreApplication::setSetuidAllowed(
bool allow)
977 QCoreApplicationPrivate::setuidAllowed = allow;
981
982
983
984
985
986
987
988bool QCoreApplication::isSetuidAllowed()
990 return QCoreApplicationPrivate::setuidAllowed;
994
995
996
997
998
999
1000
1001
1002
1003void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute,
bool on)
1007 static_assert(Qt::AA_AttributeCount <=
sizeof(QCoreApplicationPrivate::attribs) * CHAR_BIT);
1010 QCoreApplicationPrivate::attribs |= 1 << attribute;
1012 QCoreApplicationPrivate::attribs &= ~(1 << attribute);
1013#if defined(QT_NO_QOBJECT)
1014 if (Q_UNLIKELY(qApp)) {
1016 if (Q_UNLIKELY(QCoreApplicationPrivate::is_app_running)) {
1018 switch (attribute) {
1019 case Qt::AA_PluginApplication:
1020 case Qt::AA_UseDesktopOpenGL:
1021 case Qt::AA_UseOpenGLES:
1022 case Qt::AA_UseSoftwareOpenGL:
1023#ifdef QT_BOOTSTRAPPED
1024 qWarning(
"Attribute %d must be set before QCoreApplication is created.",
1027 qWarning(
"Attribute Qt::%s must be set before QCoreApplication is created.",
1028 QMetaEnum::fromType<Qt::ApplicationAttribute>().valueToKey(attribute));
1038
1039
1040
1041
1042
1043bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute)
1045 return QCoreApplicationPrivate::testAttribute(attribute);
1048#ifndef QT_NO_QOBJECT
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1069bool QCoreApplication::isQuitLockEnabled()
1071 return quitLockEnabled;
1074static bool doNotify(QObject *, QEvent *);
1076void QCoreApplication::setQuitLockEnabled(
bool enabled)
1078 quitLockEnabled = enabled;
1082
1083
1084
1085
1086
1087
1088bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
1094 QObjectPrivate *d = receiver->d_func();
1095 QThreadData *threadData = d->threadData.loadAcquire();
1096 bool selfRequired = threadData->requiresCoreApplication;
1097 if (selfRequired && !
qApp)
1102 bool result =
false;
1103 void *cbdata[] = { receiver, event, &result };
1104 if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) {
1108 QScopedScopeLevelCounter scopeLevelCounter(threadData);
1110 return doNotify(receiver, event);
1112 if (QCoreApplicationPrivate::is_app_closing)
1115#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1116 if (!QThread::isMainThread())
1119 return qApp->notify(receiver, event);
1123
1124
1125
1126
1127
1128
1129bool QCoreApplication::forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent)
1131 if (event && originatingEvent)
1132 event->m_spont = originatingEvent->m_spont;
1134 return notifyInternal2(receiver, event);
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
1187
1188
1190bool QCoreApplication::notify(QObject *receiver, QEvent *event)
1195#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1196 Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed()
1197 == QCoreApplicationPrivate::mainThread());
1201 if (QCoreApplicationPrivate::is_app_closing)
1203 return doNotify(receiver, event);
1206static bool doNotify(QObject *receiver, QEvent *event)
1211 if (receiver ==
nullptr) {
1212 qWarning(
"QCoreApplication::notify: Unexpected null receiver");
1216 QCoreApplicationPrivate::checkReceiverThread(receiver,
"QCoreApplication::notify");
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);
1717 const auto pending = QSpan(std::as_const(*postedEvents)).subspan(postedEvents->startOffset);
1720 if (event->type() == QEvent::Timer) {
1721 const int timerId =
static_cast<QTimerEvent *>(event)->timerId();
1722 auto it = pending.begin();
1723 const auto end = pending.end();
1725 if (it->event && it->event->type() == QEvent::Timer && it->receiver == receiver) {
1726 if (
static_cast<QTimerEvent *>(it->event)->timerId() == timerId) {
1736 if (event->type() == QEvent::Quit) {
1737 for (
const QPostEvent &cur : pending) {
1738 if (cur.receiver != receiver
1739 || cur.event ==
nullptr
1740 || cur.event->type() != event->type())
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768void QCoreApplication::sendPostedEvents(QObject *receiver,
int event_type)
1773 QThreadData *data = QThreadData::current();
1775 QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data);
1778void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver,
int event_type,
1781 if (event_type == -1) {
1786 if (receiver && receiver->d_func()->threadData.loadRelaxed() != data) {
1787 qWarning(
"QCoreApplication::sendPostedEvents: Cannot send "
1788 "posted events for objects in another thread");
1792 ++data->postEventList.recursion;
1794 auto locker = qt_unique_lock(data->postEventList.mutex);
1799 data->canWait = (data->postEventList.size() == 0);
1801 if (data->postEventList.size() == 0
1802 || (receiver && !receiver->d_func()->postedEvents.loadAcquire())) {
1803 --data->postEventList.recursion;
1807 data->canWait =
true;
1811 qsizetype startOffset = data->postEventList.startOffset;
1812 qsizetype &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset;
1813 data->postEventList.insertionOffset = data->postEventList.size();
1817 Q_DISABLE_COPY_MOVE(CleanUp)
1822 bool exceptionCaught;
1824 inline CleanUp(QObject *receiver,
int event_type, QThreadData *data) :
1825 receiver(receiver), event_type(event_type), data(data), exceptionCaught(
true)
1829 if (exceptionCaught) {
1831 data->canWait =
false;
1834 --data->postEventList.recursion;
1835 if (!data->postEventList.recursion && !data->canWait && data->hasEventDispatcher())
1836 data->eventDispatcher.loadRelaxed()->wakeUp();
1840 if (!event_type && !receiver && data->postEventList.startOffset >= 0) {
1841 const QPostEventList::iterator it = data->postEventList.begin();
1842 data->postEventList.erase(it, it + data->postEventList.startOffset);
1843 data->postEventList.insertionOffset -= data->postEventList.startOffset;
1844 Q_ASSERT(data->postEventList.insertionOffset >= 0);
1845 data->postEventList.startOffset = 0;
1849 CleanUp cleanup(receiver, event_type, data);
1851 while (i < data->postEventList.size()) {
1853 if (i >= data->postEventList.insertionOffset)
1856 const QPostEvent &pe = data->postEventList.at(i);
1861 if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) {
1862 data->canWait =
false;
1866 if (pe.event->type() == QEvent::DeferredDelete) {
1873 const int eventLoopLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->loopLevel();
1874 const int eventScopeLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->scopeLevel();
1876 const bool postedBeforeOutermostLoop = eventLoopLevel == 0;
1877 const bool allowDeferredDelete =
1878 (eventLoopLevel + eventScopeLevel > data->loopLevel + data->scopeLevel
1879 || (postedBeforeOutermostLoop && data->loopLevel > 0)
1880 || (event_type == QEvent::DeferredDelete
1881 && eventLoopLevel + eventScopeLevel == data->loopLevel + data->scopeLevel));
1882 if (!allowDeferredDelete) {
1884 if (!event_type && !receiver) {
1889 QPostEvent pe_copy = pe;
1893 const_cast<QPostEvent &>(pe).event =
nullptr;
1896 data->postEventList.addEvent(pe_copy);
1904 pe.event->m_posted =
false;
1905 QEvent *e = pe.event;
1906 QObject * r = pe.receiver;
1909 const auto previous = r->d_func()->postedEvents.fetchAndSubAcquire(1);
1910 Q_ASSERT(previous > 0);
1914 const_cast<QPostEvent &>(pe).event =
nullptr;
1917 const auto relocker = qScopeGuard([&locker] { locker.lock(); });
1919 const std::unique_ptr<QEvent> event_deleter(e);
1922 QCoreApplication::sendEvent(r, e);
1929 cleanup.exceptionCaught =
false;
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1951void QCoreApplication::removePostedEvents(QObject *receiver,
int eventType)
1953 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1954 QThreadData *data = locker.threadData;
1960 if (receiver && !receiver->d_func()->postedEvents.loadAcquire())
1965 QVarLengthArray<QEvent*> events;
1966 qsizetype n = data->postEventList.size();
1969 for (qsizetype i = 0; i < n; ++i) {
1970 const QPostEvent &pe = data->postEventList.at(i);
1972 if ((!receiver || pe.receiver == receiver)
1973 && (pe.event && (eventType == 0 || pe.event->type() == eventType))) {
1974 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
1975 pe.event->m_posted =
false;
1976 events.append(pe.event);
1977 const_cast<QPostEvent &>(pe).event =
nullptr;
1978 }
else if (!data->postEventList.recursion) {
1980 qSwap(data->postEventList[i], data->postEventList[j]);
1986 if (receiver && eventType == 0) {
1987 Q_ASSERT(!receiver->d_func()->postedEvents.loadRelaxed());
1991 if (!data->postEventList.recursion) {
1993 data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end());
2001
2002
2003
2004
2005
2006
2007
2008
2009
2011void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
2013 if (!event || !event->m_posted)
2016 QThreadData *data = QThreadData::current();
2018 const auto locker = qt_scoped_lock(data->postEventList.mutex);
2020 if (data->postEventList.size() == 0) {
2021#if defined(QT_DEBUG)
2022 qDebug(
"QCoreApplication::removePostedEvent: Internal error: %p %d is posted",
2023 (
void*)event, event->type());
2028 for (
const QPostEvent &pe : std::as_const(data->postEventList)) {
2029 if (pe.event == event) {
2031 qWarning(
"QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s",
2033 pe.receiver->metaObject()->className(),
2034 pe.receiver->objectName().toLocal8Bit().data());
2036 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
2037 pe.event->m_posted =
false;
2039 const_cast<QPostEvent &>(pe).event =
nullptr;
2046
2047
2048bool QCoreApplication::event(QEvent *e)
2050 Q_D(QCoreApplication);
2051 if (e->type() == QEvent::Quit) {
2052 if (d->handlingQuitEvent) {
2057 QScopedValueRollback rollback(d->handlingQuitEvent,
true);
2058 e->setAccepted(d->handleQuitEvent());
2061 return QObject::event(e);
2064void QCoreApplicationPrivate::ref()
2069void QCoreApplicationPrivate::deref()
2071 quitLockRef.deref();
2073 if (quitLockEnabled && canQuitAutomatically())
2074 quitAutomatically();
2077bool QCoreApplicationPrivate::canQuitAutomatically()
2088 if (quitLockRef.loadRelaxed())
2094void QCoreApplicationPrivate::quitAutomatically()
2096 Q_Q(QCoreApplication);
2104 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141void QCoreApplication::quit()
2146 if (!self->d_func()->in_exec)
2149 if (QThread::isMainThread() && self->d_func()->handlingQuitEvent)
2152 self->d_func()->quit();
2155void QCoreApplicationPrivate::quit()
2157 Q_Q(QCoreApplication);
2159 if (QThread::isMainThread()) {
2160 QEvent quitEvent(QEvent::Quit);
2161 QCoreApplication::sendEvent(q, &quitEvent);
2163 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179bool QCoreApplicationPrivate::handleQuitEvent()
2181 QCoreApplication::exit(0);
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2208#ifndef QT_NO_TRANSLATION
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2240bool QCoreApplication::installTranslator(QTranslator *translationFile)
2242 if (!translationFile)
2245 if (!QCoreApplicationPrivate::checkInstance(
"installTranslator"))
2248 QCoreApplicationPrivate *d = self->d_func();
2250 QWriteLocker locker(&d->translateMutex);
2251 d->translators.prepend(translationFile);
2254 if (translationFile->isEmpty())
2257#ifndef QT_NO_QOBJECT
2258 QEvent ev(QEvent::LanguageChange);
2259 QCoreApplication::sendEvent(self, &ev);
2266
2267
2268
2269
2270
2271
2272
2273
2275bool QCoreApplication::removeTranslator(QTranslator *translationFile)
2277 if (!translationFile)
2279 if (!QCoreApplicationPrivate::checkInstance(
"removeTranslator"))
2281 QCoreApplicationPrivate *d = self->d_func();
2282 QWriteLocker locker(&d->translateMutex);
2283 if (d->translators.removeAll(translationFile)) {
2284#ifndef QT_NO_QOBJECT
2286 if (!self->closingDown()) {
2287 QEvent ev(QEvent::LanguageChange);
2288 QCoreApplication::sendEvent(self, &ev);
2299 qsizetype percentPos = 0;
2301 while ((percentPos = result->indexOf(u'%', percentPos + len)) != -1) {
2303 if (percentPos + len == result->size())
2306 if (result->at(percentPos + len) == u'L') {
2308 if (percentPos + len == result->size())
2314 if (result->at(percentPos + len) == u'n') {
2317 result->replace(percentPos, len, fmt);
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2358 const char *disambiguation,
int n)
2366 QCoreApplicationPrivate *d = self->d_func();
2367 QReadLocker locker(&d->translateMutex);
2368 if (!d->translators.isEmpty()) {
2369 QList<QTranslator*>::ConstIterator it;
2370 QTranslator *translationFile;
2371 for (it = d->translators.constBegin(); it != d->translators.constEnd(); ++it) {
2372 translationFile = *it;
2373 result = translationFile->translate(context, sourceText, disambiguation, n);
2374 if (!result.isNull())
2380 if (result.isNull())
2381 result = QString::fromUtf8(sourceText);
2383 replacePercentN(&result, n);
2390 return QCoreApplication::translate(
nullptr, id,
nullptr, n);
2393bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
2395 if (!QCoreApplication::self)
2397 QCoreApplicationPrivate *d = QCoreApplication::self->d_func();
2398 QReadLocker locker(&d->translateMutex);
2399 return d->translators.contains(translator);
2404QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2405 const char *disambiguation,
int n)
2408 Q_UNUSED(disambiguation);
2409 QString ret = QString::fromUtf8(sourceText);
2411 ret.replace(
"%n"_L1, QString::number(n));
2417#ifndef QT_BOOTSTRAPPED
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441QString QCoreApplication::applicationDirPath()
2444 qWarning(
"QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
2448 QFileSystemEntry appFilePath(applicationFilePath(), QFileSystemEntry::FromInternalPath{});
2449 return appFilePath.isEmpty() ? QString() : appFilePath.path();
2452#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN)
2456# if defined(Q_OS_ANDROID) || defined(Q_OS_HARMONY)
2459# elif defined(Q_OS_LINUX)
2461 return QFile::decodeName(qt_readlink(
"/proc/self/exe"));
2462# elif defined(AT_EXECPATH)
2464 char execfn[PATH_MAX];
2465 if (elf_aux_info(AT_EXECPATH, execfn,
sizeof(execfn)) != 0)
2468 qsizetype len = qstrlen(execfn);
2469 return QFile::decodeName(QByteArray::fromRawData(execfn, len));
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492QString QCoreApplication::applicationFilePath()
2495 qWarning(
"QCoreApplication::applicationFilePath: Please instantiate the QApplication object first");
2499 QCoreApplicationPrivate *d = self->d_func();
2502 static QByteArray procName = QByteArray(d->argv[0]);
2503 if (procName != QByteArrayView(d->argv[0])) {
2505 d->cachedApplicationFilePath = QString();
2506 procName.assign(d->argv[0]);
2510 if (!d->cachedApplicationFilePath.isNull())
2511 return d->cachedApplicationFilePath;
2513 QString absPath = qAppFileName();
2514 if (Q_LIKELY(!absPath.isEmpty())) {
2516 return d->cachedApplicationFilePath = std::move(absPath);
2519 if (
const QStringList args = arguments(); !args.isEmpty()) {
2520 const QString &argv0 = args[0];
2522 if (!argv0.isEmpty() && argv0.at(0) == u'/') {
2524
2525
2526
2528 }
else if (argv0.contains(u'/')) {
2530
2531
2532
2533 absPath = QDir::current().absoluteFilePath(argv0);
2536
2537
2538
2539 absPath = QStandardPaths::findExecutable(argv0);
2543 absPath = QFileInfo(absPath).canonicalFilePath();
2544 if (!absPath.isEmpty()) {
2545 return d->cachedApplicationFilePath = std::move(absPath);
2552
2553
2554
2555
2556qint64 QCoreApplication::applicationPid()
noexcept
2558#if defined(Q_OS_WIN)
2559 return GetCurrentProcessId();
2560#elif defined(Q_OS_VXWORKS)
2561 return (pid_t) taskIdCurrent;
2568static QStringList winCmdArgs()
2577 if (
wchar_t **argv = CommandLineToArgvW(GetCommandLine(), &size)) {
2578 result.reserve(size);
2579 wchar_t **argvEnd = argv + size;
2580 for (
wchar_t **a = argv; a < argvEnd; ++a)
2581 result.append(QString::fromWCharArray(*a));
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2620QStringList QCoreApplication::arguments()
2625 qWarning(
"QCoreApplication::arguments: Please instantiate the QApplication object first");
2629 const QCoreApplicationPrivate *d = self->d_func();
2631 const int argc = d->argc;
2632 char **
const argv = d->argv;
2635#if defined(Q_OS_WIN)
2636 const bool argsModifiedByUser = d->origArgv ==
nullptr;
2637 if (!argsModifiedByUser) {
2638 QStringList commandLineArguments = winCmdArgs();
2643 if (argc != d->origArgc) {
2650 for (
int i = 0; i < d->origArgc; ++i) {
2651 if (!contains(argc, argv, d->origArgv[i]))
2652 commandLineArguments.removeAll(QString::fromLocal8Bit(d->origArgv[i]));
2656 return commandLineArguments;
2660 for (
int a = 0; a < argc; ++a)
2661 list << QString::fromLocal8Bit(argv[a]);
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2683
2684
2685
2686
2687
2688
2689
2690void QCoreApplication::setOrganizationName(
const QString &orgName)
2692 if (coreappdata()->orgName == orgName)
2694 coreappdata()->orgName = orgName;
2695#ifndef QT_NO_QOBJECT
2696 if (QCoreApplication::self)
2697 emit QCoreApplication::self->organizationNameChanged();
2701QString QCoreApplication::organizationName()
2703 return coreappdata()->orgName;
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2722
2723
2724
2725
2726
2727void QCoreApplication::setOrganizationDomain(
const QString &orgDomain)
2729 if (coreappdata()->orgDomain == orgDomain)
2731 coreappdata()->orgDomain = orgDomain;
2732#ifndef QT_NO_QOBJECT
2733 if (QCoreApplication::self)
2734 emit QCoreApplication::self->organizationDomainChanged();
2738QString QCoreApplication::organizationDomain()
2740 return coreappdata()->orgDomain;
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2760
2761
2762
2763
2764
2765void QCoreApplication::setApplicationName(
const QString &application)
2767 coreappdata()->applicationNameSet = !application.isEmpty();
2768 QString newAppName = application;
2769 if (newAppName.isEmpty() && QCoreApplication::self)
2770 newAppName = QCoreApplication::self->d_func()->appName();
2771 if (coreappdata()->application == newAppName)
2773 coreappdata()->application = newAppName;
2774#ifndef QT_NO_QOBJECT
2775 if (QCoreApplication::self)
2776 emit QCoreApplication::self->applicationNameChanged();
2780QString QCoreApplication::applicationName()
2782 return coreappdata() ? coreappdata()->application : QString();
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2816
2817
2818
2819
2820
2821void QCoreApplication::setApplicationVersion(
const QString &version)
2823 coreappdata()->applicationVersionSet = !version.isEmpty();
2824 QString newVersion = version;
2825 if (newVersion.isEmpty() && QCoreApplication::self)
2826 newVersion = QCoreApplication::self->d_func()->appVersion();
2827 if (coreappdata()->applicationVersion == newVersion)
2829 coreappdata()->applicationVersion = newVersion;
2830#ifndef QT_NO_QOBJECT
2831 if (QCoreApplication::self)
2832 emit QCoreApplication::self->applicationVersionChanged();
2836QString QCoreApplication::applicationVersion()
2838 return coreappdata() ? coreappdata()->applicationVersion : QString();
2841#if QT_CONFIG(permissions) || defined(Q_QDOC)
2844
2845
2846
2847
2848
2849
2850
2851
2852Qt::PermissionStatus QCoreApplication::checkPermission(
const QPermission &permission)
2854 return QPermissions::Private::checkPermission(permission);
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
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2924
2925
2926
2927
2928
2929
2930
2931void QCoreApplication::requestPermissionImpl(
const QPermission &requestedPermission,
2932 QtPrivate::QSlotObjectBase *slotObjRaw,
const QObject *context)
2934 QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw};
2937 if (!QThread::isMainThread()) {
2938 qCWarning(lcPermissions,
"Permissions can only be requested from the GUI (main) thread");
2942 class PermissionReceiver :
public QObject
2945 explicit PermissionReceiver(QtPrivate::SlotObjUniquePtr &&slotObject,
const QObject *context)
2946 : slotObject(std::move(slotObject)), context(context ? context :
this)
2948 Q_ASSERT(
this->context);
2949 moveToThread(
this->context->thread());
2952 void finalizePermissionRequest(
const QPermission &permission)
2954 Q_ASSERT(slotObject);
2957 void *args[] = {
nullptr,
const_cast<QPermission *>(&permission) };
2958 slotObject->call(
const_cast<QObject *>(context.data()), args);
2963 QtPrivate::SlotObjUniquePtr slotObject;
2964 QPointer<
const QObject> context;
2968 auto receiver = std::make_shared<PermissionReceiver>(std::move(slotObj), context);
2970 QPermissions::Private::requestPermission(requestedPermission,
2971 [=, receiver = std::move(receiver)](Qt::PermissionStatus status)
mutable {
2972 if (status == Qt::PermissionStatus::Undetermined) {
2973 Q_ASSERT_X(
false,
"QPermission",
2974 "Internal error: requestPermission() should never return Undetermined");
2975 status = Qt::PermissionStatus::Denied;
2978 if (QCoreApplication::self) {
2979 QPermission permission = requestedPermission;
2980 permission.m_status = status;
2981 auto receiverObject = receiver.get();
2982 QMetaObject::invokeMethod(receiverObject,
2983 [receiver = std::move(receiver), permission] {
2984 receiver->finalizePermissionRequest(permission);
2985 }, Qt::QueuedConnection);
2992#if QT_CONFIG(library)
2993static QStringList libraryPathsLocked();
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028QStringList QCoreApplication::libraryPaths()
3030 QMutexLocker locker(&coreappdata->libraryPathMutex);
3031 return libraryPathsLocked();
3035
3036
3037static QStringList libraryPathsLocked()
3039 QCoreApplicationData *d = coreappdata;
3040 if (d->libPathsManuallySet())
3041 return d->manual_libpaths;
3043 QStringList *app_libpaths = &d->app_libpaths;
3044 if (!d->libPathsInitialized()) {
3046 auto setPathsFromEnv = [&](QString libPathEnv) {
3047 if (!libPathEnv.isEmpty()) {
3048 QStringList paths = libPathEnv.split(QDir::listSeparator(), Qt::SkipEmptyParts);
3049 for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
3050 QString canonicalPath = QDir(*it).canonicalPath();
3051 if (!canonicalPath.isEmpty()
3052 && !app_libpaths->contains(canonicalPath)) {
3053 app_libpaths->append(canonicalPath);
3058 setPathsFromEnv(qEnvironmentVariable(
"QT_PLUGIN_PATH"));
3060 const auto pluginPaths = QLibraryInfo::paths(QLibraryInfo::PluginsPath);
3061 for (
auto path : pluginPaths) {
3062 if (QFile::exists(path)) {
3064 path = QDir(path).canonicalPath();
3065 if (!app_libpaths->contains(path))
3066 app_libpaths->append(path);
3073 QString app_location = QCoreApplication::applicationFilePath();
3074 app_location.truncate(app_location.lastIndexOf(u'/'));
3075 app_location = QDir(app_location).canonicalPath();
3076 if (QFile::exists(app_location) && !app_libpaths->contains(app_location))
3077 app_libpaths->append(app_location);
3079 if (app_libpaths->isEmpty())
3080 app_libpaths->reserve(1);
3081 Q_ASSERT(d->libPathsInitialized());
3083 return *app_libpaths;
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103void QCoreApplication::setLibraryPaths(
const QStringList &paths)
3105 QCoreApplicationData *d = coreappdata;
3106 QMutexLocker locker(&d->libraryPathMutex);
3111 if (!d->libPathsInitialized())
3112 libraryPathsLocked();
3114 d->manual_libpaths = paths;
3115 if (d->manual_libpaths.isEmpty())
3116 d->manual_libpaths.reserve(1);
3117 Q_ASSERT(d->libPathsManuallySet());
3120 QFactoryLoader::refreshAll();
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142void QCoreApplication::addLibraryPath(
const QString &path)
3147 QString canonicalPath = QDir(path).canonicalPath();
3148 if (canonicalPath.isEmpty())
3151 QCoreApplicationData *d = coreappdata;
3152 QMutexLocker locker(&d->libraryPathMutex);
3154 QStringList *libpaths = &d->manual_libpaths;
3155 if (d->libPathsManuallySet()) {
3156 if (d->manual_libpaths.contains(canonicalPath))
3160 libraryPathsLocked();
3161 QStringList *app_libpaths = &d->app_libpaths;
3162 if (app_libpaths->contains(canonicalPath))
3165 *libpaths = *app_libpaths;
3168 libpaths->prepend(canonicalPath);
3169 Q_ASSERT(d->libPathsManuallySet());
3171 QFactoryLoader::refreshAll();
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186void QCoreApplication::removeLibraryPath(
const QString &path)
3191 QString canonicalPath = QDir(path).canonicalPath();
3192 if (canonicalPath.isEmpty())
3195 QCoreApplicationData *d = coreappdata;
3196 QMutexLocker locker(&d->libraryPathMutex);
3198 QStringList *libpaths = &d->manual_libpaths;
3199 if (d->libPathsManuallySet()) {
3200 if (libpaths->removeAll(canonicalPath) == 0)
3204 libraryPathsLocked();
3205 QStringList *app_libpaths = &d->app_libpaths;
3206 if (!app_libpaths->contains(canonicalPath))
3209 *libpaths = *app_libpaths;
3210 libpaths->removeAll(canonicalPath);
3211 Q_ASSERT(d->libPathsManuallySet());
3215 QFactoryLoader::refreshAll();
3220#ifndef QT_NO_QOBJECT
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
3253 if (QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
3254 qWarning(
"Native event filters are not applied when the Qt::AA_PluginApplication attribute is set");
3258 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3259 if (!filterObj || !eventDispatcher)
3261 eventDispatcher->installNativeEventFilter(filterObj);
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277void QCoreApplication::removeNativeEventFilter(QAbstractNativeEventFilter *filterObject)
3279 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3280 if (!filterObject || !eventDispatcher)
3282 eventDispatcher->removeNativeEventFilter(filterObject);
3286
3287
3288
3289QAbstractEventDispatcher *QCoreApplication::eventDispatcher()
3291 if (QCoreApplicationPrivate::theMainThread.loadAcquire())
3292 return QCoreApplicationPrivate::theMainThread.loadRelaxed()->eventDispatcher();
3297
3298
3299
3300
3301
3302void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
3304 QThread *mainThread = QCoreApplicationPrivate::theMainThread.loadAcquire();
3306 mainThread = QThread::currentThread();
3307 mainThread->setEventDispatcher(eventDispatcher);
3310void QCoreApplicationPrivate::destroyEventDispatcher()
3313 QThreadData *data = QThreadData::current();
3314 Q_ASSERT(data->eventDispatcher.loadAcquire() == eventDispatcher);
3318 QAbstractEventDispatcher *dispatcher = eventDispatcher;
3319 eventDispatcher =
nullptr;
3320 data->eventDispatcher.storeRelaxed(
nullptr);
3322 dispatcher->closingDown();
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
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3437void *QCoreApplication::resolveInterface(
const char *name,
int revision)
const
3439#if defined(Q_OS_ANDROID)
3444 using namespace QNativeInterface;
3445 struct AndroidApplication :
public QAndroidApplication {};
3446 static AndroidApplication androidApplication;
3447 QT_NATIVE_INTERFACE_RETURN_IF(QAndroidApplication, &androidApplication);
3449#if defined(Q_OS_HARMONY) && !defined(QT_BOOTSTRAPPED)
3450 using namespace QNativeInterface;
3451 struct HarmonyApplication :
public QHarmonyApplication {};
3452 static HarmonyApplication harmonyApplication;
3453 QT_NATIVE_INTERFACE_RETURN_IF(QHarmonyApplication, &harmonyApplication);
3455 Q_UNUSED(name); Q_UNUSED(revision);
3461#ifndef QT_NO_QOBJECT
3462#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(*) QtStartUpFunction()
void(*) QtCleanUpFunction()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QString applicationVersion
QCoreApplicationData() noexcept
bool applicationVersionSet