15#include <private/qproperty_p.h>
16#include <qdatastream.h>
21#include <private/qfilesystementry_p.h>
23#include <private/qloggingregistry_p.h>
24#include <qscopeguard.h>
25#include <qstandardpaths.h>
28#include <qthreadstorage.h>
30#include <QtCore/qpromise.h>
32#include <private/qthread_p.h>
34#include <qthreadpool.h>
35#include <private/qthreadpool_p.h>
38#include <qlibraryinfo.h>
40#include <qvarlengtharray.h>
41#include <private/qfactoryloader_p.h>
42#include <private/qfunctions_p.h>
43#include <private/qlocale_p.h>
44#include <private/qlocking_p.h>
45#include <private/qhooks_p.h>
46#include <private/qnativeinterface_p.h>
48#if QT_CONFIG(permissions)
49#include <private/qpermissions_p.h>
54# if defined(Q_OS_DARWIN)
55# include "qeventdispatcher_cf_p.h"
57# if !defined(QT_NO_GLIB)
58# include "qeventdispatcher_glib_p.h"
61# if !defined(Q_OS_WASM)
62# include "qeventdispatcher_unix_p.h"
66#include "qeventdispatcher_win_p.h"
70#if defined(Q_OS_ANDROID)
71#include <QtCore/qjniobject.h>
75# include "qcore_mac_p.h"
82# ifndef Q_OS_INTEGRITY
86# include <sys/types.h>
88# include "qcore_unix_p.h"
100#include <emscripten/val.h>
103#ifdef QT_BOOTSTRAPPED
104#include <private/qtrace_p.h>
106#include <qtcore_tracepoints_p.h>
114# include <qt_windows.h>
119using namespace Qt::StringLiterals;
122 "#include <qcoreevent.h>"
134#if (defined(Q_OS_WIN) || defined(Q_OS_DARWIN)) && !defined(QT_BOOTSTRAPPED)
135extern QString qAppFileName();
138Q_CONSTINIT
bool QCoreApplicationPrivate::setuidAllowed =
false;
140#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
141# warning "Audit remaining direct usages of this variable for memory ordering semantics"
142Q_CONSTINIT QBasicAtomicPointer<QCoreApplication> QCoreApplication::self =
nullptr;
145Q_CONSTINIT
static QBasicAtomicPointer<QCoreApplication> g_self =
nullptr;
147# define qApp g_self.loadRelaxed()
150
151
152
153
154
155
156
157
158
159bool QCoreApplication::instanceExists()
noexcept
161 return qApp !=
nullptr;
165#if !defined(Q_OS_WIN) || defined(QT_BOOTSTRAPPED)
167QString QCoreApplicationPrivate::infoDictionaryStringProperty(
const QString &propertyName)
170 QCFString cfPropertyName = propertyName.toCFString();
171 CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(),
174 bundleName = QString::fromCFString(
static_cast<CFStringRef>(string));
178QString QCoreApplicationPrivate::appName()
const
180 QString applicationName;
182 applicationName = infoDictionaryStringProperty(QStringLiteral(
"CFBundleName"));
184 if (applicationName.isEmpty() && argv[0]) {
185 char *p = strrchr(argv[0],
'/');
186 applicationName = QString::fromLocal8Bit(p ? p + 1 : argv[0]);
189 return applicationName;
191QString QCoreApplicationPrivate::appVersion()
const
193 QString applicationVersion;
194#if defined(Q_OS_DARWIN)
195 applicationVersion = infoDictionaryStringProperty(QStringLiteral(
"CFBundleVersion"));
196#elif defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
197 QJniObject context(QNativeInterface::QAndroidApplication::context());
198 if (context.isValid()) {
199 QJniObject pm = context.callObjectMethod(
200 "getPackageManager",
"()Landroid/content/pm/PackageManager;");
201 QJniObject pn = context.callObjectMethod<jstring>(
"getPackageName");
202 if (pm.isValid() && pn.isValid()) {
203 QJniObject packageInfo = pm.callObjectMethod(
204 "getPackageInfo",
"(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;",
206 if (packageInfo.isValid()) {
207 QJniObject versionName = packageInfo.getObjectField(
208 "versionName",
"Ljava/lang/String;");
209 if (versionName.isValid())
210 return versionName.toString();
215 return applicationVersion;
219bool QCoreApplicationPrivate::checkInstance(
const char *function)
221 bool b = (
qApp !=
nullptr);
223 qWarning(
"QApplication::%s: Please instantiate the QApplication object first", function);
227#if QT_CONFIG(commandlineparser)
228void QCoreApplicationPrivate::addQtOptions(QList<QCommandLineOption> *options)
230 options->append(QCommandLineOption(QStringLiteral(
"qmljsdebugger"),
231 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."),
232 QStringLiteral(
"value")));
236void QCoreApplicationPrivate::processCommandLineArguments()
238 int j = argc ? 1 : 0;
239 for (
int i = 1; i < argc; ++i) {
242 if (*argv[i] !=
'-') {
246 const char *arg = argv[i];
249 if (strncmp(arg,
"-qmljsdebugger=", 15) == 0) {
250 qmljs_debug_arguments = QString::fromLocal8Bit(arg + 15);
251 }
else if (strcmp(arg,
"-qmljsdebugger") == 0 && i < argc - 1) {
253 qmljs_debug_arguments = QString::fromLocal8Bit(argv[i]);
278Q_GLOBAL_STATIC(QVFuncList, postRList)
279Q_CONSTINIT
static QBasicMutex globalRoutinesMutex;
283
284
285
286
287
301 const auto locker = qt_scoped_lock(globalRoutinesMutex);
310 const auto locker = qt_scoped_lock(globalRoutinesMutex);
319 const auto locker = qt_scoped_lock(globalRoutinesMutex);
328 if (!preRList.exists())
332 const auto locker = qt_scoped_lock(globalRoutinesMutex);
339 for (QtStartUpFunction f : list)
343void Q_CORE_EXPORT qt_call_post_routines()
345 if (!postRList.exists())
352 const auto locker = qt_scoped_lock(globalRoutinesMutex);
353 qSwap(*postRList, list);
358 for (QtCleanUpFunction f : std::as_const(list))
367Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_running =
false;
369Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_closing =
false;
373 const QPostEventList &l = QThreadData::current()->postEventList;
374 return l.size() - l.startOffset;
381Q_CONSTINIT uint QCoreApplicationPrivate::attribs =
382 (1 << Qt::AA_SynthesizeMouseForUnhandledTouchEvents) |
383 (1 << Qt::AA_SynthesizeMouseForUnhandledTabletEvents);
398#if QT_CONFIG(library)
411Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata)
414Q_CONSTINIT
static bool quitLockEnabled =
true;
423static inline bool isArgvModified(
int argc,
char **argv)
425 if (__argc != argc || !__argv )
429 for (
int a = 0; a < argc; ++a) {
430 if (argv[a] != __argv[a] && strcmp(argv[a], __argv[a]))
436static inline bool contains(
int argc,
char **argv,
const char *needle)
438 for (
int a = 0; a < argc; ++a) {
439 if (!strcmp(argv[a], needle))
446QCoreApplicationPrivate::QCoreApplicationPrivate(
int &aargc,
char **aargv)
447 : argc(aargc), argv(aargv)
449 static const char *
const empty =
"";
450 if (argc == 0 || argv ==
nullptr) {
452 argv =
const_cast<
char **>(&empty);
455 if (!isArgvModified(argc, argv)) {
457 origArgv = q20::make_unique_for_overwrite<
char *[]>(argc);
458 std::copy(argv, argv + argc, origArgv.get());
463 QCoreApplicationPrivate::is_app_closing =
false;
465# if defined(Q_OS_UNIX)
466 if (Q_UNLIKELY(!setuidAllowed && (geteuid() != getuid())))
467 qFatal(
"FATAL: The application binary appears to be running setuid, this is a security hole.");
470 QThread *cur = QThread::currentThread();
471 if (cur != theMainThread.loadAcquire())
472 qWarning(
"WARNING: QApplication was not created in the main() thread.");
476QCoreApplicationPrivate::~QCoreApplicationPrivate()
481#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
482 cleanupDebuggingConsole();
488void QCoreApplicationPrivate::cleanupThreadData()
490 auto thisThreadData = threadData.loadRelaxed();
492 if (thisThreadData && !threadData_clean) {
494 QThreadStoragePrivate::finish(&thisThreadData->tls);
498 const auto locker = qt_scoped_lock(thisThreadData->postEventList.mutex);
499 for (
const QPostEvent &pe : std::as_const(thisThreadData->postEventList)) {
501 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
502 pe.event->m_posted =
false;
506 thisThreadData->postEventList.clear();
507 thisThreadData->postEventList.recursion = 0;
508 thisThreadData->quitNow =
false;
509 threadData_clean =
true;
513void QCoreApplicationPrivate::createEventDispatcher()
515 Q_Q(QCoreApplication);
516 QThreadData *data = QThreadData::current();
517 Q_ASSERT(!data->hasEventDispatcher());
518 eventDispatcher = data->createEventDispatcher();
519 eventDispatcher->setParent(q);
522void QCoreApplicationPrivate::eventDispatcherReady()
526Q_CONSTINIT QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
527Q_CONSTINIT QBasicAtomicPointer<
void> QCoreApplicationPrivate::theMainThreadId = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
528QThread *QCoreApplicationPrivate::mainThread()
530 Q_ASSERT(theMainThread.loadRelaxed() !=
nullptr);
531 return theMainThread.loadRelaxed();
534void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
536 QThread *currentThread = QThread::currentThread();
537 QThread *thr = receiver->thread();
538 Q_ASSERT_X(currentThread == thr || !thr,
539 "QCoreApplication::sendEvent",
540 qPrintable(QString::fromLatin1(
"Cannot send events to objects owned by a different thread. "
541 "Current thread %1. Receiver '%2' was created in thread %3").arg(
542 QDebug::toString(currentThread), QDebug::toString(receiver), QDebug::toString(thr))));
543 Q_UNUSED(currentThread);
551 if (!QCoreApplicationPrivate::checkInstance(
"qAppName"))
553 return QCoreApplication::instance()->d_func()->appName();
556void QCoreApplicationPrivate::initLocale()
558#if defined(QT_BOOTSTRAPPED)
560#elif defined(Q_OS_UNIX)
561 Q_CONSTINIT
static bool qt_locale_initialized =
false;
562 if (qt_locale_initialized)
564 qt_locale_initialized =
true;
569 setlocale(LC_ALL,
"");
573# if defined(Q_OS_INTEGRITY)
574 setlocale(LC_CTYPE,
"UTF-8");
575# elif defined(Q_OS_QNX)
578# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__
581# elif defined(Q_OS_VXWORKS)
585 const std::string oldEncoding = nl_langinfo(CODESET);
586 if (!Q_LIKELY(qstricmp(oldEncoding.data(),
"UTF-8") == 0
587 || qstricmp(oldEncoding.data(),
"utf8") == 0)) {
588 const QByteArray oldLocale = setlocale(LC_ALL,
nullptr);
589 QByteArray newLocale;
590 bool warnOnOverride =
true;
591# if defined(Q_OS_DARWIN)
595 warnOnOverride = qstrcmp(setlocale(LC_CTYPE,
nullptr),
"C") != 0
596 || getenv(
"LC_ALL") || getenv(
"LC_CTYPE") || getenv(
"LANG");
600 newLocale = setlocale(LC_CTYPE,
"UTF-8");
602 newLocale = setlocale(LC_CTYPE,
nullptr);
603 if (qsizetype dot = newLocale.indexOf(
'.'); dot != -1)
604 newLocale.truncate(dot);
605 if (qsizetype at = newLocale.indexOf(
'@'); at != -1)
606 newLocale.truncate(at);
607 newLocale +=
".UTF-8";
608 newLocale = setlocale(LC_CTYPE, newLocale);
611 if (newLocale.isEmpty())
612 newLocale = setlocale(LC_CTYPE,
"C.UTF-8");
613 if (newLocale.isEmpty())
614 newLocale = setlocale(LC_CTYPE,
"C.utf8");
617 if (newLocale.isEmpty()) {
619 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
620 "Qt depends on a UTF-8 locale, but has failed to switch to one.\n"
621 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
622 "for more information.", oldLocale.constData(), oldEncoding.data());
623 }
else if (warnOnOverride) {
625 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
626 "Qt depends on a UTF-8 locale, and has switched to \"%s\" instead.\n"
627 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
628 "for more information.",
629 oldLocale.constData(), oldEncoding.data(), newLocale.constData());
638
639
640
641
642
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
714
715
716
717
718
719
720
723
724
725QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
729 : QObject(p,
nullptr)
732 d_func()->q_ptr =
this;
738
739
740
741
742
743
744
745
746
747
748
749
750
751QCoreApplication::QCoreApplication(
int &argc,
char **argv
757 : d_ptr(
new QCoreApplicationPrivate(argc, argv))
759 : QObject(*
new QCoreApplicationPrivate(argc, argv))
762 d_func()->q_ptr =
this;
765 QCoreApplicationPrivate::eventDispatcher->startingUp();
770
771
772
773
774
776void Q_TRACE_INSTRUMENT(qtcore) QCoreApplicationPrivate::init()
778 Q_TRACE_SCOPE(QCoreApplicationPrivate_init);
780#if defined(Q_OS_MACOS)
781 QMacAutoReleasePool pool;
784 Q_Q(QCoreApplication);
786#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
787 initDebuggingConsole();
792 Q_ASSERT_X(!QCoreApplication::self,
"QCoreApplication",
"there should be only one application object");
793#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
794 QCoreApplication::self = q;
795 g_self.storeRelaxed(q);
797 QCoreApplication::self.storeRelaxed(q);
802 emscripten::val hardwareConcurrency = emscripten::val::global(
"navigator")[
"hardwareConcurrency"];
803 if (hardwareConcurrency.isUndefined())
804 QThreadPrivate::idealThreadCount = 2;
806 QThreadPrivate::idealThreadCount = hardwareConcurrency.as<
int>();
811 if (!coreappdata()->applicationNameSet)
812 coreappdata()->application = appName();
814 if (!coreappdata()->applicationVersionSet)
815 coreappdata()->applicationVersion = appVersion();
817#if defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
822 QLoggingRegistry::instance()->initializeRules();
825#if QT_CONFIG(library)
829 if (coreappdata->libPathsInitialized()) {
830 const QStringList appPaths = std::move(coreappdata->app_libpaths);
831 Q_ASSERT(!coreappdata->libPathsInitialized());
833 if (coreappdata->libPathsManuallySet()) {
834 const QStringList manualPaths = std::move(coreappdata->manual_libpaths);
835 Q_ASSERT(!coreappdata->libPathsManuallySet());
841 QStringList newPaths(q->libraryPaths());
842 for (qsizetype i = manualPaths.size(), j = appPaths.size(); i > 0 || j > 0; qt_noop()) {
844 newPaths.prepend(manualPaths[--i]);
845 }
else if (--i < 0) {
846 newPaths.removeAll(appPaths[j]);
847 }
else if (manualPaths[i] != appPaths[j]) {
848 newPaths.removeAll(appPaths[j]);
852 coreappdata->manual_libpaths.swap(newPaths);
859 Q_ASSERT(!eventDispatcher);
860 auto thisThreadData = threadData.loadRelaxed();
861 eventDispatcher = thisThreadData->eventDispatcher.loadRelaxed();
864 if (!eventDispatcher)
865 createEventDispatcher();
866 Q_ASSERT(eventDispatcher);
868 if (!eventDispatcher->parent()) {
869 eventDispatcher->moveToThread(thisThreadData->thread.loadAcquire());
870 eventDispatcher->setParent(q);
873 thisThreadData->eventDispatcher = eventDispatcher;
874 eventDispatcherReady();
877 processCommandLineArguments();
879 qt_call_pre_routines();
880 QT_MANGLE_NAMESPACE(qt_startup_hook)();
881#ifndef QT_BOOTSTRAPPED
882 QtPrivate::initBindingStatusThreadId();
883 if (Q_UNLIKELY(qtHookData[QHooks::Startup]))
884 reinterpret_cast<QHooks::StartupCallback>(qtHookData[QHooks::Startup])();
888 is_app_running =
true;
893
894
895QCoreApplication::~QCoreApplication()
897 preRoutinesCalled =
false;
899 qt_call_post_routines();
901#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
903 g_self.storeRelaxed(
nullptr);
905 self.storeRelaxed(
nullptr);
909 QCoreApplicationPrivate::is_app_closing =
true;
910 QCoreApplicationPrivate::is_app_running =
false;
915 QThreadPool *globalThreadPool =
nullptr;
917 globalThreadPool = QThreadPool::globalInstance();
921 if (globalThreadPool) {
922 globalThreadPool->waitForDone();
923 delete globalThreadPool;
928 d_func()->threadData.loadRelaxed()->eventDispatcher =
nullptr;
929 if (QCoreApplicationPrivate::eventDispatcher)
930 QCoreApplicationPrivate::eventDispatcher->closingDown();
931 QCoreApplicationPrivate::eventDispatcher =
nullptr;
934#if QT_CONFIG(library)
935 if (coreappdata.exists()) {
937 coreappdata->app_libpaths = QStringList();
938 coreappdata->manual_libpaths = QStringList();
939 Q_ASSERT(!coreappdata->libPathsManuallySet());
940 Q_ASSERT(!coreappdata->libPathsInitialized());
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968void QCoreApplication::setSetuidAllowed(
bool allow)
970 QCoreApplicationPrivate::setuidAllowed = allow;
974
975
976
977
978
979
980
981bool QCoreApplication::isSetuidAllowed()
983 return QCoreApplicationPrivate::setuidAllowed;
987
988
989
990
991
992
993
994
995
996void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute,
bool on)
1000 static_assert(Qt::AA_AttributeCount <=
sizeof(QCoreApplicationPrivate::attribs) * CHAR_BIT);
1003 QCoreApplicationPrivate::attribs |= 1 << attribute;
1005 QCoreApplicationPrivate::attribs &= ~(1 << attribute);
1006#if defined(QT_NO_QOBJECT)
1007 if (Q_UNLIKELY(qApp)) {
1009 if (Q_UNLIKELY(QCoreApplicationPrivate::is_app_running)) {
1011 switch (attribute) {
1012 case Qt::AA_PluginApplication:
1013 case Qt::AA_UseDesktopOpenGL:
1014 case Qt::AA_UseOpenGLES:
1015 case Qt::AA_UseSoftwareOpenGL:
1016#ifdef QT_BOOTSTRAPPED
1017 qWarning(
"Attribute %d must be set before QCoreApplication is created.",
1020 qWarning(
"Attribute Qt::%s must be set before QCoreApplication is created.",
1021 QMetaEnum::fromType<Qt::ApplicationAttribute>().valueToKey(attribute));
1031
1032
1033
1034
1035
1036bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute)
1038 return QCoreApplicationPrivate::testAttribute(attribute);
1041#ifndef QT_NO_QOBJECT
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1062bool QCoreApplication::isQuitLockEnabled()
1064 return quitLockEnabled;
1067static bool doNotify(QObject *, QEvent *);
1069void QCoreApplication::setQuitLockEnabled(
bool enabled)
1071 quitLockEnabled = enabled;
1075
1076
1077
1078
1079
1080
1081bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
1087 QObjectPrivate *d = receiver->d_func();
1088 QThreadData *threadData = d->threadData.loadAcquire();
1089 bool selfRequired = threadData->requiresCoreApplication;
1090 if (selfRequired && !
qApp)
1095 bool result =
false;
1096 void *cbdata[] = { receiver, event, &result };
1097 if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) {
1101 QScopedScopeLevelCounter scopeLevelCounter(threadData);
1103 return doNotify(receiver, event);
1105#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1106 if (!QThread::isMainThread())
1109 return qApp->notify(receiver, event);
1113
1114
1115
1116
1117
1118
1119bool QCoreApplication::forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent)
1121 if (event && originatingEvent)
1122 event->m_spont = originatingEvent->m_spont;
1124 return notifyInternal2(receiver, event);
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1180bool QCoreApplication::notify(QObject *receiver, QEvent *event)
1185#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1186 Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed()
1187 == QCoreApplicationPrivate::mainThread());
1191 if (QCoreApplicationPrivate::is_app_closing)
1193 return doNotify(receiver, event);
1196static bool doNotify(QObject *receiver, QEvent *event)
1201 if (receiver ==
nullptr) {
1202 qWarning(
"QCoreApplication::notify: Unexpected null receiver");
1207 QCoreApplicationPrivate::checkReceiverThread(receiver);
1210 return receiver->isWidgetType() ?
false : QCoreApplicationPrivate::notify_helper(receiver, event);
1213bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
1216 Q_ASSERT(QThread::isMainThread());
1220 for (qsizetype i = 0; i < extraData->eventFilters.size(); ++i) {
1221 QObject *obj = extraData->eventFilters.at(i);
1224 if (obj->d_func()->threadData.loadRelaxed() != threadData.loadRelaxed()) {
1225 qWarning(
"QCoreApplication: Application event filter cannot be in a different thread.");
1228 if (obj->eventFilter(receiver, event))
1235bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
1237 if (receiver !=
qApp && receiver->d_func()->extraData) {
1238 for (qsizetype i = 0; i < receiver->d_func()->extraData->eventFilters.size(); ++i) {
1239 QObject *obj = receiver->d_func()->extraData->eventFilters.at(i);
1242 if (obj->d_func()->threadData.loadRelaxed() != receiver->d_func()->threadData.loadRelaxed()) {
1243 qWarning(
"QCoreApplication: Object event filter cannot be in a different thread.");
1246 if (obj->eventFilter(receiver, event))
1254
1255
1256
1257
1258bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
1262 Q_TRACE(QCoreApplication_notify_entry, receiver, event, event->type());
1263 bool consumed =
false;
1264 bool filtered =
false;
1265 Q_TRACE_EXIT(QCoreApplication_notify_exit, consumed, filtered);
1268 if (QThread::isMainThread()
1269 && QCoreApplication::self
1270 && QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event)) {
1275 if (sendThroughObjectEventFilters(receiver, event)) {
1281 consumed = receiver->event(event);
1286
1287
1288
1289
1290
1292bool QCoreApplication::startingUp()
1294 return !QCoreApplicationPrivate::is_app_running;
1298
1299
1300
1301
1302
1304bool QCoreApplication::closingDown()
1306 return QCoreApplicationPrivate::is_app_closing;
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags)
1341 QThreadData *data = QThreadData::current();
1342 if (!data->hasEventDispatcher())
1344 data->eventDispatcher.loadRelaxed()->processEvents(flags);
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags,
int ms)
1361 QCoreApplication::processEvents(flags, QDeadlineTimer(ms));
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline)
1393 QThreadData *data = QThreadData::current();
1394 if (!data->hasEventDispatcher())
1397 while (data->eventDispatcher.loadRelaxed()->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) {
1398 if (deadline.hasExpired())
1404
1405
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433int QCoreApplication::exec()
1435 if (!QCoreApplicationPrivate::checkInstance(
"exec"))
1438 QThreadData *threadData = self->d_func()->threadData.loadAcquire();
1439 if (threadData != QThreadData::current()) {
1440 qWarning(
"%s::exec: Must be called from the main thread", self->metaObject()->className());
1443 if (!threadData->eventLoops.isEmpty()) {
1444 qWarning(
"QCoreApplication::exec: The event loop is already running");
1448 threadData->quitNow =
false;
1449 QEventLoop eventLoop;
1450 self->d_func()->in_exec =
true;
1451 self->d_func()->aboutToQuitEmitted =
false;
1452 int returnCode = eventLoop.exec(QEventLoop::ApplicationExec);
1453 threadData->quitNow =
false;
1456 self->d_func()->execCleanup();
1467void QCoreApplicationPrivate::execCleanup()
1469 threadData.loadRelaxed()->quitNow =
false;
1471 QCoreApplication::sendPostedEvents(
nullptr, QEvent::DeferredDelete);
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505void QCoreApplication::exit(
int returnCode)
1509 QCoreApplicationPrivate *d = self->d_func();
1510 if (!d->aboutToQuitEmitted) {
1511 emit self->aboutToQuit(QCoreApplication::QPrivateSignal());
1512 d->aboutToQuitEmitted =
true;
1514 QThreadData *data = d->threadData.loadRelaxed();
1515 data->quitNow =
true;
1516 for (qsizetype i = 0; i < data->eventLoops.size(); ++i) {
1517 QEventLoop *eventLoop = data->eventLoops.at(i);
1518 eventLoop->exit(returnCode);
1523
1524
1526#ifndef QT_NO_QOBJECT
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
1543 Q_ASSERT_X(receiver,
"QCoreApplication::sendEvent",
"Unexpected null receiver");
1544 Q_ASSERT_X(event,
"QCoreApplication::sendEvent",
"Unexpected null event");
1546 Q_TRACE(QCoreApplication_sendEvent, receiver, event, event->type());
1548 event->m_spont =
false;
1549 return notifyInternal2(receiver, event);
1553
1554
1555bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
1557 Q_ASSERT_X(receiver,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null receiver");
1558 Q_ASSERT_X(event,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null event");
1560 Q_TRACE(QCoreApplication_sendSpontaneousEvent, receiver, event, event->type());
1562 event->m_spont =
true;
1563 return notifyInternal2(receiver, event);
1568QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThreadPostEventList(QObject *object)
1570 QPostEventListLocker locker;
1573 locker.threadData = QThreadData::current();
1574 locker.locker = qt_unique_lock(locker.threadData->postEventList.mutex);
1578 auto &threadData = QObjectPrivate::get(object)->threadData;
1583 locker.threadData = threadData.loadAcquire();
1584 if (!locker.threadData) {
1589 auto temporaryLocker = qt_unique_lock(locker.threadData->postEventList.mutex);
1590 if (locker.threadData == threadData.loadAcquire()) {
1591 locker.locker = std::move(temporaryLocker);
1596 Q_ASSERT(locker.threadData);
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636void QCoreApplication::postEvent(QObject *receiver, QEvent *event,
int priority)
1638 Q_ASSERT_X(event,
"QCoreApplication::postEvent",
"Unexpected null event");
1640 Q_TRACE_SCOPE(QCoreApplication_postEvent, receiver, event, event->type());
1643 if (receiver ==
nullptr) {
1644 qWarning(
"QCoreApplication::postEvent: Unexpected null receiver");
1649 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1650 if (!locker.threadData) {
1656 QThreadData *data = locker.threadData;
1659 QT_WARNING_DISABLE_DEPRECATED
1661 if (receiver->d_func()->postedEvents.loadAcquire()
1662 && self && self->compressEvent(event, receiver, &data->postEventList)) {
1663 Q_TRACE(QCoreApplication_postEvent_event_compressed, receiver, event);
1670 std::unique_ptr<QEvent> eventDeleter(event);
1671 Q_TRACE(QCoreApplication_postEvent_event_posted, receiver, event, event->type());
1672 data->postEventList.addEvent(QPostEvent(receiver, event, priority));
1673 Q_UNUSED(eventDeleter.release());
1674 event->m_posted =
true;
1675 receiver->d_func()->postedEvents.fetchAndAddRelease(1);
1676 data->canWait =
false;
1679 QAbstractEventDispatcher* dispatcher = data->eventDispatcher.loadAcquire();
1681 dispatcher->wakeUp();
1685
1686
1687
1688#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1689bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1691 return d_func()->compressEvent(event, receiver, postedEvents);
1695bool QCoreApplicationPrivate::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1699 Q_ASSERT(postedEvents);
1702 if (event->type() == QEvent::Timer) {
1703 const int timerId =
static_cast<QTimerEvent *>(event)->timerId();
1704 auto it = postedEvents->cbegin();
1705 const auto end = postedEvents->cend();
1707 if (it->event && it->event->type() == QEvent::Timer && it->receiver == receiver) {
1708 if (
static_cast<QTimerEvent *>(it->event)->timerId() == timerId) {
1718 if (event->type() == QEvent::Quit) {
1719 for (
const QPostEvent &cur : std::as_const(*postedEvents)) {
1720 if (cur.receiver != receiver
1721 || cur.event ==
nullptr
1722 || cur.event->type() != event->type())
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750void QCoreApplication::sendPostedEvents(QObject *receiver,
int event_type)
1755 QThreadData *data = QThreadData::current();
1757 QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data);
1760void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver,
int event_type,
1763 if (event_type == -1) {
1768 if (receiver && receiver->d_func()->threadData.loadRelaxed() != data) {
1769 qWarning(
"QCoreApplication::sendPostedEvents: Cannot send "
1770 "posted events for objects in another thread");
1774 ++data->postEventList.recursion;
1776 auto locker = qt_unique_lock(data->postEventList.mutex);
1781 data->canWait = (data->postEventList.size() == 0);
1783 if (data->postEventList.size() == 0
1784 || (receiver && !receiver->d_func()->postedEvents.loadAcquire())) {
1785 --data->postEventList.recursion;
1789 data->canWait =
true;
1793 qsizetype startOffset = data->postEventList.startOffset;
1794 qsizetype &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset;
1795 data->postEventList.insertionOffset = data->postEventList.size();
1799 Q_DISABLE_COPY_MOVE(CleanUp)
1804 bool exceptionCaught;
1806 inline CleanUp(QObject *receiver,
int event_type, QThreadData *data) :
1807 receiver(receiver), event_type(event_type), data(data), exceptionCaught(
true)
1811 if (exceptionCaught) {
1813 data->canWait =
false;
1816 --data->postEventList.recursion;
1817 if (!data->postEventList.recursion && !data->canWait && data->hasEventDispatcher())
1818 data->eventDispatcher.loadRelaxed()->wakeUp();
1822 if (!event_type && !receiver && data->postEventList.startOffset >= 0) {
1823 const QPostEventList::iterator it = data->postEventList.begin();
1824 data->postEventList.erase(it, it + data->postEventList.startOffset);
1825 data->postEventList.insertionOffset -= data->postEventList.startOffset;
1826 Q_ASSERT(data->postEventList.insertionOffset >= 0);
1827 data->postEventList.startOffset = 0;
1831 CleanUp cleanup(receiver, event_type, data);
1833 while (i < data->postEventList.size()) {
1835 if (i >= data->postEventList.insertionOffset)
1838 const QPostEvent &pe = data->postEventList.at(i);
1843 if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) {
1844 data->canWait =
false;
1848 if (pe.event->type() == QEvent::DeferredDelete) {
1855 const int eventLoopLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->loopLevel();
1856 const int eventScopeLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->scopeLevel();
1858 const bool postedBeforeOutermostLoop = eventLoopLevel == 0;
1859 const bool allowDeferredDelete =
1860 (eventLoopLevel + eventScopeLevel > data->loopLevel + data->scopeLevel
1861 || (postedBeforeOutermostLoop && data->loopLevel > 0)
1862 || (event_type == QEvent::DeferredDelete
1863 && eventLoopLevel + eventScopeLevel == data->loopLevel + data->scopeLevel));
1864 if (!allowDeferredDelete) {
1866 if (!event_type && !receiver) {
1871 QPostEvent pe_copy = pe;
1875 const_cast<QPostEvent &>(pe).event =
nullptr;
1878 data->postEventList.addEvent(pe_copy);
1886 pe.event->m_posted =
false;
1887 QEvent *e = pe.event;
1888 QObject * r = pe.receiver;
1891 const auto previous = r->d_func()->postedEvents.fetchAndSubAcquire(1);
1892 Q_ASSERT(previous > 0);
1896 const_cast<QPostEvent &>(pe).event =
nullptr;
1899 const auto relocker = qScopeGuard([&locker] { locker.lock(); });
1901 const std::unique_ptr<QEvent> event_deleter(e);
1904 QCoreApplication::sendEvent(r, e);
1911 cleanup.exceptionCaught =
false;
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1933void QCoreApplication::removePostedEvents(QObject *receiver,
int eventType)
1935 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1936 QThreadData *data = locker.threadData;
1942 if (receiver && !receiver->d_func()->postedEvents.loadAcquire())
1947 QVarLengthArray<QEvent*> events;
1948 qsizetype n = data->postEventList.size();
1951 for (qsizetype i = 0; i < n; ++i) {
1952 const QPostEvent &pe = data->postEventList.at(i);
1954 if ((!receiver || pe.receiver == receiver)
1955 && (pe.event && (eventType == 0 || pe.event->type() == eventType))) {
1956 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
1957 pe.event->m_posted =
false;
1958 events.append(pe.event);
1959 const_cast<QPostEvent &>(pe).event =
nullptr;
1960 }
else if (!data->postEventList.recursion) {
1962 qSwap(data->postEventList[i], data->postEventList[j]);
1968 if (receiver && eventType == 0) {
1969 Q_ASSERT(!receiver->d_func()->postedEvents.loadRelaxed());
1973 if (!data->postEventList.recursion) {
1975 data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end());
1983
1984
1985
1986
1987
1988
1989
1990
1991
1993void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
1995 if (!event || !event->m_posted)
1998 QThreadData *data = QThreadData::current();
2000 const auto locker = qt_scoped_lock(data->postEventList.mutex);
2002 if (data->postEventList.size() == 0) {
2003#if defined(QT_DEBUG)
2004 qDebug(
"QCoreApplication::removePostedEvent: Internal error: %p %d is posted",
2005 (
void*)event, event->type());
2010 for (
const QPostEvent &pe : std::as_const(data->postEventList)) {
2011 if (pe.event == event) {
2013 qWarning(
"QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s",
2015 pe.receiver->metaObject()->className(),
2016 pe.receiver->objectName().toLocal8Bit().data());
2018 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
2019 pe.event->m_posted =
false;
2021 const_cast<QPostEvent &>(pe).event =
nullptr;
2028
2029
2030bool QCoreApplication::event(QEvent *e)
2032 if (e->type() == QEvent::Quit) {
2036 return QObject::event(e);
2039void QCoreApplicationPrivate::ref()
2044void QCoreApplicationPrivate::deref()
2046 quitLockRef.deref();
2048 if (quitLockEnabled && canQuitAutomatically())
2049 quitAutomatically();
2052bool QCoreApplicationPrivate::canQuitAutomatically()
2063 if (quitLockRef.loadRelaxed())
2069void QCoreApplicationPrivate::quitAutomatically()
2071 Q_Q(QCoreApplication);
2079 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116void QCoreApplication::quit()
2121 if (!self->d_func()->in_exec)
2124 self->d_func()->quit();
2127void QCoreApplicationPrivate::quit()
2129 Q_Q(QCoreApplication);
2131 if (QThread::isMainThread()) {
2132 QEvent quitEvent(QEvent::Quit);
2133 QCoreApplication::sendEvent(q, &quitEvent);
2135 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2162#ifndef QT_NO_TRANSLATION
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2194bool QCoreApplication::installTranslator(QTranslator *translationFile)
2196 if (!translationFile)
2199 if (!QCoreApplicationPrivate::checkInstance(
"installTranslator"))
2202 QCoreApplicationPrivate *d = self->d_func();
2204 QWriteLocker locker(&d->translateMutex);
2205 d->translators.prepend(translationFile);
2208 if (translationFile->isEmpty())
2211#ifndef QT_NO_QOBJECT
2212 QEvent ev(QEvent::LanguageChange);
2213 QCoreApplication::sendEvent(self, &ev);
2220
2221
2222
2223
2224
2225
2226
2227
2229bool QCoreApplication::removeTranslator(QTranslator *translationFile)
2231 if (!translationFile)
2233 if (!QCoreApplicationPrivate::checkInstance(
"removeTranslator"))
2235 QCoreApplicationPrivate *d = self->d_func();
2236 QWriteLocker locker(&d->translateMutex);
2237 if (d->translators.removeAll(translationFile)) {
2238#ifndef QT_NO_QOBJECT
2240 if (!self->closingDown()) {
2241 QEvent ev(QEvent::LanguageChange);
2242 QCoreApplication::sendEvent(self, &ev);
2253 qsizetype percentPos = 0;
2255 while ((percentPos = result->indexOf(u'%', percentPos + len)) != -1) {
2257 if (percentPos + len == result->size())
2260 if (result->at(percentPos + len) == u'L') {
2262 if (percentPos + len == result->size())
2268 if (result->at(percentPos + len) == u'n') {
2271 result->replace(percentPos, len, fmt);
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2312 const char *disambiguation,
int n)
2320 QCoreApplicationPrivate *d = self->d_func();
2321 QReadLocker locker(&d->translateMutex);
2322 if (!d->translators.isEmpty()) {
2323 QList<QTranslator*>::ConstIterator it;
2324 QTranslator *translationFile;
2325 for (it = d->translators.constBegin(); it != d->translators.constEnd(); ++it) {
2326 translationFile = *it;
2327 result = translationFile->translate(context, sourceText, disambiguation, n);
2328 if (!result.isNull())
2334 if (result.isNull())
2335 result = QString::fromUtf8(sourceText);
2337 replacePercentN(&result, n);
2344 return QCoreApplication::translate(
nullptr, id,
nullptr, n);
2347bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
2349 if (!QCoreApplication::self)
2351 QCoreApplicationPrivate *d = QCoreApplication::self->d_func();
2352 QReadLocker locker(&d->translateMutex);
2353 return d->translators.contains(translator);
2358QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2359 const char *disambiguation,
int n)
2362 Q_UNUSED(disambiguation);
2363 QString ret = QString::fromUtf8(sourceText);
2365 ret.replace(
"%n"_L1, QString::number(n));
2371#ifndef QT_BOOTSTRAPPED
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395QString QCoreApplication::applicationDirPath()
2398 qWarning(
"QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
2402 QFileSystemEntry appFilePath(applicationFilePath(), QFileSystemEntry::FromInternalPath{});
2403 return appFilePath.isEmpty() ? QString() : appFilePath.path();
2406#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN)
2410# if defined(Q_OS_ANDROID)
2413# elif defined(Q_OS_LINUX)
2415 return QFile::decodeName(qt_readlink(
"/proc/self/exe"));
2416# elif defined(AT_EXECPATH)
2418 char execfn[PATH_MAX];
2419 if (elf_aux_info(AT_EXECPATH, execfn,
sizeof(execfn)) != 0)
2422 qsizetype len = qstrlen(execfn);
2423 return QFile::decodeName(QByteArray::fromRawData(execfn, len));
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446QString QCoreApplication::applicationFilePath()
2449 qWarning(
"QCoreApplication::applicationFilePath: Please instantiate the QApplication object first");
2453 QCoreApplicationPrivate *d = self->d_func();
2456 static QByteArray procName = QByteArray(d->argv[0]);
2457 if (procName != QByteArrayView(d->argv[0])) {
2459 d->cachedApplicationFilePath = QString();
2460 procName.assign(d->argv[0]);
2464 if (!d->cachedApplicationFilePath.isNull())
2465 return d->cachedApplicationFilePath;
2467 QString absPath = qAppFileName();
2468 if (Q_LIKELY(!absPath.isEmpty())) {
2470 return d->cachedApplicationFilePath = std::move(absPath);
2473 if (
const QStringList args = arguments(); !args.isEmpty()) {
2474 const QString &argv0 = args[0];
2476 if (!argv0.isEmpty() && argv0.at(0) == u'/') {
2478
2479
2480
2482 }
else if (argv0.contains(u'/')) {
2484
2485
2486
2487 absPath = QDir::current().absoluteFilePath(argv0);
2490
2491
2492
2493 absPath = QStandardPaths::findExecutable(argv0);
2497 absPath = QFileInfo(absPath).canonicalFilePath();
2498 if (!absPath.isEmpty()) {
2499 return d->cachedApplicationFilePath = std::move(absPath);
2506
2507
2508
2509
2510qint64 QCoreApplication::applicationPid()
noexcept
2512#if defined(Q_OS_WIN)
2513 return GetCurrentProcessId();
2514#elif defined(Q_OS_VXWORKS)
2515 return (pid_t) taskIdCurrent;
2522static QStringList winCmdArgs()
2531 if (
wchar_t **argv = CommandLineToArgvW(GetCommandLine(), &size)) {
2532 result.reserve(size);
2533 wchar_t **argvEnd = argv + size;
2534 for (
wchar_t **a = argv; a < argvEnd; ++a)
2535 result.append(QString::fromWCharArray(*a));
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2574QStringList QCoreApplication::arguments()
2579 qWarning(
"QCoreApplication::arguments: Please instantiate the QApplication object first");
2583 const QCoreApplicationPrivate *d = self->d_func();
2585 const int argc = d->argc;
2586 char **
const argv = d->argv;
2589#if defined(Q_OS_WIN)
2590 const bool argsModifiedByUser = d->origArgv ==
nullptr;
2591 if (!argsModifiedByUser) {
2592 QStringList commandLineArguments = winCmdArgs();
2597 if (argc != d->origArgc) {
2604 for (
int i = 0; i < d->origArgc; ++i) {
2605 if (!contains(argc, argv, d->origArgv[i]))
2606 commandLineArguments.removeAll(QString::fromLocal8Bit(d->origArgv[i]));
2610 return commandLineArguments;
2614 for (
int a = 0; a < argc; ++a)
2615 list << QString::fromLocal8Bit(argv[a]);
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2637
2638
2639
2640
2641
2642
2643
2644void QCoreApplication::setOrganizationName(
const QString &orgName)
2646 if (coreappdata()->orgName == orgName)
2648 coreappdata()->orgName = orgName;
2649#ifndef QT_NO_QOBJECT
2650 if (QCoreApplication::self)
2651 emit QCoreApplication::self->organizationNameChanged();
2655QString QCoreApplication::organizationName()
2657 return coreappdata()->orgName;
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2676
2677
2678
2679
2680
2681void QCoreApplication::setOrganizationDomain(
const QString &orgDomain)
2683 if (coreappdata()->orgDomain == orgDomain)
2685 coreappdata()->orgDomain = orgDomain;
2686#ifndef QT_NO_QOBJECT
2687 if (QCoreApplication::self)
2688 emit QCoreApplication::self->organizationDomainChanged();
2692QString QCoreApplication::organizationDomain()
2694 return coreappdata()->orgDomain;
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2714
2715
2716
2717
2718
2719void QCoreApplication::setApplicationName(
const QString &application)
2721 coreappdata()->applicationNameSet = !application.isEmpty();
2722 QString newAppName = application;
2723 if (newAppName.isEmpty() && QCoreApplication::self)
2724 newAppName = QCoreApplication::self->d_func()->appName();
2725 if (coreappdata()->application == newAppName)
2727 coreappdata()->application = newAppName;
2728#ifndef QT_NO_QOBJECT
2729 if (QCoreApplication::self)
2730 emit QCoreApplication::self->applicationNameChanged();
2734QString QCoreApplication::applicationName()
2736 return coreappdata() ? coreappdata()->application : QString();
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2767
2768
2769
2770
2771
2772void QCoreApplication::setApplicationVersion(
const QString &version)
2774 coreappdata()->applicationVersionSet = !version.isEmpty();
2775 QString newVersion = version;
2776 if (newVersion.isEmpty() && QCoreApplication::self)
2777 newVersion = QCoreApplication::self->d_func()->appVersion();
2778 if (coreappdata()->applicationVersion == newVersion)
2780 coreappdata()->applicationVersion = newVersion;
2781#ifndef QT_NO_QOBJECT
2782 if (QCoreApplication::self)
2783 emit QCoreApplication::self->applicationVersionChanged();
2787QString QCoreApplication::applicationVersion()
2789 return coreappdata() ? coreappdata()->applicationVersion : QString();
2792#if QT_CONFIG(permissions) || defined(Q_QDOC)
2795
2796
2797
2798
2799
2800
2801
2802
2803Qt::PermissionStatus QCoreApplication::checkPermission(
const QPermission &permission)
2805 return QPermissions::Private::checkPermission(permission);
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2875
2876
2877
2878
2879
2880
2881
2882void QCoreApplication::requestPermissionImpl(
const QPermission &requestedPermission,
2883 QtPrivate::QSlotObjectBase *slotObjRaw,
const QObject *context)
2885 QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw};
2888 if (!QThread::isMainThread()) {
2889 qCWarning(lcPermissions,
"Permissions can only be requested from the GUI (main) thread");
2893 class PermissionReceiver :
public QObject
2896 explicit PermissionReceiver(QtPrivate::SlotObjUniquePtr &&slotObject,
const QObject *context)
2897 : slotObject(std::move(slotObject)), context(context ? context :
this)
2899 Q_ASSERT(
this->context);
2900 moveToThread(
this->context->thread());
2903 void finalizePermissionRequest(
const QPermission &permission)
2905 Q_ASSERT(slotObject);
2908 void *args[] = {
nullptr,
const_cast<QPermission *>(&permission) };
2909 slotObject->call(
const_cast<QObject *>(context.data()), args);
2914 QtPrivate::SlotObjUniquePtr slotObject;
2915 QPointer<
const QObject> context;
2919 auto receiver = std::make_shared<PermissionReceiver>(std::move(slotObj), context);
2921 QPermissions::Private::requestPermission(requestedPermission,
2922 [=, receiver = std::move(receiver)](Qt::PermissionStatus status)
mutable {
2923 if (status == Qt::PermissionStatus::Undetermined) {
2924 Q_ASSERT_X(
false,
"QPermission",
2925 "Internal error: requestPermission() should never return Undetermined");
2926 status = Qt::PermissionStatus::Denied;
2929 if (QCoreApplication::self) {
2930 QPermission permission = requestedPermission;
2931 permission.m_status = status;
2932 auto receiverObject = receiver.get();
2933 QMetaObject::invokeMethod(receiverObject,
2934 [receiver = std::move(receiver), permission] {
2935 receiver->finalizePermissionRequest(permission);
2936 }, Qt::QueuedConnection);
2943#if QT_CONFIG(library)
2944static QStringList libraryPathsLocked();
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975QStringList QCoreApplication::libraryPaths()
2977 QMutexLocker locker(&coreappdata->libraryPathMutex);
2978 return libraryPathsLocked();
2982
2983
2984static QStringList libraryPathsLocked()
2986 QCoreApplicationData *d = coreappdata;
2987 if (d->libPathsManuallySet())
2988 return d->manual_libpaths;
2990 QStringList *app_libpaths = &d->app_libpaths;
2991 if (!d->libPathsInitialized()) {
2993 auto setPathsFromEnv = [&](QString libPathEnv) {
2994 if (!libPathEnv.isEmpty()) {
2995 QStringList paths = libPathEnv.split(QDir::listSeparator(), Qt::SkipEmptyParts);
2996 for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
2997 QString canonicalPath = QDir(*it).canonicalPath();
2998 if (!canonicalPath.isEmpty()
2999 && !app_libpaths->contains(canonicalPath)) {
3000 app_libpaths->append(canonicalPath);
3005 setPathsFromEnv(qEnvironmentVariable(
"QT_PLUGIN_PATH"));
3011 if (CFBundleRef bundleRef = CFBundleGetMainBundle()) {
3012 if (QCFType<CFURLRef> urlRef = CFBundleCopyBuiltInPlugInsURL(bundleRef)) {
3013 if (QCFType<CFURLRef> absoluteUrlRef = CFURLCopyAbsoluteURL(urlRef)) {
3014 if (QCFString path = CFURLCopyFileSystemPath(absoluteUrlRef, kCFURLPOSIXPathStyle)) {
3015 if (QFile::exists(path)) {
3016 path = QDir(path).canonicalPath();
3017 if (!app_libpaths->contains(path))
3018 app_libpaths->append(path);
3026 QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
3027 if (QFile::exists(installPathPlugins)) {
3029 installPathPlugins = QDir(installPathPlugins).canonicalPath();
3030 if (!app_libpaths->contains(installPathPlugins))
3031 app_libpaths->append(installPathPlugins);
3037 QString app_location = QCoreApplication::applicationFilePath();
3038 app_location.truncate(app_location.lastIndexOf(u'/'));
3039 app_location = QDir(app_location).canonicalPath();
3040 if (QFile::exists(app_location) && !app_libpaths->contains(app_location))
3041 app_libpaths->append(app_location);
3043 if (app_libpaths->isEmpty())
3044 app_libpaths->reserve(1);
3045 Q_ASSERT(d->libPathsInitialized());
3047 return *app_libpaths;
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064void QCoreApplication::setLibraryPaths(
const QStringList &paths)
3066 QCoreApplicationData *d = coreappdata;
3067 QMutexLocker locker(&d->libraryPathMutex);
3072 if (!d->libPathsInitialized())
3073 libraryPathsLocked();
3075 d->manual_libpaths = paths;
3076 if (d->manual_libpaths.isEmpty())
3077 d->manual_libpaths.reserve(1);
3078 Q_ASSERT(d->libPathsManuallySet());
3081 QFactoryLoader::refreshAll();
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100void QCoreApplication::addLibraryPath(
const QString &path)
3105 QString canonicalPath = QDir(path).canonicalPath();
3106 if (canonicalPath.isEmpty())
3109 QCoreApplicationData *d = coreappdata;
3110 QMutexLocker locker(&d->libraryPathMutex);
3112 QStringList *libpaths = &d->manual_libpaths;
3113 if (d->libPathsManuallySet()) {
3114 if (d->manual_libpaths.contains(canonicalPath))
3118 libraryPathsLocked();
3119 QStringList *app_libpaths = &d->app_libpaths;
3120 if (app_libpaths->contains(canonicalPath))
3123 *libpaths = *app_libpaths;
3126 libpaths->prepend(canonicalPath);
3127 Q_ASSERT(d->libPathsManuallySet());
3129 QFactoryLoader::refreshAll();
3133
3134
3135
3136
3137
3138
3139
3140
3141void QCoreApplication::removeLibraryPath(
const QString &path)
3146 QString canonicalPath = QDir(path).canonicalPath();
3147 if (canonicalPath.isEmpty())
3150 QCoreApplicationData *d = coreappdata;
3151 QMutexLocker locker(&d->libraryPathMutex);
3153 QStringList *libpaths = &d->manual_libpaths;
3154 if (d->libPathsManuallySet()) {
3155 if (libpaths->removeAll(canonicalPath) == 0)
3159 libraryPathsLocked();
3160 QStringList *app_libpaths = &d->app_libpaths;
3161 if (!app_libpaths->contains(canonicalPath))
3164 *libpaths = *app_libpaths;
3165 libpaths->removeAll(canonicalPath);
3166 Q_ASSERT(d->libPathsManuallySet());
3170 QFactoryLoader::refreshAll();
3175#ifndef QT_NO_QOBJECT
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
3208 if (QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
3209 qWarning(
"Native event filters are not applied when the Qt::AA_PluginApplication attribute is set");
3213 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3214 if (!filterObj || !eventDispatcher)
3216 eventDispatcher->installNativeEventFilter(filterObj);
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232void QCoreApplication::removeNativeEventFilter(QAbstractNativeEventFilter *filterObject)
3234 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3235 if (!filterObject || !eventDispatcher)
3237 eventDispatcher->removeNativeEventFilter(filterObject);
3241
3242
3243
3244QAbstractEventDispatcher *QCoreApplication::eventDispatcher()
3246 if (QCoreApplicationPrivate::theMainThread.loadAcquire())
3247 return QCoreApplicationPrivate::theMainThread.loadRelaxed()->eventDispatcher();
3252
3253
3254
3255
3256
3257void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
3259 QThread *mainThread = QCoreApplicationPrivate::theMainThread.loadAcquire();
3261 mainThread = QThread::currentThread();
3262 mainThread->setEventDispatcher(eventDispatcher);
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3376void *QCoreApplication::resolveInterface(
const char *name,
int revision)
const
3378#if defined(Q_OS_ANDROID)
3383 using namespace QNativeInterface;
3384 struct AndroidApplication :
public QAndroidApplication {};
3385 static AndroidApplication androidApplication;
3386 QT_NATIVE_INTERFACE_RETURN_IF(QAndroidApplication, &androidApplication);
3388 Q_UNUSED(name); Q_UNUSED(revision);
3394#ifndef QT_NO_QOBJECT
3395#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