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>
79#if defined(Q_OS_HARMONY) && !defined(QT_BOOTSTRAPPED)
80#include <QtCore/private/qcore_ohos_p.h>
81#include <QtCore/private/qnapi_p.h>
82#include <QtCore/private/qohosmemoizingjsthreadfetcher_p.h>
86# include "qcore_mac_p.h"
93# ifndef Q_OS_INTEGRITY
97# include <sys/types.h>
99# include "qcore_unix_p.h"
103# include <sys/auxv.h>
111#include <emscripten/val.h>
114#ifdef QT_BOOTSTRAPPED
115#include <private/qtrace_p.h>
117#include <qtcore_tracepoints_p.h>
125# include <qt_windows.h>
130using namespace Qt::StringLiterals;
133 "#include <qcoreevent.h>"
145#if (defined(Q_OS_WIN) || defined(Q_OS_DARWIN)) && !defined(QT_BOOTSTRAPPED)
146extern QString qAppFileName();
149Q_CONSTINIT
bool QCoreApplicationPrivate::setuidAllowed =
false;
151#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
152# warning "Audit remaining direct usages of this variable for memory ordering semantics"
153Q_CONSTINIT QBasicAtomicPointer<QCoreApplication> QCoreApplication::self =
nullptr;
156Q_CONSTINIT
static QBasicAtomicPointer<QCoreApplication> g_self =
nullptr;
158# define qApp g_self.loadRelaxed()
161
162
163
164
165
166
167
168
169
170bool QCoreApplication::instanceExists()
noexcept
172 return qApp !=
nullptr;
176#if !defined(Q_OS_WIN) || defined(QT_BOOTSTRAPPED)
178QString QCoreApplicationPrivate::infoDictionaryStringProperty(
const QString &propertyName)
181 QCFString cfPropertyName = propertyName.toCFString();
182 CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(),
185 bundleName = QString::fromCFString(
static_cast<CFStringRef>(string));
189QString QCoreApplicationPrivate::appName()
const
191 QString applicationName;
193 applicationName = infoDictionaryStringProperty(QStringLiteral(
"CFBundleName"));
195 if (applicationName.isEmpty() && argv[0]) {
196 char *p = strrchr(argv[0],
'/');
197 applicationName = QString::fromLocal8Bit(p ? p + 1 : argv[0]);
200 return applicationName;
203#if defined(Q_OS_HARMONY) && !defined(QT_BOOTSTRAPPED)
204static QOhosMemoizingJsThreadFetcher<QString> bundleVersionNameFetcher(
205 [](QOhosJsState &jsState) -> std::optional<QString> {
206 const auto flag = jsState.eval<QNapi::Number>(
207 "@ohos.bundle.bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION");
208 return QString::fromStdString(
209 jsState.eval<QNapi::String>(
210 "@ohos.bundle.bundleManager.getBundleInfoForSelfSync(*).versionName",
213 "BundleInfo.versionName");
216QString QCoreApplicationPrivate::appVersion()
const
218 QString applicationVersion;
219#if defined(Q_OS_DARWIN)
220 applicationVersion = infoDictionaryStringProperty(QStringLiteral(
"CFBundleVersion"));
221#elif defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
222 QJniObject context(QNativeInterface::QAndroidApplication::context());
223 if (context.isValid()) {
224 QJniObject pm = context.callObjectMethod(
225 "getPackageManager",
"()Landroid/content/pm/PackageManager;");
226 QJniObject pn = context.callObjectMethod<jstring>(
"getPackageName");
227 if (pm.isValid() && pn.isValid()) {
228 QJniObject packageInfo = pm.callObjectMethod(
229 "getPackageInfo",
"(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;",
231 if (packageInfo.isValid()) {
232 QJniObject versionName = packageInfo.getObjectField(
233 "versionName",
"Ljava/lang/String;");
234 if (versionName.isValid())
235 return versionName.toString();
239#elif defined(Q_OS_HARMONY) && !defined(QT_BOOTSTRAPPED)
240 applicationVersion = bundleVersionNameFetcher.optValue().value_or(QString());
242 return applicationVersion;
246bool QCoreApplicationPrivate::checkInstance(
const char *function)
248 bool b = (
qApp !=
nullptr);
250 qWarning(
"QApplication::%s: Please instantiate the QApplication object first", function);
254#if QT_CONFIG(commandlineparser)
255void QCoreApplicationPrivate::addQtOptions(QList<QCommandLineOption> *options)
257 options->append(QCommandLineOption(QStringLiteral(
"qmljsdebugger"),
258 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."),
259 QStringLiteral(
"value")));
263void QCoreApplicationPrivate::processCommandLineArguments()
265 int j = argc ? 1 : 0;
266 for (
int i = 1; i < argc; ++i) {
269 if (*argv[i] !=
'-') {
273 const char *arg = argv[i];
276 if (strncmp(arg,
"-qmljsdebugger=", 15) == 0) {
277 qmljs_debug_arguments = QString::fromLocal8Bit(arg + 15);
278 }
else if (strcmp(arg,
"-qmljsdebugger") == 0 && i < argc - 1) {
280 qmljs_debug_arguments = QString::fromLocal8Bit(argv[i]);
305Q_GLOBAL_STATIC(QVFuncList, postRList)
306Q_CONSTINIT
static QBasicMutex globalRoutinesMutex;
310
311
312
313
314
328 const auto locker = qt_scoped_lock(globalRoutinesMutex);
337 const auto locker = qt_scoped_lock(globalRoutinesMutex);
346 const auto locker = qt_scoped_lock(globalRoutinesMutex);
355 if (!preRList.exists())
359 const auto locker = qt_scoped_lock(globalRoutinesMutex);
366 for (QtStartUpFunction f : list)
370void Q_CORE_EXPORT qt_call_post_routines()
372 if (!postRList.exists())
379 const auto locker = qt_scoped_lock(globalRoutinesMutex);
380 qSwap(*postRList, list);
385 for (QtCleanUpFunction f : std::as_const(list))
394Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_running =
false;
396Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_closing =
false;
400 const QPostEventList &l = QThreadData::current()->postEventList;
401 return l.size() - l.startOffset;
408Q_CONSTINIT uint QCoreApplicationPrivate::attribs =
409 (1 << Qt::AA_SynthesizeMouseForUnhandledTouchEvents) |
410 (1 << Qt::AA_SynthesizeMouseForUnhandledTabletEvents);
425#if QT_CONFIG(library)
438Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata)
441Q_CONSTINIT
static bool quitLockEnabled =
true;
450static inline bool isArgvModified(
int argc,
char **argv)
452 if (__argc != argc || !__argv )
456 for (
int a = 0; a < argc; ++a) {
457 if (argv[a] != __argv[a] && strcmp(argv[a], __argv[a]))
463static inline bool contains(
int argc,
char **argv,
const char *needle)
465 for (
int a = 0; a < argc; ++a) {
466 if (!strcmp(argv[a], needle))
473QCoreApplicationPrivate::QCoreApplicationPrivate(
int &aargc,
char **aargv)
474 : argc(aargc), argv(aargv)
476 static const char *
const empty =
"";
477 if (argc == 0 || argv ==
nullptr) {
479 argv =
const_cast<
char **>(&empty);
482 if (!isArgvModified(argc, argv)) {
484 origArgv = q20::make_unique_for_overwrite<
char *[]>(argc);
485 std::copy(argv, argv + argc, origArgv.get());
490 QCoreApplicationPrivate::is_app_closing =
false;
492# if defined(Q_OS_UNIX)
493 if (Q_UNLIKELY(!setuidAllowed && (geteuid() != getuid())))
494 qFatal(
"FATAL: The application binary appears to be running setuid, this is a security hole.");
497 QThread *cur = QThread::currentThread();
498 if (cur != theMainThread.loadAcquire())
499 qWarning(
"WARNING: QApplication was not created in the main() thread.");
503QCoreApplicationPrivate::~QCoreApplicationPrivate()
508#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
509 cleanupDebuggingConsole();
515void QCoreApplicationPrivate::cleanupThreadData()
517 auto thisThreadData = threadData.loadRelaxed();
519 if (thisThreadData && !threadData_clean) {
521 QThreadStoragePrivate::finish(&thisThreadData->tls);
525 thisThreadData->clearEvents();
526 thisThreadData->quitNow =
false;
527 threadData_clean =
true;
531void QCoreApplicationPrivate::createEventDispatcher()
533 Q_Q(QCoreApplication);
534 QThreadData *data = QThreadData::current();
535 Q_ASSERT(!data->hasEventDispatcher());
536 eventDispatcher = data->createEventDispatcher();
537 eventDispatcher->setParent(q);
540void QCoreApplicationPrivate::eventDispatcherReady()
544Q_CONSTINIT QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
545Q_CONSTINIT QBasicAtomicPointer<
void> QCoreApplicationPrivate::theMainThreadId = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
546QThread *QCoreApplicationPrivate::mainThread()
548 Q_ASSERT(theMainThread.loadRelaxed() !=
nullptr);
549 return theMainThread.loadRelaxed();
556 if (!QCoreApplicationPrivate::checkInstance(
"qAppName"))
558 return QCoreApplication::instance()->d_func()->appName();
561void QCoreApplicationPrivate::initLocale()
563#if defined(QT_BOOTSTRAPPED)
565#elif defined(Q_OS_UNIX)
566 Q_CONSTINIT
static bool qt_locale_initialized =
false;
567 if (qt_locale_initialized)
569 qt_locale_initialized =
true;
574 setlocale(LC_ALL,
"");
578# if defined(Q_OS_INTEGRITY)
579 setlocale(LC_CTYPE,
"UTF-8");
580# elif defined(Q_OS_QNX)
583# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__
586# elif defined(Q_OS_VXWORKS)
590 const std::string oldEncoding = nl_langinfo(CODESET);
591 if (!Q_LIKELY(qstricmp(oldEncoding.data(),
"UTF-8") == 0
592 || qstricmp(oldEncoding.data(),
"utf8") == 0)) {
593 const QByteArray oldLocale = setlocale(LC_ALL,
nullptr);
594 QByteArray newLocale;
595 bool warnOnOverride =
true;
596# if defined(Q_OS_DARWIN)
600 warnOnOverride = qstrcmp(setlocale(LC_CTYPE,
nullptr),
"C") != 0
601 || getenv(
"LC_ALL") || getenv(
"LC_CTYPE") || getenv(
"LANG");
605 newLocale = setlocale(LC_CTYPE,
"UTF-8");
607 newLocale = setlocale(LC_CTYPE,
nullptr);
608 if (qsizetype dot = newLocale.indexOf(
'.'); dot != -1)
609 newLocale.truncate(dot);
610 if (qsizetype at = newLocale.indexOf(
'@'); at != -1)
611 newLocale.truncate(at);
612 newLocale +=
".UTF-8";
613 newLocale = setlocale(LC_CTYPE, newLocale);
616 if (newLocale.isEmpty())
617 newLocale = setlocale(LC_CTYPE,
"C.UTF-8");
618 if (newLocale.isEmpty())
619 newLocale = setlocale(LC_CTYPE,
"C.utf8");
622 if (newLocale.isEmpty()) {
624 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
625 "Qt depends on a UTF-8 locale, but has failed to switch to one.\n"
626 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
627 "for more information.", oldLocale.constData(), oldEncoding.data());
628 }
else if (warnOnOverride) {
630 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
631 "Qt depends on a UTF-8 locale, and has switched to \"%s\" instead.\n"
632 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
633 "for more information.",
634 oldLocale.constData(), oldEncoding.data(), newLocale.constData());
643
644
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
719
720
721
722
723
724
725
726
727
728
729
732
733
734QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
738 : QObject(p,
nullptr)
741 d_func()->q_ptr =
this;
747
748
749
750
751
752
753
754
755
756
757
758
759
760QCoreApplication::QCoreApplication(
int &argc,
char **argv
766 : d_ptr(
new QCoreApplicationPrivate(argc, argv))
768 : QObject(*
new QCoreApplicationPrivate(argc, argv))
771 d_func()->q_ptr =
this;
774 QCoreApplicationPrivate::eventDispatcher->startingUp();
779
780
781
782
783
785void Q_TRACE_INSTRUMENT(qtcore) QCoreApplicationPrivate::init()
787 Q_TRACE_SCOPE(QCoreApplicationPrivate_init);
789#if defined(Q_OS_MACOS)
790 QMacAutoReleasePool pool;
793 Q_Q(QCoreApplication);
795#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
796 initDebuggingConsole();
801 Q_ASSERT_X(!QCoreApplication::self,
"QCoreApplication",
"there should be only one application object");
802#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
803# ifdef __cpp_lib_atomic_ref
804 std::atomic_ref(QCoreApplication::self).store(q, std::memory_order_relaxed);
806 QCoreApplication::self = q;
808 g_self.storeRelaxed(q);
810 QCoreApplication::self.storeRelaxed(q);
815 emscripten::val hardwareConcurrency = emscripten::val::global(
"navigator")[
"hardwareConcurrency"];
816 if (hardwareConcurrency.isUndefined())
817 QThreadPrivate::idealThreadCount = 2;
819 QThreadPrivate::idealThreadCount = hardwareConcurrency.as<
int>();
824 if (!coreappdata()->applicationNameSet)
825 coreappdata()->application = appName();
827 if (!coreappdata()->applicationVersionSet)
828 coreappdata()->applicationVersion = appVersion();
830#if (defined(Q_OS_ANDROID) || defined(Q_OS_HARMONY)) && !defined(QT_BOOTSTRAPPED)
835 QLoggingRegistry::instance()->initializeRules();
838#if QT_CONFIG(library)
842 if (coreappdata->libPathsInitialized()) {
843 const QStringList appPaths = std::exchange(coreappdata->app_libpaths, {});
844 Q_ASSERT(!coreappdata->libPathsInitialized());
846 if (coreappdata->libPathsManuallySet()) {
847 const QStringList manualPaths = std::exchange(coreappdata->manual_libpaths, {});
848 Q_ASSERT(!coreappdata->libPathsManuallySet());
854 QStringList newPaths(q->libraryPaths());
855 for (qsizetype i = manualPaths.size(), j = appPaths.size(); i > 0 || j > 0; qt_noop()) {
857 newPaths.prepend(manualPaths[--i]);
858 }
else if (--i < 0) {
859 newPaths.removeAll(appPaths[j]);
860 }
else if (manualPaths[i] != appPaths[j]) {
861 newPaths.removeAll(appPaths[j]);
865 coreappdata->manual_libpaths.swap(newPaths);
872 Q_ASSERT(!eventDispatcher);
873 auto thisThreadData = threadData.loadRelaxed();
874 eventDispatcher = thisThreadData->eventDispatcher.loadRelaxed();
877 if (!eventDispatcher)
878 createEventDispatcher();
879 Q_ASSERT(eventDispatcher);
881 if (!eventDispatcher->parent()) {
882 eventDispatcher->moveToThread(thisThreadData->thread.loadAcquire());
883 eventDispatcher->setParent(q);
886 thisThreadData->eventDispatcher = eventDispatcher;
887 eventDispatcherReady();
890 processCommandLineArguments();
892 qt_call_pre_routines();
893 QT_MANGLE_NAMESPACE(qt_startup_hook)();
894#ifndef QT_BOOTSTRAPPED
895 QtPrivate::initBindingStatusThreadId();
896 if (Q_UNLIKELY(qtHookData[QHooks::Startup]))
897 reinterpret_cast<QHooks::StartupCallback>(qtHookData[QHooks::Startup])();
901 is_app_running =
true;
906
907
908QCoreApplication::~QCoreApplication()
910 preRoutinesCalled =
false;
912 qt_call_post_routines();
914#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
915# ifdef __cpp_lib_atomic_ref
916 std::atomic_ref(self).store(
nullptr, std::memory_order_relaxed);
920 g_self.storeRelaxed(
nullptr);
922 self.storeRelaxed(
nullptr);
926 QCoreApplicationPrivate::is_app_closing =
true;
927 QCoreApplicationPrivate::is_app_running =
false;
932 QThreadPool *globalThreadPool =
nullptr;
934 globalThreadPool = QThreadPool::globalInstance();
938 if (globalThreadPool) {
939 globalThreadPool->waitForDone();
940 delete globalThreadPool;
945 d_func()->threadData.loadRelaxed()->eventDispatcher =
nullptr;
946 if (QCoreApplicationPrivate::eventDispatcher)
947 QCoreApplicationPrivate::eventDispatcher->closingDown();
948 QCoreApplicationPrivate::eventDispatcher =
nullptr;
951#if QT_CONFIG(library)
952 if (coreappdata.exists()) {
954 coreappdata->app_libpaths = QStringList();
955 coreappdata->manual_libpaths = QStringList();
956 Q_ASSERT(!coreappdata->libPathsManuallySet());
957 Q_ASSERT(!coreappdata->libPathsInitialized());
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985void QCoreApplication::setSetuidAllowed(
bool allow)
987 QCoreApplicationPrivate::setuidAllowed = allow;
991
992
993
994
995
996
997
998bool QCoreApplication::isSetuidAllowed()
1000 return QCoreApplicationPrivate::setuidAllowed;
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute,
bool on)
1017 static_assert(Qt::AA_AttributeCount <=
sizeof(QCoreApplicationPrivate::attribs) * CHAR_BIT);
1020 QCoreApplicationPrivate::attribs |= 1 << attribute;
1022 QCoreApplicationPrivate::attribs &= ~(1 << attribute);
1023#if defined(QT_NO_QOBJECT)
1024 if (Q_UNLIKELY(qApp)) {
1026 if (Q_UNLIKELY(QCoreApplicationPrivate::is_app_running)) {
1028 switch (attribute) {
1029 case Qt::AA_PluginApplication:
1030 case Qt::AA_UseDesktopOpenGL:
1031 case Qt::AA_UseOpenGLES:
1032 case Qt::AA_UseSoftwareOpenGL:
1033#ifdef QT_BOOTSTRAPPED
1034 qWarning(
"Attribute %d must be set before QCoreApplication is created.",
1037 qWarning(
"Attribute Qt::%s must be set before QCoreApplication is created.",
1038 QMetaEnum::fromType<Qt::ApplicationAttribute>().valueToKey(attribute));
1048
1049
1050
1051
1052
1053bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute)
1055 return QCoreApplicationPrivate::testAttribute(attribute);
1058#ifndef QT_NO_QOBJECT
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1079bool QCoreApplication::isQuitLockEnabled()
1081 return quitLockEnabled;
1084static bool doNotify(QObject *, QEvent *);
1086void QCoreApplication::setQuitLockEnabled(
bool enabled)
1088 quitLockEnabled = enabled;
1092
1093
1094
1095
1096
1097
1098bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
1104 QObjectPrivate *d = receiver->d_func();
1105 QThreadData *threadData = d->threadData.loadAcquire();
1106 bool selfRequired = threadData->requiresCoreApplication;
1107 if (selfRequired && !
qApp)
1112 bool result =
false;
1113 void *cbdata[] = { receiver, event, &result };
1114 if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) {
1118 QScopedScopeLevelCounter scopeLevelCounter(threadData);
1120 return doNotify(receiver, event);
1122#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1123 if (!QThread::isMainThread())
1126 return qApp->notify(receiver, event);
1130
1131
1132
1133
1134
1135
1136bool QCoreApplication::forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent)
1138 if (event && originatingEvent)
1139 event->m_spont = originatingEvent->m_spont;
1141 return notifyInternal2(receiver, event);
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
1189
1190
1191
1192
1193
1194
1195
1197bool QCoreApplication::notify(QObject *receiver, QEvent *event)
1202#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1203 Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed()
1204 == QCoreApplicationPrivate::mainThread());
1208 if (QCoreApplicationPrivate::is_app_closing)
1210 return doNotify(receiver, event);
1213static bool doNotify(QObject *receiver, QEvent *event)
1218 if (receiver ==
nullptr) {
1219 qWarning(
"QCoreApplication::notify: Unexpected null receiver");
1223 QCoreApplicationPrivate::checkReceiverThread(receiver,
"QCoreApplication::notify");
1225 return receiver->isWidgetType() ?
false : QCoreApplicationPrivate::notify_helper(receiver, event);
1228bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
1231 Q_ASSERT(QThread::isMainThread());
1235 for (qsizetype i = 0; i < extraData->eventFilters.size(); ++i) {
1236 QObject *obj = extraData->eventFilters.at(i);
1239 if (obj->d_func()->threadData.loadRelaxed() != threadData.loadRelaxed()) {
1240 qWarning(
"QCoreApplication: Application event filter cannot be in a different thread.");
1243 if (obj->eventFilter(receiver, event))
1250bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
1252 if (receiver !=
qApp && receiver->d_func()->extraData) {
1253 for (qsizetype i = 0; i < receiver->d_func()->extraData->eventFilters.size(); ++i) {
1254 QObject *obj = receiver->d_func()->extraData->eventFilters.at(i);
1257 if (obj->d_func()->threadData.loadRelaxed() != receiver->d_func()->threadData.loadRelaxed()) {
1258 qWarning(
"QCoreApplication: Object event filter cannot be in a different thread.");
1261 if (obj->eventFilter(receiver, event))
1269
1270
1271
1272
1273bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
1277 Q_TRACE(QCoreApplication_notify_entry, receiver, event, event->type());
1278 bool consumed =
false;
1279 bool filtered =
false;
1280 Q_TRACE_EXIT(QCoreApplication_notify_exit, consumed, filtered);
1283 if (QThread::isMainThread()
1284 && QCoreApplication::self
1285 && QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event)) {
1290 if (sendThroughObjectEventFilters(receiver, event)) {
1296 consumed = receiver->event(event);
1301
1302
1303
1304
1305
1307bool QCoreApplication::startingUp()
1309 return !QCoreApplicationPrivate::is_app_running;
1313
1314
1315
1316
1317
1319bool QCoreApplication::closingDown()
1321 return QCoreApplicationPrivate::is_app_closing;
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags)
1356 QThreadData *data = QThreadData::current();
1357 if (!data->hasEventDispatcher())
1359 data->eventDispatcher.loadRelaxed()->processEvents(flags);
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags,
int ms)
1376 QCoreApplication::processEvents(flags, QDeadlineTimer(ms));
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline)
1408 QThreadData *data = QThreadData::current();
1409 if (!data->hasEventDispatcher())
1412 while (data->eventDispatcher.loadRelaxed()->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) {
1413 if (deadline.hasExpired())
1419
1420
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448int QCoreApplication::exec()
1450 if (!QCoreApplicationPrivate::checkInstance(
"exec"))
1453 QThreadData *threadData = self->d_func()->threadData.loadAcquire();
1454 if (threadData != QThreadData::current()) {
1455 qWarning(
"%s::exec: Must be called from the main thread", self->metaObject()->className());
1458 if (!threadData->eventLoops.isEmpty()) {
1459 qWarning(
"QCoreApplication::exec: The event loop is already running");
1463 threadData->quitNow =
false;
1464 QEventLoop eventLoop;
1465 self->d_func()->in_exec =
true;
1466 self->d_func()->aboutToQuitEmitted =
false;
1467 int returnCode = eventLoop.exec(QEventLoop::ApplicationExec);
1468 threadData->quitNow =
false;
1471 self->d_func()->execCleanup();
1482void QCoreApplicationPrivate::execCleanup()
1484 threadData.loadRelaxed()->quitNow =
false;
1486 QCoreApplication::sendPostedEvents(
nullptr, QEvent::DeferredDelete);
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520void QCoreApplication::exit(
int returnCode)
1526 if (!qstdweb::haveAsyncify())
1527 qFatal(
"Terminating since we don't have asyncify");
1529 QCoreApplicationPrivate *d = self->d_func();
1530 if (!d->aboutToQuitEmitted) {
1531 emit self->aboutToQuit(QCoreApplication::QPrivateSignal());
1532 d->aboutToQuitEmitted =
true;
1534 QThreadData *data = d->threadData.loadRelaxed();
1535 data->quitNow =
true;
1536 for (qsizetype i = 0; i < data->eventLoops.size(); ++i) {
1537 QEventLoop *eventLoop = data->eventLoops.at(i);
1538 eventLoop->exit(returnCode);
1543
1544
1546#ifndef QT_NO_QOBJECT
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
1563 Q_ASSERT_X(receiver,
"QCoreApplication::sendEvent",
"Unexpected null receiver");
1564 Q_ASSERT_X(event,
"QCoreApplication::sendEvent",
"Unexpected null event");
1566 Q_TRACE(QCoreApplication_sendEvent, receiver, event, event->type());
1568 event->m_spont =
false;
1569 return notifyInternal2(receiver, event);
1573
1574
1575bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
1577 Q_ASSERT_X(receiver,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null receiver");
1578 Q_ASSERT_X(event,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null event");
1580 Q_TRACE(QCoreApplication_sendSpontaneousEvent, receiver, event, event->type());
1582 event->m_spont =
true;
1583 return notifyInternal2(receiver, event);
1588QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThreadPostEventList(QObject *object)
1590 QPostEventListLocker locker;
1593 locker.threadData = QThreadData::current();
1594 locker.locker = qt_unique_lock(locker.threadData->postEventList.mutex);
1598 auto &threadData = QObjectPrivate::get(object)->threadData;
1603 locker.threadData = threadData.loadAcquire();
1604 if (!locker.threadData) {
1609 auto temporaryLocker = qt_unique_lock(locker.threadData->postEventList.mutex);
1610 if (locker.threadData == threadData.loadAcquire()) {
1611 locker.locker = std::move(temporaryLocker);
1616 Q_ASSERT(locker.threadData);
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
1649
1650
1651
1652
1653
1654
1655
1656void QCoreApplication::postEvent(QObject *receiver, QEvent *event,
int priority)
1658 Q_ASSERT_X(event,
"QCoreApplication::postEvent",
"Unexpected null event");
1660 Q_TRACE_SCOPE(QCoreApplication_postEvent, receiver, event, event->type());
1663 if (receiver ==
nullptr) {
1664 qWarning(
"QCoreApplication::postEvent: Unexpected null receiver");
1669 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1670 if (!locker.threadData) {
1676 QThreadData *data = locker.threadData;
1679 QT_WARNING_DISABLE_DEPRECATED
1681 if (receiver->d_func()->postedEvents.loadAcquire()
1682 && self && self->compressEvent(event, receiver, &data->postEventList)) {
1683 Q_TRACE(QCoreApplication_postEvent_event_compressed, receiver, event);
1690 std::unique_ptr<QEvent> eventDeleter(event);
1691 Q_TRACE(QCoreApplication_postEvent_event_posted, receiver, event, event->type());
1692 data->postEventList.addEvent(QPostEvent(receiver, event, priority));
1693 Q_UNUSED(eventDeleter.release());
1694 event->m_posted =
true;
1695 receiver->d_func()->postedEvents.fetchAndAddRelease(1);
1696 data->canWait =
false;
1699 QAbstractEventDispatcher* dispatcher = data->eventDispatcher.loadAcquire();
1701 dispatcher->wakeUp();
1705
1706
1707
1708#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1709bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1711 return d_func()->compressEvent(event, receiver, postedEvents);
1715bool QCoreApplicationPrivate::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1719 Q_ASSERT(postedEvents);
1722 if (event->type() == QEvent::Timer) {
1723 const int timerId =
static_cast<QTimerEvent *>(event)->timerId();
1724 auto it = postedEvents->cbegin();
1725 const auto end = postedEvents->cend();
1727 if (it->event && it->event->type() == QEvent::Timer && it->receiver == receiver) {
1728 if (
static_cast<QTimerEvent *>(it->event)->timerId() == timerId) {
1738 if (event->type() == QEvent::Quit) {
1739 for (
const QPostEvent &cur : std::as_const(*postedEvents)) {
1740 if (cur.receiver != receiver
1741 || cur.event ==
nullptr
1742 || cur.event->type() != event->type())
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770void QCoreApplication::sendPostedEvents(QObject *receiver,
int event_type)
1775 QThreadData *data = QThreadData::current();
1777 QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data);
1780void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver,
int event_type,
1783 if (event_type == -1) {
1788 if (receiver && receiver->d_func()->threadData.loadRelaxed() != data) {
1789 qWarning(
"QCoreApplication::sendPostedEvents: Cannot send "
1790 "posted events for objects in another thread");
1794 ++data->postEventList.recursion;
1796 auto locker = qt_unique_lock(data->postEventList.mutex);
1801 data->canWait = (data->postEventList.size() == 0);
1803 if (data->postEventList.size() == 0
1804 || (receiver && !receiver->d_func()->postedEvents.loadAcquire())) {
1805 --data->postEventList.recursion;
1809 data->canWait =
true;
1813 qsizetype startOffset = data->postEventList.startOffset;
1814 qsizetype &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset;
1815 data->postEventList.insertionOffset = data->postEventList.size();
1819 Q_DISABLE_COPY_MOVE(CleanUp)
1824 bool exceptionCaught;
1826 inline CleanUp(QObject *receiver,
int event_type, QThreadData *data) :
1827 receiver(receiver), event_type(event_type), data(data), exceptionCaught(
true)
1831 if (exceptionCaught) {
1833 data->canWait =
false;
1836 --data->postEventList.recursion;
1837 if (!data->postEventList.recursion && !data->canWait && data->hasEventDispatcher())
1838 data->eventDispatcher.loadRelaxed()->wakeUp();
1842 if (!event_type && !receiver && data->postEventList.startOffset >= 0) {
1843 const QPostEventList::iterator it = data->postEventList.begin();
1844 data->postEventList.erase(it, it + data->postEventList.startOffset);
1845 data->postEventList.insertionOffset -= data->postEventList.startOffset;
1846 Q_ASSERT(data->postEventList.insertionOffset >= 0);
1847 data->postEventList.startOffset = 0;
1851 CleanUp cleanup(receiver, event_type, data);
1853 while (i < data->postEventList.size()) {
1855 if (i >= data->postEventList.insertionOffset)
1858 const QPostEvent &pe = data->postEventList.at(i);
1863 if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) {
1864 data->canWait =
false;
1868 if (pe.event->type() == QEvent::DeferredDelete) {
1875 const int eventLoopLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->loopLevel();
1876 const int eventScopeLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->scopeLevel();
1878 const bool postedBeforeOutermostLoop = eventLoopLevel == 0;
1879 const bool allowDeferredDelete =
1880 (eventLoopLevel + eventScopeLevel > data->loopLevel + data->scopeLevel
1881 || (postedBeforeOutermostLoop && data->loopLevel > 0)
1882 || (event_type == QEvent::DeferredDelete
1883 && eventLoopLevel + eventScopeLevel == data->loopLevel + data->scopeLevel));
1884 if (!allowDeferredDelete) {
1886 if (!event_type && !receiver) {
1891 QPostEvent pe_copy = pe;
1895 const_cast<QPostEvent &>(pe).event =
nullptr;
1898 data->postEventList.addEvent(pe_copy);
1906 pe.event->m_posted =
false;
1907 QEvent *e = pe.event;
1908 QObject * r = pe.receiver;
1911 const auto previous = r->d_func()->postedEvents.fetchAndSubAcquire(1);
1912 Q_ASSERT(previous > 0);
1916 const_cast<QPostEvent &>(pe).event =
nullptr;
1919 const auto relocker = qScopeGuard([&locker] { locker.lock(); });
1921 const std::unique_ptr<QEvent> event_deleter(e);
1924 QCoreApplication::sendEvent(r, e);
1931 cleanup.exceptionCaught =
false;
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1953void QCoreApplication::removePostedEvents(QObject *receiver,
int eventType)
1955 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1956 QThreadData *data = locker.threadData;
1962 if (receiver && !receiver->d_func()->postedEvents.loadAcquire())
1967 QVarLengthArray<QEvent*> events;
1968 qsizetype n = data->postEventList.size();
1971 for (qsizetype i = 0; i < n; ++i) {
1972 const QPostEvent &pe = data->postEventList.at(i);
1974 if ((!receiver || pe.receiver == receiver)
1975 && (pe.event && (eventType == 0 || pe.event->type() == eventType))) {
1976 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
1977 pe.event->m_posted =
false;
1978 events.append(pe.event);
1979 const_cast<QPostEvent &>(pe).event =
nullptr;
1980 }
else if (!data->postEventList.recursion) {
1982 qSwap(data->postEventList[i], data->postEventList[j]);
1988 if (receiver && eventType == 0) {
1989 Q_ASSERT(!receiver->d_func()->postedEvents.loadRelaxed());
1993 if (!data->postEventList.recursion) {
1995 data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end());
2003
2004
2005
2006
2007
2008
2009
2010
2011
2013void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
2015 if (!event || !event->m_posted)
2018 QThreadData *data = QThreadData::current();
2020 const auto locker = qt_scoped_lock(data->postEventList.mutex);
2022 if (data->postEventList.size() == 0) {
2023#if defined(QT_DEBUG)
2024 qDebug(
"QCoreApplication::removePostedEvent: Internal error: %p %d is posted",
2025 (
void*)event, event->type());
2030 for (
const QPostEvent &pe : std::as_const(data->postEventList)) {
2031 if (pe.event == event) {
2033 qWarning(
"QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s",
2035 pe.receiver->metaObject()->className(),
2036 pe.receiver->objectName().toLocal8Bit().data());
2038 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
2039 pe.event->m_posted =
false;
2041 const_cast<QPostEvent &>(pe).event =
nullptr;
2048
2049
2050bool QCoreApplication::event(QEvent *e)
2052 if (e->type() == QEvent::Quit) {
2056 return QObject::event(e);
2059void QCoreApplicationPrivate::ref()
2064void QCoreApplicationPrivate::deref()
2066 quitLockRef.deref();
2068 if (quitLockEnabled && canQuitAutomatically())
2069 quitAutomatically();
2072bool QCoreApplicationPrivate::canQuitAutomatically()
2083 if (quitLockRef.loadRelaxed())
2089void QCoreApplicationPrivate::quitAutomatically()
2091 Q_Q(QCoreApplication);
2099 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
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
2129
2130
2131
2132
2133
2134
2135
2136void QCoreApplication::quit()
2141 if (!self->d_func()->in_exec)
2144 self->d_func()->quit();
2147void QCoreApplicationPrivate::quit()
2149 Q_Q(QCoreApplication);
2151 if (QThread::isMainThread()) {
2152 QEvent quitEvent(QEvent::Quit);
2153 QCoreApplication::sendEvent(q, &quitEvent);
2155 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2182#ifndef QT_NO_TRANSLATION
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2214bool QCoreApplication::installTranslator(QTranslator *translationFile)
2216 if (!translationFile)
2219 if (!QCoreApplicationPrivate::checkInstance(
"installTranslator"))
2222 QCoreApplicationPrivate *d = self->d_func();
2224 QWriteLocker locker(&d->translateMutex);
2225 d->translators.prepend(translationFile);
2228 if (translationFile->isEmpty())
2231#ifndef QT_NO_QOBJECT
2232 QEvent ev(QEvent::LanguageChange);
2233 QCoreApplication::sendEvent(self, &ev);
2240
2241
2242
2243
2244
2245
2246
2247
2249bool QCoreApplication::removeTranslator(QTranslator *translationFile)
2251 if (!translationFile)
2253 if (!QCoreApplicationPrivate::checkInstance(
"removeTranslator"))
2255 QCoreApplicationPrivate *d = self->d_func();
2256 QWriteLocker locker(&d->translateMutex);
2257 if (d->translators.removeAll(translationFile)) {
2258#ifndef QT_NO_QOBJECT
2260 if (!self->closingDown()) {
2261 QEvent ev(QEvent::LanguageChange);
2262 QCoreApplication::sendEvent(self, &ev);
2273 qsizetype percentPos = 0;
2275 while ((percentPos = result->indexOf(u'%', percentPos + len)) != -1) {
2277 if (percentPos + len == result->size())
2280 if (result->at(percentPos + len) == u'L') {
2282 if (percentPos + len == result->size())
2288 if (result->at(percentPos + len) == u'n') {
2291 result->replace(percentPos, len, fmt);
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
2324
2325
2326
2327
2328
2329
2330
2331QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2332 const char *disambiguation,
int n)
2340 QCoreApplicationPrivate *d = self->d_func();
2341 QReadLocker locker(&d->translateMutex);
2342 if (!d->translators.isEmpty()) {
2343 QList<QTranslator*>::ConstIterator it;
2344 QTranslator *translationFile;
2345 for (it = d->translators.constBegin(); it != d->translators.constEnd(); ++it) {
2346 translationFile = *it;
2347 result = translationFile->translate(context, sourceText, disambiguation, n);
2348 if (!result.isNull())
2354 if (result.isNull())
2355 result = QString::fromUtf8(sourceText);
2357 replacePercentN(&result, n);
2364 return QCoreApplication::translate(
nullptr, id,
nullptr, n);
2367bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
2369 if (!QCoreApplication::self)
2371 QCoreApplicationPrivate *d = QCoreApplication::self->d_func();
2372 QReadLocker locker(&d->translateMutex);
2373 return d->translators.contains(translator);
2378QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2379 const char *disambiguation,
int n)
2382 Q_UNUSED(disambiguation);
2383 QString ret = QString::fromUtf8(sourceText);
2385 ret.replace(
"%n"_L1, QString::number(n));
2391#ifndef QT_BOOTSTRAPPED
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415QString QCoreApplication::applicationDirPath()
2418 qWarning(
"QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
2422 QFileSystemEntry appFilePath(applicationFilePath(), QFileSystemEntry::FromInternalPath{});
2423 return appFilePath.isEmpty() ? QString() : appFilePath.path();
2426#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN)
2430# if defined(Q_OS_ANDROID) || defined(Q_OS_HARMONY)
2433# elif defined(Q_OS_LINUX)
2435 return QFile::decodeName(qt_readlink(
"/proc/self/exe"));
2436# elif defined(AT_EXECPATH)
2438 char execfn[PATH_MAX];
2439 if (elf_aux_info(AT_EXECPATH, execfn,
sizeof(execfn)) != 0)
2442 qsizetype len = qstrlen(execfn);
2443 return QFile::decodeName(QByteArray::fromRawData(execfn, len));
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466QString QCoreApplication::applicationFilePath()
2469 qWarning(
"QCoreApplication::applicationFilePath: Please instantiate the QApplication object first");
2473 QCoreApplicationPrivate *d = self->d_func();
2476 static QByteArray procName = QByteArray(d->argv[0]);
2477 if (procName != QByteArrayView(d->argv[0])) {
2479 d->cachedApplicationFilePath = QString();
2480 procName.assign(d->argv[0]);
2484 if (!d->cachedApplicationFilePath.isNull())
2485 return d->cachedApplicationFilePath;
2487 QString absPath = qAppFileName();
2488 if (Q_LIKELY(!absPath.isEmpty())) {
2490 return d->cachedApplicationFilePath = std::move(absPath);
2493 if (
const QStringList args = arguments(); !args.isEmpty()) {
2494 const QString &argv0 = args[0];
2496 if (!argv0.isEmpty() && argv0.at(0) == u'/') {
2498
2499
2500
2502 }
else if (argv0.contains(u'/')) {
2504
2505
2506
2507 absPath = QDir::current().absoluteFilePath(argv0);
2510
2511
2512
2513 absPath = QStandardPaths::findExecutable(argv0);
2517 absPath = QFileInfo(absPath).canonicalFilePath();
2518 if (!absPath.isEmpty()) {
2519 return d->cachedApplicationFilePath = std::move(absPath);
2526
2527
2528
2529
2530qint64 QCoreApplication::applicationPid()
noexcept
2532#if defined(Q_OS_WIN)
2533 return GetCurrentProcessId();
2534#elif defined(Q_OS_VXWORKS)
2535 return (pid_t) taskIdCurrent;
2542static QStringList winCmdArgs()
2551 if (
wchar_t **argv = CommandLineToArgvW(GetCommandLine(), &size)) {
2552 result.reserve(size);
2553 wchar_t **argvEnd = argv + size;
2554 for (
wchar_t **a = argv; a < argvEnd; ++a)
2555 result.append(QString::fromWCharArray(*a));
2563
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
2594QStringList QCoreApplication::arguments()
2599 qWarning(
"QCoreApplication::arguments: Please instantiate the QApplication object first");
2603 const QCoreApplicationPrivate *d = self->d_func();
2605 const int argc = d->argc;
2606 char **
const argv = d->argv;
2609#if defined(Q_OS_WIN)
2610 const bool argsModifiedByUser = d->origArgv ==
nullptr;
2611 if (!argsModifiedByUser) {
2612 QStringList commandLineArguments = winCmdArgs();
2617 if (argc != d->origArgc) {
2624 for (
int i = 0; i < d->origArgc; ++i) {
2625 if (!contains(argc, argv, d->origArgv[i]))
2626 commandLineArguments.removeAll(QString::fromLocal8Bit(d->origArgv[i]));
2630 return commandLineArguments;
2634 for (
int a = 0; a < argc; ++a)
2635 list << QString::fromLocal8Bit(argv[a]);
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2657
2658
2659
2660
2661
2662
2663
2664void QCoreApplication::setOrganizationName(
const QString &orgName)
2666 if (coreappdata()->orgName == orgName)
2668 coreappdata()->orgName = orgName;
2669#ifndef QT_NO_QOBJECT
2670 if (QCoreApplication::self)
2671 emit QCoreApplication::self->organizationNameChanged();
2675QString QCoreApplication::organizationName()
2677 return coreappdata()->orgName;
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2696
2697
2698
2699
2700
2701void QCoreApplication::setOrganizationDomain(
const QString &orgDomain)
2703 if (coreappdata()->orgDomain == orgDomain)
2705 coreappdata()->orgDomain = orgDomain;
2706#ifndef QT_NO_QOBJECT
2707 if (QCoreApplication::self)
2708 emit QCoreApplication::self->organizationDomainChanged();
2712QString QCoreApplication::organizationDomain()
2714 return coreappdata()->orgDomain;
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2734
2735
2736
2737
2738
2739void QCoreApplication::setApplicationName(
const QString &application)
2741 coreappdata()->applicationNameSet = !application.isEmpty();
2742 QString newAppName = application;
2743 if (newAppName.isEmpty() && QCoreApplication::self)
2744 newAppName = QCoreApplication::self->d_func()->appName();
2745 if (coreappdata()->application == newAppName)
2747 coreappdata()->application = newAppName;
2748#ifndef QT_NO_QOBJECT
2749 if (QCoreApplication::self)
2750 emit QCoreApplication::self->applicationNameChanged();
2754QString QCoreApplication::applicationName()
2756 return coreappdata() ? coreappdata()->application : QString();
2760
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
2787
2788
2790
2791
2792
2793
2794
2795void QCoreApplication::setApplicationVersion(
const QString &version)
2797 coreappdata()->applicationVersionSet = !version.isEmpty();
2798 QString newVersion = version;
2799 if (newVersion.isEmpty() && QCoreApplication::self)
2800 newVersion = QCoreApplication::self->d_func()->appVersion();
2801 if (coreappdata()->applicationVersion == newVersion)
2803 coreappdata()->applicationVersion = newVersion;
2804#ifndef QT_NO_QOBJECT
2805 if (QCoreApplication::self)
2806 emit QCoreApplication::self->applicationVersionChanged();
2810QString QCoreApplication::applicationVersion()
2812 return coreappdata() ? coreappdata()->applicationVersion : QString();
2815#if QT_CONFIG(permissions) || defined(Q_QDOC)
2818
2819
2820
2821
2822
2823
2824
2825
2826Qt::PermissionStatus QCoreApplication::checkPermission(
const QPermission &permission)
2828 return QPermissions::Private::checkPermission(permission);
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
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
2894
2895
2898
2899
2900
2901
2902
2903
2904
2905void QCoreApplication::requestPermissionImpl(
const QPermission &requestedPermission,
2906 QtPrivate::QSlotObjectBase *slotObjRaw,
const QObject *context)
2908 QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw};
2911 if (!QThread::isMainThread()) {
2912 qCWarning(lcPermissions,
"Permissions can only be requested from the GUI (main) thread");
2916 class PermissionReceiver :
public QObject
2919 explicit PermissionReceiver(QtPrivate::SlotObjUniquePtr &&slotObject,
const QObject *context)
2920 : slotObject(std::move(slotObject)), context(context ? context :
this)
2922 Q_ASSERT(
this->context);
2923 moveToThread(
this->context->thread());
2926 void finalizePermissionRequest(
const QPermission &permission)
2928 Q_ASSERT(slotObject);
2931 void *args[] = {
nullptr,
const_cast<QPermission *>(&permission) };
2932 slotObject->call(
const_cast<QObject *>(context.data()), args);
2937 QtPrivate::SlotObjUniquePtr slotObject;
2938 QPointer<
const QObject> context;
2942 auto receiver = std::make_shared<PermissionReceiver>(std::move(slotObj), context);
2944 QPermissions::Private::requestPermission(requestedPermission,
2945 [=, receiver = std::move(receiver)](Qt::PermissionStatus status)
mutable {
2946 if (status == Qt::PermissionStatus::Undetermined) {
2947 Q_ASSERT_X(
false,
"QPermission",
2948 "Internal error: requestPermission() should never return Undetermined");
2949 status = Qt::PermissionStatus::Denied;
2952 if (QCoreApplication::self) {
2953 QPermission permission = requestedPermission;
2954 permission.m_status = status;
2955 auto receiverObject = receiver.get();
2956 QMetaObject::invokeMethod(receiverObject,
2957 [receiver = std::move(receiver), permission] {
2958 receiver->finalizePermissionRequest(permission);
2959 }, Qt::QueuedConnection);
2966#if QT_CONFIG(library)
2967static QStringList libraryPathsLocked();
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
2996
2997
2998
2999
3000
3001
3002QStringList QCoreApplication::libraryPaths()
3004 QMutexLocker locker(&coreappdata->libraryPathMutex);
3005 return libraryPathsLocked();
3009
3010
3011static QStringList libraryPathsLocked()
3013 QCoreApplicationData *d = coreappdata;
3014 if (d->libPathsManuallySet())
3015 return d->manual_libpaths;
3017 QStringList *app_libpaths = &d->app_libpaths;
3018 if (!d->libPathsInitialized()) {
3020 auto setPathsFromEnv = [&](QString libPathEnv) {
3021 if (!libPathEnv.isEmpty()) {
3022 QStringList paths = libPathEnv.split(QDir::listSeparator(), Qt::SkipEmptyParts);
3023 for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
3024 QString canonicalPath = QDir(*it).canonicalPath();
3025 if (!canonicalPath.isEmpty()
3026 && !app_libpaths->contains(canonicalPath)) {
3027 app_libpaths->append(canonicalPath);
3032 setPathsFromEnv(qEnvironmentVariable(
"QT_PLUGIN_PATH"));
3034 const auto pluginPaths = QLibraryInfo::paths(QLibraryInfo::PluginsPath);
3035 for (
auto path : pluginPaths) {
3036 if (QFile::exists(path)) {
3038 path = QDir(path).canonicalPath();
3039 if (!app_libpaths->contains(path))
3040 app_libpaths->append(path);
3047 QString app_location = QCoreApplication::applicationFilePath();
3048 app_location.truncate(app_location.lastIndexOf(u'/'));
3049 app_location = QDir(app_location).canonicalPath();
3050 if (QFile::exists(app_location) && !app_libpaths->contains(app_location))
3051 app_libpaths->append(app_location);
3053 if (app_libpaths->isEmpty())
3054 app_libpaths->reserve(1);
3055 Q_ASSERT(d->libPathsInitialized());
3057 return *app_libpaths;
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077void QCoreApplication::setLibraryPaths(
const QStringList &paths)
3079 QCoreApplicationData *d = coreappdata;
3080 QMutexLocker locker(&d->libraryPathMutex);
3085 if (!d->libPathsInitialized())
3086 libraryPathsLocked();
3088 d->manual_libpaths = paths;
3089 if (d->manual_libpaths.isEmpty())
3090 d->manual_libpaths.reserve(1);
3091 Q_ASSERT(d->libPathsManuallySet());
3094 QFactoryLoader::refreshAll();
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116void QCoreApplication::addLibraryPath(
const QString &path)
3121 QString canonicalPath = QDir(path).canonicalPath();
3122 if (canonicalPath.isEmpty())
3125 QCoreApplicationData *d = coreappdata;
3126 QMutexLocker locker(&d->libraryPathMutex);
3128 QStringList *libpaths = &d->manual_libpaths;
3129 if (d->libPathsManuallySet()) {
3130 if (d->manual_libpaths.contains(canonicalPath))
3134 libraryPathsLocked();
3135 QStringList *app_libpaths = &d->app_libpaths;
3136 if (app_libpaths->contains(canonicalPath))
3139 *libpaths = *app_libpaths;
3142 libpaths->prepend(canonicalPath);
3143 Q_ASSERT(d->libPathsManuallySet());
3145 QFactoryLoader::refreshAll();
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160void QCoreApplication::removeLibraryPath(
const QString &path)
3165 QString canonicalPath = QDir(path).canonicalPath();
3166 if (canonicalPath.isEmpty())
3169 QCoreApplicationData *d = coreappdata;
3170 QMutexLocker locker(&d->libraryPathMutex);
3172 QStringList *libpaths = &d->manual_libpaths;
3173 if (d->libPathsManuallySet()) {
3174 if (libpaths->removeAll(canonicalPath) == 0)
3178 libraryPathsLocked();
3179 QStringList *app_libpaths = &d->app_libpaths;
3180 if (!app_libpaths->contains(canonicalPath))
3183 *libpaths = *app_libpaths;
3184 libpaths->removeAll(canonicalPath);
3185 Q_ASSERT(d->libPathsManuallySet());
3189 QFactoryLoader::refreshAll();
3194#ifndef QT_NO_QOBJECT
3197
3198
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
3225void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
3227 if (QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
3228 qWarning(
"Native event filters are not applied when the Qt::AA_PluginApplication attribute is set");
3232 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3233 if (!filterObj || !eventDispatcher)
3235 eventDispatcher->installNativeEventFilter(filterObj);
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251void QCoreApplication::removeNativeEventFilter(QAbstractNativeEventFilter *filterObject)
3253 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3254 if (!filterObject || !eventDispatcher)
3256 eventDispatcher->removeNativeEventFilter(filterObject);
3260
3261
3262
3263QAbstractEventDispatcher *QCoreApplication::eventDispatcher()
3265 if (QCoreApplicationPrivate::theMainThread.loadAcquire())
3266 return QCoreApplicationPrivate::theMainThread.loadRelaxed()->eventDispatcher();
3271
3272
3273
3274
3275
3276void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
3278 QThread *mainThread = QCoreApplicationPrivate::theMainThread.loadAcquire();
3280 mainThread = QThread::currentThread();
3281 mainThread->setEventDispatcher(eventDispatcher);
3287
3288
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
3316
3317
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
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3395void *QCoreApplication::resolveInterface(
const char *name,
int revision)
const
3397#if defined(Q_OS_ANDROID)
3402 using namespace QNativeInterface;
3403 struct AndroidApplication :
public QAndroidApplication {};
3404 static AndroidApplication androidApplication;
3405 QT_NATIVE_INTERFACE_RETURN_IF(QAndroidApplication, &androidApplication);
3407 Q_UNUSED(name); Q_UNUSED(revision);
3413#ifndef QT_NO_QOBJECT
3414#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