2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
2015-04-16 22:44:16 +00:00
|
|
|
** Copyright (C) 2015 Intel Corporation.
|
2016-01-15 07:08:27 +00:00
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qcoreapplication.h"
|
|
|
|
#include "qcoreapplication_p.h"
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "qabstracteventdispatcher.h"
|
|
|
|
#include "qcoreevent.h"
|
|
|
|
#include "qeventloop.h"
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "qcorecmdlineargs_p.h"
|
|
|
|
#include <qdatastream.h>
|
|
|
|
#include <qdebug.h>
|
|
|
|
#include <qdir.h>
|
|
|
|
#include <qfile.h>
|
|
|
|
#include <qfileinfo.h>
|
2013-09-30 06:39:03 +00:00
|
|
|
#include <qmutex.h>
|
2014-02-04 15:35:26 +00:00
|
|
|
#include <private/qloggingregistry_p.h>
|
2011-10-21 18:57:31 +00:00
|
|
|
#include <qstandardpaths.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <qtextcodec.h>
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <qthread.h>
|
|
|
|
#include <qthreadpool.h>
|
|
|
|
#include <qthreadstorage.h>
|
|
|
|
#include <private/qthread_p.h>
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <qelapsedtimer.h>
|
|
|
|
#include <qlibraryinfo.h>
|
|
|
|
#include <qvarlengtharray.h>
|
|
|
|
#include <private/qfactoryloader_p.h>
|
|
|
|
#include <private/qfunctions_p.h>
|
|
|
|
#include <private/qlocale_p.h>
|
2014-04-15 11:11:40 +00:00
|
|
|
#include <private/qhooks_p.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2012-01-23 20:21:40 +00:00
|
|
|
#if defined(Q_OS_UNIX)
|
2015-11-23 06:13:00 +00:00
|
|
|
# if defined(Q_OS_OSX)
|
|
|
|
# include "qeventdispatcher_cf_p.h"
|
|
|
|
# else
|
|
|
|
# if !defined(QT_NO_GLIB)
|
|
|
|
# include "qeventdispatcher_glib_p.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
# endif
|
2015-10-15 13:24:54 +00:00
|
|
|
# endif
|
|
|
|
# include "qeventdispatcher_unix_p.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_WIN
|
2013-09-20 12:06:33 +00:00
|
|
|
# ifdef Q_OS_WINRT
|
|
|
|
# include "qeventdispatcher_winrt_p.h"
|
2015-01-16 10:29:54 +00:00
|
|
|
# include "qfunctions_winrt.h"
|
|
|
|
# include <wrl.h>
|
|
|
|
# include <Windows.ApplicationModel.core.h>
|
|
|
|
using namespace ABI::Windows::ApplicationModel::Core;
|
|
|
|
using namespace Microsoft::WRL;
|
2013-09-20 12:06:33 +00:00
|
|
|
# else
|
2011-04-27 10:05:43 +00:00
|
|
|
# include "qeventdispatcher_win_p.h"
|
2013-09-20 12:06:33 +00:00
|
|
|
# endif
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif // QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
# include "qcore_mac_p.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
# include <locale.h>
|
2012-12-06 19:02:08 +00:00
|
|
|
# include <unistd.h>
|
2014-01-01 16:58:19 +00:00
|
|
|
# include <sys/types.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef Q_OS_VXWORKS
|
|
|
|
# include <taskLib.h>
|
|
|
|
#endif
|
|
|
|
|
2013-09-03 20:09:30 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
class QMutexUnlocker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline explicit QMutexUnlocker(QMutex *m)
|
|
|
|
: mtx(m)
|
|
|
|
{ }
|
|
|
|
inline ~QMutexUnlocker() { unlock(); }
|
|
|
|
inline void unlock() { if (mtx) mtx->unlock(); mtx = 0; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QMutexUnlocker)
|
|
|
|
|
|
|
|
QMutex *mtx;
|
|
|
|
};
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-10-21 07:55:40 +00:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
2011-04-27 10:05:43 +00:00
|
|
|
extern QString qAppFileName();
|
|
|
|
#endif
|
|
|
|
|
2011-11-02 14:23:50 +00:00
|
|
|
#if QT_VERSION >= 0x060000
|
|
|
|
# error "Bump QCoreApplicatoinPrivate::app_compile_version to 0x060000"
|
|
|
|
#endif
|
|
|
|
int QCoreApplicationPrivate::app_compile_version = 0x050000; //we don't know exactly, but it's at least 5.0.0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-01-01 16:58:19 +00:00
|
|
|
bool QCoreApplicationPrivate::setuidAllowed = false;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#if !defined(Q_OS_WIN)
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
QString QCoreApplicationPrivate::macMenuBarName()
|
|
|
|
{
|
|
|
|
QString bundleName;
|
|
|
|
CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), CFSTR("CFBundleName"));
|
|
|
|
if (string)
|
|
|
|
bundleName = QCFString::toQString(static_cast<CFStringRef>(string));
|
|
|
|
return bundleName;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
QString QCoreApplicationPrivate::appName() const
|
|
|
|
{
|
2013-10-08 20:41:20 +00:00
|
|
|
QString applicationName;
|
2011-04-27 10:05:43 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2013-10-08 20:41:20 +00:00
|
|
|
applicationName = macMenuBarName();
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
2013-10-08 20:41:20 +00:00
|
|
|
if (applicationName.isEmpty() && argv[0]) {
|
|
|
|
char *p = strrchr(argv[0], '/');
|
|
|
|
applicationName = QString::fromLocal8Bit(p ? p + 1 : argv[0]);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-09-25 15:18:38 +00:00
|
|
|
|
|
|
|
return applicationName;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-22 13:31:05 +00:00
|
|
|
QString *QCoreApplicationPrivate::cachedApplicationFilePath = 0;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
bool QCoreApplicationPrivate::checkInstance(const char *function)
|
|
|
|
{
|
|
|
|
bool b = (QCoreApplication::self != 0);
|
|
|
|
if (!b)
|
|
|
|
qWarning("QApplication::%s: Please instantiate the QApplication object first", function);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2011-09-19 08:56:12 +00:00
|
|
|
void QCoreApplicationPrivate::processCommandLineArguments()
|
|
|
|
{
|
|
|
|
int j = argc ? 1 : 0;
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (argv[i] && *argv[i] != '-') {
|
|
|
|
argv[j++] = argv[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QByteArray arg = argv[i];
|
2014-01-31 08:49:58 +00:00
|
|
|
if (arg.startsWith("--"))
|
|
|
|
arg.remove(0, 1);
|
2011-09-19 08:56:12 +00:00
|
|
|
if (arg.startsWith("-qmljsdebugger=")) {
|
|
|
|
qmljs_debug_arguments = QString::fromLocal8Bit(arg.right(arg.length() - 15));
|
2012-10-11 09:15:03 +00:00
|
|
|
} else if (arg == "-qmljsdebugger" && i < argc - 1) {
|
|
|
|
++i;
|
|
|
|
qmljs_debug_arguments = QString::fromLocal8Bit(argv[i]);
|
2011-09-19 08:56:12 +00:00
|
|
|
} else {
|
|
|
|
argv[j++] = argv[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j < argc) {
|
|
|
|
argv[j] = 0;
|
|
|
|
argc = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// Support for introspection
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
QSignalSpyCallbackSet Q_CORE_EXPORT qt_signal_spy_callback_set = { 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
void qt_register_signal_spy_callbacks(const QSignalSpyCallbackSet &callback_set)
|
|
|
|
{
|
|
|
|
qt_signal_spy_callback_set = callback_set;
|
|
|
|
}
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
extern "C" void Q_CORE_EXPORT qt_startup_hook()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-14 11:58:31 +00:00
|
|
|
typedef QList<QtStartUpFunction> QStartUpFuncList;
|
|
|
|
Q_GLOBAL_STATIC(QStartUpFuncList, preRList)
|
2011-04-27 10:05:43 +00:00
|
|
|
typedef QList<QtCleanUpFunction> QVFuncList;
|
|
|
|
Q_GLOBAL_STATIC(QVFuncList, postRList)
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2013-01-14 11:58:31 +00:00
|
|
|
static QBasicMutex globalPreRoutinesMutex;
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2013-01-14 11:58:31 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
|
|
|
|
Adds a global routine that will be called from the QCoreApplication
|
|
|
|
constructor. The public API is Q_COREAPP_STARTUP_FUNCTION.
|
|
|
|
*/
|
|
|
|
void qAddPreRoutine(QtStartUpFunction p)
|
|
|
|
{
|
|
|
|
QStartUpFuncList *list = preRList();
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
// Due to C++11 parallel dynamic initialization, this can be called
|
|
|
|
// from multiple threads.
|
|
|
|
#ifndef QT_NO_THREAD
|
|
|
|
QMutexLocker locker(&globalPreRoutinesMutex);
|
|
|
|
#endif
|
|
|
|
if (QCoreApplication::instance())
|
|
|
|
p();
|
|
|
|
list->prepend(p); // in case QCoreApplication is re-created, see qt_call_pre_routines
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void qAddPostRoutine(QtCleanUpFunction p)
|
|
|
|
{
|
|
|
|
QVFuncList *list = postRList();
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
list->prepend(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qRemovePostRoutine(QtCleanUpFunction p)
|
|
|
|
{
|
|
|
|
QVFuncList *list = postRList();
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
list->removeAll(p);
|
|
|
|
}
|
|
|
|
|
2013-01-14 11:58:31 +00:00
|
|
|
static void qt_call_pre_routines()
|
|
|
|
{
|
|
|
|
QStartUpFuncList *list = preRList();
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
#ifndef QT_NO_THREAD
|
|
|
|
QMutexLocker locker(&globalPreRoutinesMutex);
|
|
|
|
#endif
|
|
|
|
// Unlike qt_call_post_routines, we don't empty the list, because
|
|
|
|
// Q_COREAPP_STARTUP_FUNCTION is a macro, so the user expects
|
|
|
|
// the function to be executed every time QCoreApplication is created.
|
|
|
|
for (int i = 0; i < list->count(); ++i)
|
|
|
|
list->at(i)();
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void Q_CORE_EXPORT qt_call_post_routines()
|
|
|
|
{
|
|
|
|
QVFuncList *list = 0;
|
|
|
|
QT_TRY {
|
|
|
|
list = postRList();
|
|
|
|
} QT_CATCH(const std::bad_alloc &) {
|
|
|
|
// ignore - if we can't allocate a post routine list,
|
|
|
|
// there's a high probability that there's no post
|
|
|
|
// routine to be executed :)
|
|
|
|
}
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
while (!list->isEmpty())
|
|
|
|
(list->takeFirst())();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
// initialized in qcoreapplication and in qtextstream autotest when setlocale is called.
|
|
|
|
static bool qt_locale_initialized = false;
|
|
|
|
|
|
|
|
#ifndef QT_NO_QOBJECT
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// app starting up if false
|
|
|
|
bool QCoreApplicationPrivate::is_app_running = false;
|
|
|
|
// app closing down if true
|
|
|
|
bool QCoreApplicationPrivate::is_app_closing = false;
|
|
|
|
|
|
|
|
Q_CORE_EXPORT uint qGlobalPostedEventsCount()
|
|
|
|
{
|
|
|
|
QThreadData *currentThreadData = QThreadData::current();
|
|
|
|
return currentThreadData->postEventList.size() - currentThreadData->postEventList.startOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAbstractEventDispatcher *QCoreApplicationPrivate::eventDispatcher = 0;
|
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
Qt::HANDLE qt_application_thread_id = 0;
|
|
|
|
#endif
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif // QT_NO_QOBJECT
|
|
|
|
|
|
|
|
QCoreApplication *QCoreApplication::self = 0;
|
|
|
|
uint QCoreApplicationPrivate::attribs = (1 << Qt::AA_SynthesizeMouseForUnhandledTouchEvents);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
struct QCoreApplicationData {
|
2015-01-08 14:08:22 +00:00
|
|
|
QCoreApplicationData() Q_DECL_NOTHROW {
|
2015-03-29 20:09:56 +00:00
|
|
|
applicationNameSet = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
~QCoreApplicationData() {
|
2013-03-21 15:24:36 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
|
|
|
// cleanup the QAdoptedThread created for the main() thread
|
|
|
|
if (QCoreApplicationPrivate::theMainThread) {
|
|
|
|
QThreadData *data = QThreadData::get2(QCoreApplicationPrivate::theMainThread);
|
|
|
|
data->deref(); // deletes the data and the adopted thread
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2012-10-09 17:34:29 +00:00
|
|
|
|
2015-02-22 18:35:43 +00:00
|
|
|
QString orgName, orgDomain;
|
|
|
|
QString application; // application name, initially from argv[0], can then be modified.
|
2011-04-27 10:05:43 +00:00
|
|
|
QString applicationVersion;
|
2015-03-29 20:09:56 +00:00
|
|
|
bool applicationNameSet; // true if setApplicationName was called
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifndef QT_NO_LIBRARY
|
2015-03-26 09:20:11 +00:00
|
|
|
QScopedPointer<QStringList> app_libpaths;
|
|
|
|
QScopedPointer<QStringList> manual_libpaths;
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata)
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-10-26 11:29:51 +00:00
|
|
|
static bool quitLockRefEnabled = true;
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-10-26 11:29:51 +00:00
|
|
|
|
2015-01-09 13:52:02 +00:00
|
|
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
|
|
|
// Check whether the command line arguments match those passed to main()
|
|
|
|
// by comparing to the global __argv/__argc (MS extension).
|
|
|
|
// Deep comparison is required since argv/argc is rebuilt by WinMain for
|
|
|
|
// GUI apps or when using MinGW due to its globbing.
|
|
|
|
static inline bool isArgvModified(int argc, char **argv)
|
|
|
|
{
|
2015-07-10 07:34:46 +00:00
|
|
|
if (__argc != argc || !__argv /* wmain() */)
|
2015-01-09 13:52:02 +00:00
|
|
|
return true;
|
|
|
|
if (__argv == argv)
|
|
|
|
return false;
|
|
|
|
for (int a = 0; a < argc; ++a) {
|
|
|
|
if (argv[a] != __argv[a] && strcmp(argv[a], __argv[a]))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool contains(int argc, char **argv, const char *needle)
|
|
|
|
{
|
|
|
|
for (int a = 0; a < argc; ++a) {
|
|
|
|
if (!strcmp(argv[a], needle))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif // Q_OS_WIN && !Q_OS_WINRT
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplicationPrivate::QCoreApplicationPrivate(int &aargc, char **aargv, uint flags)
|
2012-12-06 19:02:08 +00:00
|
|
|
:
|
|
|
|
#ifndef QT_NO_QOBJECT
|
|
|
|
QObjectPrivate(),
|
|
|
|
#endif
|
|
|
|
argc(aargc)
|
2012-05-16 07:39:39 +00:00
|
|
|
, argv(aargv)
|
2015-01-09 13:52:02 +00:00
|
|
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
2015-02-09 16:34:06 +00:00
|
|
|
, origArgc(0)
|
|
|
|
, origArgv(Q_NULLPTR)
|
2012-05-16 07:39:39 +00:00
|
|
|
#endif
|
2013-07-04 13:44:53 +00:00
|
|
|
, application_type(QCoreApplicationPrivate::Tty)
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2012-05-16 07:39:39 +00:00
|
|
|
, in_exec(false)
|
|
|
|
, aboutToQuitEmitted(false)
|
|
|
|
, threadData_clean(false)
|
2012-12-06 19:02:08 +00:00
|
|
|
#else
|
|
|
|
, q_ptr(0)
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
app_compile_version = flags & 0xffffff;
|
|
|
|
static const char *const empty = "";
|
|
|
|
if (argc == 0 || argv == 0) {
|
|
|
|
argc = 0;
|
2015-03-06 06:25:45 +00:00
|
|
|
argv = const_cast<char **>(&empty);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2015-01-09 13:52:02 +00:00
|
|
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
2015-02-09 16:34:06 +00:00
|
|
|
if (!isArgvModified(argc, argv)) {
|
|
|
|
origArgc = argc;
|
|
|
|
origArgv = new char *[argc];
|
|
|
|
std::copy(argv, argv + argc, origArgv);
|
|
|
|
}
|
2015-01-09 13:52:02 +00:00
|
|
|
#endif // Q_OS_WIN && !Q_OS_WINRT
|
2012-12-06 19:02:08 +00:00
|
|
|
|
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplicationPrivate::is_app_closing = false;
|
|
|
|
|
2014-01-01 16:58:19 +00:00
|
|
|
# if defined(Q_OS_UNIX)
|
2015-11-12 09:16:22 +00:00
|
|
|
if (Q_UNLIKELY(!setuidAllowed && (geteuid() != getuid())))
|
2014-01-01 16:58:19 +00:00
|
|
|
qFatal("FATAL: The application binary appears to be running setuid, this is a security hole.");
|
|
|
|
# endif // Q_OS_UNIX
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
# if defined(Q_OS_UNIX)
|
2011-04-27 10:05:43 +00:00
|
|
|
qt_application_thread_id = QThread::currentThreadId();
|
2012-12-06 19:02:08 +00:00
|
|
|
# endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-08-01 13:50:00 +00:00
|
|
|
QThread *cur = QThread::currentThread(); // note: this may end up setting theMainThread!
|
|
|
|
if (cur != theMainThread)
|
2011-04-27 10:05:43 +00:00
|
|
|
qWarning("WARNING: QApplication was not created in the main() thread.");
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QCoreApplicationPrivate::~QCoreApplicationPrivate()
|
|
|
|
{
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-06-07 15:25:22 +00:00
|
|
|
cleanupThreadData();
|
2015-02-09 16:34:06 +00:00
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
|
|
|
delete [] origArgv;
|
2012-05-16 07:39:39 +00:00
|
|
|
#endif
|
2013-11-22 13:31:05 +00:00
|
|
|
QCoreApplicationPrivate::clearApplicationFilePath();
|
2011-06-07 15:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
|
|
|
|
2011-06-07 15:25:22 +00:00
|
|
|
void QCoreApplicationPrivate::cleanupThreadData()
|
|
|
|
{
|
|
|
|
if (threadData && !threadData_clean) {
|
2011-04-27 10:05:43 +00:00
|
|
|
#ifndef QT_NO_THREAD
|
|
|
|
void *data = &threadData->tls;
|
|
|
|
QThreadStorageData::finish((void **)data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// need to clear the state of the mainData, just in case a new QCoreApplication comes along.
|
|
|
|
QMutexLocker locker(&threadData->postEventList.mutex);
|
|
|
|
for (int i = 0; i < threadData->postEventList.size(); ++i) {
|
|
|
|
const QPostEvent &pe = threadData->postEventList.at(i);
|
|
|
|
if (pe.event) {
|
|
|
|
--pe.receiver->d_func()->postedEvents;
|
|
|
|
pe.event->posted = false;
|
|
|
|
delete pe.event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
threadData->postEventList.clear();
|
|
|
|
threadData->postEventList.recursion = 0;
|
|
|
|
threadData->quitNow = false;
|
2011-06-07 15:25:22 +00:00
|
|
|
threadData_clean = true;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QCoreApplicationPrivate::createEventDispatcher()
|
|
|
|
{
|
|
|
|
Q_Q(QCoreApplication);
|
2012-01-23 20:21:40 +00:00
|
|
|
#if defined(Q_OS_UNIX)
|
2015-11-23 06:13:00 +00:00
|
|
|
# if defined(Q_OS_OSX)
|
2015-07-01 07:57:33 +00:00
|
|
|
bool ok = false;
|
|
|
|
int value = qEnvironmentVariableIntValue("QT_EVENT_DISPATCHER_CORE_FOUNDATION", &ok);
|
|
|
|
if (ok && value > 0)
|
|
|
|
eventDispatcher = new QEventDispatcherCoreFoundation(q);
|
|
|
|
else
|
|
|
|
eventDispatcher = new QEventDispatcherUNIX(q);
|
|
|
|
# elif !defined(QT_NO_GLIB)
|
2012-08-06 22:10:11 +00:00
|
|
|
if (qEnvironmentVariableIsEmpty("QT_NO_GLIB") && QEventDispatcherGlib::versionSupported())
|
2011-04-27 10:05:43 +00:00
|
|
|
eventDispatcher = new QEventDispatcherGlib(q);
|
|
|
|
else
|
|
|
|
eventDispatcher = new QEventDispatcherUNIX(q);
|
2015-07-01 07:57:33 +00:00
|
|
|
# else
|
2011-04-27 10:05:43 +00:00
|
|
|
eventDispatcher = new QEventDispatcherUNIX(q);
|
2012-03-23 14:43:44 +00:00
|
|
|
# endif
|
2013-09-20 12:06:33 +00:00
|
|
|
#elif defined(Q_OS_WINRT)
|
|
|
|
eventDispatcher = new QEventDispatcherWinRT(q);
|
2011-04-27 10:05:43 +00:00
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
eventDispatcher = new QEventDispatcherWin32(q);
|
|
|
|
#else
|
|
|
|
# error "QEventDispatcher not yet ported to this platform"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-10-02 12:20:06 +00:00
|
|
|
void QCoreApplicationPrivate::eventDispatcherReady()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-08-01 13:50:00 +00:00
|
|
|
QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QThread *QCoreApplicationPrivate::mainThread()
|
|
|
|
{
|
2015-08-01 13:50:00 +00:00
|
|
|
Q_ASSERT(theMainThread.load() != 0);
|
|
|
|
return theMainThread.load();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 00:21:37 +00:00
|
|
|
bool QCoreApplicationPrivate::threadRequiresCoreApplication()
|
|
|
|
{
|
|
|
|
QThreadData *data = QThreadData::current(false);
|
|
|
|
if (!data)
|
|
|
|
return true; // default setting
|
|
|
|
return data->requiresCoreApplication;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
|
|
|
|
{
|
|
|
|
QThread *currentThread = QThread::currentThread();
|
|
|
|
QThread *thr = receiver->thread();
|
|
|
|
Q_ASSERT_X(currentThread == thr || !thr,
|
|
|
|
"QCoreApplication::sendEvent",
|
|
|
|
QString::fromLatin1("Cannot send events to objects owned by a different thread. "
|
|
|
|
"Current thread %1. Receiver '%2' (of type '%3') was created in thread %4")
|
|
|
|
.arg(QString::number((quintptr) currentThread, 16))
|
|
|
|
.arg(receiver->objectName())
|
|
|
|
.arg(QLatin1String(receiver->metaObject()->className()))
|
|
|
|
.arg(QString::number((quintptr) thr, 16))
|
|
|
|
.toLocal8Bit().data());
|
|
|
|
Q_UNUSED(currentThread);
|
|
|
|
Q_UNUSED(thr);
|
|
|
|
}
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif // QT_NO_QOBJECT
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void QCoreApplicationPrivate::appendApplicationPathToLibraryPaths()
|
|
|
|
{
|
2012-05-29 01:21:13 +00:00
|
|
|
#ifndef QT_NO_LIBRARY
|
2015-03-26 09:20:11 +00:00
|
|
|
QStringList *app_libpaths = coreappdata()->app_libpaths.data();
|
2012-07-27 09:53:46 +00:00
|
|
|
if (!app_libpaths)
|
2015-03-26 09:20:11 +00:00
|
|
|
coreappdata()->app_libpaths.reset(app_libpaths = new QStringList);
|
2012-07-27 09:53:46 +00:00
|
|
|
QString app_location = QCoreApplication::applicationFilePath();
|
2011-04-27 10:05:43 +00:00
|
|
|
app_location.truncate(app_location.lastIndexOf(QLatin1Char('/')));
|
2013-11-01 15:16:15 +00:00
|
|
|
#ifdef Q_OS_WINRT
|
|
|
|
if (app_location.isEmpty())
|
|
|
|
app_location.append(QLatin1Char('/'));
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
app_location = QDir(app_location).canonicalPath();
|
|
|
|
if (QFile::exists(app_location) && !app_libpaths->contains(app_location))
|
|
|
|
app_libpaths->append(app_location);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString qAppName()
|
|
|
|
{
|
|
|
|
if (!QCoreApplicationPrivate::checkInstance("qAppName"))
|
|
|
|
return QString();
|
|
|
|
return QCoreApplication::instance()->d_func()->appName();
|
|
|
|
}
|
|
|
|
|
2012-05-12 01:33:50 +00:00
|
|
|
void QCoreApplicationPrivate::initLocale()
|
|
|
|
{
|
|
|
|
if (qt_locale_initialized)
|
|
|
|
return;
|
|
|
|
qt_locale_initialized = true;
|
2015-11-24 13:12:04 +00:00
|
|
|
#if defined(Q_OS_UNIX) && !defined(QT_BOOTSTRAPPED)
|
2012-05-12 01:33:50 +00:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\class QCoreApplication
|
2012-08-23 10:22:38 +00:00
|
|
|
\inmodule QtCore
|
2014-07-07 18:21:09 +00:00
|
|
|
\brief The QCoreApplication class provides an event loop for Qt
|
|
|
|
applications without UI.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
This class is used by non-GUI applications to provide their event
|
|
|
|
loop. For non-GUI application that uses Qt, there should be exactly
|
|
|
|
one QCoreApplication object. For GUI applications, see
|
|
|
|
QApplication.
|
|
|
|
|
|
|
|
QCoreApplication contains the main event loop, where all events
|
|
|
|
from the operating system (e.g., timer and network events) and
|
|
|
|
other sources are processed and dispatched. It also handles the
|
|
|
|
application's initialization and finalization, as well as
|
|
|
|
system-wide and application-wide settings.
|
|
|
|
|
|
|
|
\section1 The Event Loop and Event Handling
|
|
|
|
|
2015-11-20 09:27:40 +00:00
|
|
|
The event loop is started with a call to exec(). Long-running
|
2011-04-27 10:05:43 +00:00
|
|
|
operations can call processEvents() to keep the application
|
|
|
|
responsive.
|
|
|
|
|
|
|
|
In general, we recommend that you create a QCoreApplication or a
|
|
|
|
QApplication object in your \c main() function as early as
|
|
|
|
possible. exec() will not return until the event loop exits; e.g.,
|
|
|
|
when quit() is called.
|
|
|
|
|
|
|
|
Several static convenience functions are also provided. The
|
|
|
|
QCoreApplication object is available from instance(). Events can
|
|
|
|
be sent or posted using sendEvent(), postEvent(), and
|
|
|
|
sendPostedEvents(). Pending events can be removed with
|
|
|
|
removePostedEvents() or flushed with flush().
|
|
|
|
|
|
|
|
The class provides a quit() slot and an aboutToQuit() signal.
|
|
|
|
|
|
|
|
\section1 Application and Library Paths
|
|
|
|
|
|
|
|
An application has an applicationDirPath() and an
|
|
|
|
applicationFilePath(). Library paths (see QLibrary) can be retrieved
|
|
|
|
with libraryPaths() and manipulated by setLibraryPaths(), addLibraryPath(),
|
|
|
|
and removeLibraryPath().
|
|
|
|
|
|
|
|
\section1 Internationalization and Translations
|
|
|
|
|
|
|
|
Translation files can be added or removed
|
|
|
|
using installTranslator() and removeTranslator(). Application
|
|
|
|
strings can be translated using translate(). The QObject::tr()
|
|
|
|
and QObject::trUtf8() functions are implemented in terms of
|
|
|
|
translate().
|
|
|
|
|
|
|
|
\section1 Accessing Command Line Arguments
|
|
|
|
|
|
|
|
The command line arguments which are passed to QCoreApplication's
|
|
|
|
constructor should be accessed using the arguments() function.
|
2015-11-16 15:16:42 +00:00
|
|
|
|
|
|
|
\note QCoreApplication removes option \c -qmljsdebugger="...". It parses the
|
|
|
|
argument of \c qmljsdebugger, and then removes this option plus its argument.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-08-24 09:15:19 +00:00
|
|
|
For more advanced command line option handling, create a QCommandLineParser.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\section1 Locale Settings
|
|
|
|
|
|
|
|
On Unix/Linux Qt is configured to use the system locale settings by
|
|
|
|
default. This can cause a conflict when using POSIX functions, for
|
|
|
|
instance, when converting between data types such as floats and
|
|
|
|
strings, since the notation may differ between locales. To get
|
|
|
|
around this problem, call the POSIX function \c{setlocale(LC_NUMERIC,"C")}
|
|
|
|
right after initializing QApplication or QCoreApplication to reset
|
|
|
|
the locale that is used for number formatting to "C"-locale.
|
|
|
|
|
2012-08-16 21:57:02 +00:00
|
|
|
\sa QGuiApplication, QAbstractEventDispatcher, QEventLoop,
|
2011-04-27 10:05:43 +00:00
|
|
|
{Semaphores Example}, {Wait Conditions Example}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn static QCoreApplication *QCoreApplication::instance()
|
|
|
|
|
|
|
|
Returns a pointer to the application's QCoreApplication (or
|
|
|
|
QApplication) instance.
|
|
|
|
|
|
|
|
If no instance has been allocated, \c null is returned.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 05:37:01 +00:00
|
|
|
/*!
|
|
|
|
\internal
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifdef QT_NO_QOBJECT
|
|
|
|
: d_ptr(&p)
|
|
|
|
#else
|
2011-04-27 10:05:43 +00:00
|
|
|
: QObject(p, 0)
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
// note: it is the subclasses' job to call
|
|
|
|
// QCoreApplicationPrivate::eventDispatcher->startingUp();
|
|
|
|
}
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
2015-11-20 09:27:40 +00:00
|
|
|
Flushes the platform-specific event queues.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If you are doing graphical changes inside a loop that does not
|
|
|
|
return to the event loop on asynchronous window systems like X11
|
2015-02-18 11:49:44 +00:00
|
|
|
or double buffered window systems like Quartz (OS X and iOS), and you want to
|
2011-04-27 10:05:43 +00:00
|
|
|
visualize these changes immediately (e.g. Splash Screens), call
|
|
|
|
this function.
|
|
|
|
|
|
|
|
\sa sendPostedEvents()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::flush()
|
|
|
|
{
|
|
|
|
if (self && self->d_func()->eventDispatcher)
|
|
|
|
self->d_func()->eventDispatcher->flush();
|
|
|
|
}
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
2015-11-20 09:27:40 +00:00
|
|
|
Constructs a Qt core application. Core applications are applications without
|
|
|
|
a graphical user interface. Such applications are used at the console or as
|
|
|
|
server processes.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \a argc and \a argv arguments are processed by the application,
|
|
|
|
and made available in a more convenient form by the arguments()
|
|
|
|
function.
|
|
|
|
|
|
|
|
\warning The data referred to by \a argc and \a argv must stay valid
|
|
|
|
for the entire lifetime of the QCoreApplication object. In addition,
|
|
|
|
\a argc must be greater than zero and \a argv must contain at least
|
|
|
|
one valid character string.
|
|
|
|
*/
|
2012-10-16 10:55:49 +00:00
|
|
|
QCoreApplication::QCoreApplication(int &argc, char **argv
|
|
|
|
#ifndef Q_QDOC
|
|
|
|
, int _internal
|
|
|
|
#endif
|
|
|
|
)
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifdef QT_NO_QOBJECT
|
|
|
|
: d_ptr(new QCoreApplicationPrivate(argc, argv, _internal))
|
|
|
|
#else
|
2012-10-16 10:55:49 +00:00
|
|
|
: QObject(*new QCoreApplicationPrivate(argc, argv, _internal))
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
init();
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplicationPrivate::eventDispatcher->startingUp();
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ### move to QCoreApplicationPrivate constructor?
|
|
|
|
void QCoreApplication::init()
|
|
|
|
{
|
2012-12-06 19:02:08 +00:00
|
|
|
d_ptr->q_ptr = this;
|
2011-04-27 10:05:43 +00:00
|
|
|
Q_D(QCoreApplication);
|
|
|
|
|
2012-05-12 01:33:50 +00:00
|
|
|
QCoreApplicationPrivate::initLocale();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Q_ASSERT_X(!self, "QCoreApplication", "there should be only one application object");
|
|
|
|
QCoreApplication::self = this;
|
|
|
|
|
2015-02-22 18:35:43 +00:00
|
|
|
// Store app name (so it's still available after QCoreApplication is destroyed)
|
2015-03-29 20:09:56 +00:00
|
|
|
if (!coreappdata()->applicationNameSet)
|
|
|
|
coreappdata()->application = d_func()->appName();
|
2015-02-22 18:35:43 +00:00
|
|
|
|
2014-02-04 15:35:26 +00:00
|
|
|
QLoggingRegistry::instance()->init();
|
|
|
|
|
2015-03-09 12:45:44 +00:00
|
|
|
#ifndef QT_NO_LIBRARY
|
|
|
|
// Reset the lib paths, so that they will be recomputed, taking the availability of argv[0]
|
|
|
|
// into account. If necessary, recompute right away and replay the manual changes on top of the
|
|
|
|
// new lib paths.
|
2015-03-26 09:20:11 +00:00
|
|
|
QStringList *appPaths = coreappdata()->app_libpaths.take();
|
|
|
|
QStringList *manualPaths = coreappdata()->manual_libpaths.take();
|
2015-03-09 12:45:44 +00:00
|
|
|
if (appPaths) {
|
|
|
|
if (manualPaths) {
|
|
|
|
// Replay the delta. As paths can only be prepended to the front or removed from
|
|
|
|
// anywhere in the list, we can just linearly scan the lists and find the items that
|
|
|
|
// have been removed. Once the original list is exhausted we know all the remaining
|
|
|
|
// items have been added.
|
|
|
|
QStringList newPaths(libraryPaths());
|
|
|
|
for (int i = manualPaths->length(), j = appPaths->length(); i > 0 || j > 0; qt_noop()) {
|
|
|
|
if (--j < 0) {
|
|
|
|
newPaths.prepend((*manualPaths)[--i]);
|
|
|
|
} else if (--i < 0) {
|
|
|
|
newPaths.removeAll((*appPaths)[j]);
|
|
|
|
} else if ((*manualPaths)[i] != (*appPaths)[j]) {
|
|
|
|
newPaths.removeAll((*appPaths)[j]);
|
|
|
|
++i; // try again with next item.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete manualPaths;
|
2015-03-26 09:20:11 +00:00
|
|
|
coreappdata()->manual_libpaths.reset(new QStringList(newPaths));
|
2015-03-09 12:45:44 +00:00
|
|
|
}
|
|
|
|
delete appPaths;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
// use the event dispatcher created by the app programmer (if any)
|
|
|
|
if (!QCoreApplicationPrivate::eventDispatcher)
|
2013-03-18 14:19:44 +00:00
|
|
|
QCoreApplicationPrivate::eventDispatcher = d->threadData->eventDispatcher.load();
|
2011-04-27 10:05:43 +00:00
|
|
|
// otherwise we create one
|
|
|
|
if (!QCoreApplicationPrivate::eventDispatcher)
|
|
|
|
d->createEventDispatcher();
|
|
|
|
Q_ASSERT(QCoreApplicationPrivate::eventDispatcher != 0);
|
|
|
|
|
2011-09-08 15:40:55 +00:00
|
|
|
if (!QCoreApplicationPrivate::eventDispatcher->parent()) {
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplicationPrivate::eventDispatcher->moveToThread(d->threadData->thread);
|
2011-09-08 15:40:55 +00:00
|
|
|
QCoreApplicationPrivate::eventDispatcher->setParent(this);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
d->threadData->eventDispatcher = QCoreApplicationPrivate::eventDispatcher;
|
2013-10-02 12:20:06 +00:00
|
|
|
d->eventDispatcherReady();
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifdef QT_EVAL
|
2013-07-04 13:44:53 +00:00
|
|
|
extern void qt_core_eval_init(QCoreApplicationPrivate::Type);
|
2011-04-27 10:05:43 +00:00
|
|
|
qt_core_eval_init(d->application_type);
|
|
|
|
#endif
|
|
|
|
|
2011-09-19 08:56:12 +00:00
|
|
|
d->processCommandLineArguments();
|
|
|
|
|
2013-01-14 11:58:31 +00:00
|
|
|
qt_call_pre_routines();
|
2011-04-27 10:05:43 +00:00
|
|
|
qt_startup_hook();
|
2014-04-15 11:11:40 +00:00
|
|
|
#ifndef QT_BOOTSTRAPPED
|
|
|
|
if (Q_UNLIKELY(qtHookData[QHooks::Startup]))
|
|
|
|
reinterpret_cast<QHooks::StartupCallback>(qtHookData[QHooks::Startup])();
|
|
|
|
#endif
|
2013-03-13 09:34:18 +00:00
|
|
|
|
2013-03-20 12:46:57 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2013-03-13 09:34:18 +00:00
|
|
|
QCoreApplicationPrivate::is_app_running = true; // No longer starting up.
|
2013-03-20 12:46:57 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Destroys the QCoreApplication object.
|
|
|
|
*/
|
|
|
|
QCoreApplication::~QCoreApplication()
|
|
|
|
{
|
|
|
|
qt_call_post_routines();
|
|
|
|
|
|
|
|
self = 0;
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplicationPrivate::is_app_closing = true;
|
|
|
|
QCoreApplicationPrivate::is_app_running = false;
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#if !defined(QT_NO_THREAD)
|
|
|
|
// Synchronize and stop the global thread pool threads.
|
|
|
|
QThreadPool *globalThreadPool = 0;
|
|
|
|
QT_TRY {
|
|
|
|
globalThreadPool = QThreadPool::globalInstance();
|
|
|
|
} QT_CATCH (...) {
|
|
|
|
// swallow the exception, since destructors shouldn't throw
|
|
|
|
}
|
|
|
|
if (globalThreadPool)
|
|
|
|
globalThreadPool->waitForDone();
|
|
|
|
#endif
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
d_func()->threadData->eventDispatcher = 0;
|
|
|
|
if (QCoreApplicationPrivate::eventDispatcher)
|
|
|
|
QCoreApplicationPrivate::eventDispatcher->closingDown();
|
|
|
|
QCoreApplicationPrivate::eventDispatcher = 0;
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#ifndef QT_NO_LIBRARY
|
2015-03-26 09:20:11 +00:00
|
|
|
coreappdata()->app_libpaths.reset();
|
|
|
|
coreappdata()->manual_libpaths.reset();
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-01 16:58:19 +00:00
|
|
|
/*!
|
|
|
|
\since 5.3
|
|
|
|
|
|
|
|
Allows the application to run setuid on UNIX platforms if \a allow
|
|
|
|
is true.
|
|
|
|
|
|
|
|
If \a allow is false (the default) and Qt detects the application is
|
|
|
|
running with an effective user id different than the real user id,
|
|
|
|
the application will be aborted when a QCoreApplication instance is
|
|
|
|
created.
|
|
|
|
|
|
|
|
Qt is not an appropriate solution for setuid programs due to its
|
|
|
|
large attack surface. However some applications may be required
|
|
|
|
to run in this manner for historical reasons. This flag will
|
|
|
|
prevent Qt from aborting the application when this is detected,
|
|
|
|
and must be set before a QCoreApplication instance is created.
|
|
|
|
|
|
|
|
\note It is strongly recommended not to enable this option since
|
|
|
|
it introduces security risks.
|
|
|
|
*/
|
|
|
|
void QCoreApplication::setSetuidAllowed(bool allow)
|
|
|
|
{
|
|
|
|
QCoreApplicationPrivate::setuidAllowed = allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 5.3
|
|
|
|
|
|
|
|
Returns true if the application is allowed to run setuid on UNIX
|
|
|
|
platforms.
|
|
|
|
|
|
|
|
\sa QCoreApplication::setSetuidAllowed()
|
|
|
|
*/
|
|
|
|
bool QCoreApplication::isSetuidAllowed()
|
|
|
|
{
|
|
|
|
return QCoreApplicationPrivate::setuidAllowed;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets the attribute \a attribute if \a on is true;
|
|
|
|
otherwise clears the attribute.
|
|
|
|
|
|
|
|
\sa testAttribute()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute, bool on)
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
QCoreApplicationPrivate::attribs |= 1 << attribute;
|
|
|
|
else
|
|
|
|
QCoreApplicationPrivate::attribs &= ~(1 << attribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if attribute \a attribute is set;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa setAttribute()
|
|
|
|
*/
|
|
|
|
bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute)
|
|
|
|
{
|
|
|
|
return QCoreApplicationPrivate::testAttribute(attribute);
|
|
|
|
}
|
|
|
|
|
2012-10-16 10:55:49 +00:00
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
|
|
|
|
2012-10-16 10:55:49 +00:00
|
|
|
/*!
|
|
|
|
\property QCoreApplication::quitLockEnabled
|
|
|
|
|
2015-08-24 07:24:18 +00:00
|
|
|
\brief Whether the use of the QEventLoopLocker feature can cause the
|
|
|
|
application to quit.
|
|
|
|
|
|
|
|
The default is \c true.
|
2012-10-16 10:55:49 +00:00
|
|
|
|
|
|
|
\sa QEventLoopLocker
|
|
|
|
*/
|
|
|
|
|
2011-10-26 11:29:51 +00:00
|
|
|
bool QCoreApplication::isQuitLockEnabled()
|
|
|
|
{
|
|
|
|
return quitLockRefEnabled;
|
|
|
|
}
|
|
|
|
|
2015-04-17 00:21:37 +00:00
|
|
|
static bool doNotify(QObject *, QEvent *);
|
|
|
|
|
2011-10-26 11:29:51 +00:00
|
|
|
void QCoreApplication::setQuitLockEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
quitLockRefEnabled = enabled;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
2015-04-16 22:44:16 +00:00
|
|
|
\deprecated
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
This function is here to make it possible for Qt extensions to
|
|
|
|
hook into event notification without subclassing QApplication
|
|
|
|
*/
|
|
|
|
bool QCoreApplication::notifyInternal(QObject *receiver, QEvent *event)
|
|
|
|
{
|
2015-04-16 22:44:16 +00:00
|
|
|
return notifyInternal2(receiver, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
\since 5.6
|
|
|
|
|
|
|
|
This function is here to make it possible for Qt extensions to
|
|
|
|
hook into event notification without subclassing QApplication.
|
|
|
|
*/
|
|
|
|
bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
|
|
|
|
{
|
2015-04-17 00:21:37 +00:00
|
|
|
bool selfRequired = QCoreApplicationPrivate::threadRequiresCoreApplication();
|
|
|
|
if (!self && selfRequired)
|
2015-04-16 22:44:16 +00:00
|
|
|
return false;
|
|
|
|
|
2012-12-28 18:09:39 +00:00
|
|
|
// Make it possible for Qt Script to hook into events even
|
2011-12-05 12:46:39 +00:00
|
|
|
// though QApplication is subclassed...
|
|
|
|
bool result = false;
|
|
|
|
void *cbdata[] = { receiver, event, &result };
|
|
|
|
if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// Qt enforces the rule that events can only be sent to objects in
|
|
|
|
// the current thread, so receiver->d_func()->threadData is
|
|
|
|
// equivalent to QThreadData::current(), just without the function
|
|
|
|
// call overhead.
|
|
|
|
QObjectPrivate *d = receiver->d_func();
|
|
|
|
QThreadData *threadData = d->threadData;
|
2011-11-21 14:28:57 +00:00
|
|
|
QScopedLoopLevelCounter loopLevelCounter(threadData);
|
2015-04-17 00:21:37 +00:00
|
|
|
if (!selfRequired)
|
|
|
|
return doNotify(receiver, event);
|
2015-04-16 22:44:16 +00:00
|
|
|
return self->notify(receiver, event);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sends \a event to \a receiver: \a {receiver}->event(\a event).
|
|
|
|
Returns the value that is returned from the receiver's event
|
|
|
|
handler. Note that this function is called for all events sent to
|
|
|
|
any object in any thread.
|
|
|
|
|
|
|
|
For certain types of events (e.g. mouse and key events),
|
|
|
|
the event will be propagated to the receiver's parent and so on up to
|
|
|
|
the top-level object if the receiver is not interested in the event
|
2013-10-02 14:51:05 +00:00
|
|
|
(i.e., it returns \c false).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
There are five different ways that events can be processed;
|
|
|
|
reimplementing this virtual function is just one of them. All five
|
|
|
|
approaches are listed below:
|
|
|
|
\list 1
|
2014-08-25 13:56:19 +00:00
|
|
|
\li Reimplementing \l {QWidget::}{paintEvent()}, \l {QWidget::}{mousePressEvent()} and so
|
|
|
|
on. This is the commonest, easiest, and least powerful way.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-03-01 14:28:31 +00:00
|
|
|
\li Reimplementing this function. This is very powerful, providing
|
2011-04-27 10:05:43 +00:00
|
|
|
complete control; but only one subclass can be active at a time.
|
|
|
|
|
2012-03-01 14:28:31 +00:00
|
|
|
\li Installing an event filter on QCoreApplication::instance(). Such
|
2011-04-27 10:05:43 +00:00
|
|
|
an event filter is able to process all events for all widgets, so
|
|
|
|
it's just as powerful as reimplementing notify(); furthermore, it's
|
|
|
|
possible to have more than one application-global event filter.
|
|
|
|
Global event filters even see mouse events for
|
|
|
|
\l{QWidget::isEnabled()}{disabled widgets}. Note that application
|
|
|
|
event filters are only called for objects that live in the main
|
|
|
|
thread.
|
|
|
|
|
2012-03-01 14:28:31 +00:00
|
|
|
\li Reimplementing QObject::event() (as QWidget does). If you do
|
2011-04-27 10:05:43 +00:00
|
|
|
this you get Tab key presses, and you get to see the events before
|
|
|
|
any widget-specific event filters.
|
|
|
|
|
2012-03-01 14:28:31 +00:00
|
|
|
\li Installing an event filter on the object. Such an event filter gets all
|
2011-04-27 10:05:43 +00:00
|
|
|
the events, including Tab and Shift+Tab key press events, as long as they
|
|
|
|
do not change the focus widget.
|
|
|
|
\endlist
|
|
|
|
|
2015-04-17 00:32:18 +00:00
|
|
|
\b{Future direction:} This function will not be called for objects that live
|
|
|
|
outside the main thread in Qt 6. Applications that need that functionality
|
|
|
|
should find other solutions for their event inspection needs in the meantime.
|
|
|
|
The change may be extended to the main thread, causing this function to be
|
|
|
|
deprecated.
|
|
|
|
|
|
|
|
\warning If you override this function, you must ensure all threads that
|
|
|
|
process events stop doing so before your application object begins
|
|
|
|
destruction. This includes threads started by other libraries that you may be
|
|
|
|
using, but does not apply to Qt's own threads.
|
|
|
|
|
2012-06-23 19:48:53 +00:00
|
|
|
\sa QObject::event(), installNativeEventFilter()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool QCoreApplication::notify(QObject *receiver, QEvent *event)
|
|
|
|
{
|
|
|
|
// no events are delivered after ~QCoreApplication() has started
|
|
|
|
if (QCoreApplicationPrivate::is_app_closing)
|
|
|
|
return true;
|
2015-04-17 00:21:37 +00:00
|
|
|
return doNotify(receiver, event);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-04-17 00:21:37 +00:00
|
|
|
static bool doNotify(QObject *receiver, QEvent *event)
|
|
|
|
{
|
2011-04-27 10:05:43 +00:00
|
|
|
if (receiver == 0) { // serious error
|
|
|
|
qWarning("QCoreApplication::notify: Unexpected null receiver");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef QT_NO_DEBUG
|
2015-04-16 22:44:16 +00:00
|
|
|
QCoreApplicationPrivate::checkReceiverThread(receiver);
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-16 22:44:16 +00:00
|
|
|
return receiver->isWidgetType() ? false : QCoreApplicationPrivate::notify_helper(receiver, event);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
|
|
|
|
{
|
2015-04-17 00:21:37 +00:00
|
|
|
// We can't access the application event filters outside of the main thread (race conditions)
|
|
|
|
Q_ASSERT(receiver->d_func()->threadData->thread == mainThread());
|
|
|
|
|
|
|
|
if (extraData) {
|
2011-04-27 10:05:43 +00:00
|
|
|
// application event filters are only called for objects in the GUI thread
|
2012-05-17 07:39:46 +00:00
|
|
|
for (int i = 0; i < extraData->eventFilters.size(); ++i) {
|
2013-06-15 09:49:45 +00:00
|
|
|
QObject *obj = extraData->eventFilters.at(i);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
if (obj->d_func()->threadData != threadData) {
|
|
|
|
qWarning("QCoreApplication: Application event filter cannot be in a different thread.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (obj->eventFilter(receiver, event))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
|
|
|
|
{
|
2015-04-16 22:44:16 +00:00
|
|
|
if (receiver != QCoreApplication::instance() && receiver->d_func()->extraData) {
|
2012-05-17 07:39:46 +00:00
|
|
|
for (int i = 0; i < receiver->d_func()->extraData->eventFilters.size(); ++i) {
|
2013-06-15 09:49:45 +00:00
|
|
|
QObject *obj = receiver->d_func()->extraData->eventFilters.at(i);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
if (obj->d_func()->threadData != receiver->d_func()->threadData) {
|
|
|
|
qWarning("QCoreApplication: Object event filter cannot be in a different thread.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (obj->eventFilter(receiver, event))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-15 05:37:01 +00:00
|
|
|
/*!
|
|
|
|
\internal
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-04-16 22:44:16 +00:00
|
|
|
Helper function called by QCoreApplicationPrivate::notify() and qapplication.cpp
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
|
|
|
|
{
|
2015-04-16 22:44:16 +00:00
|
|
|
// send to all application event filters (only does anything in the main thread)
|
2015-04-17 00:21:37 +00:00
|
|
|
if (QCoreApplication::self
|
|
|
|
&& receiver->d_func()->threadData->thread == mainThread()
|
|
|
|
&& QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event))
|
2011-04-27 10:05:43 +00:00
|
|
|
return true;
|
|
|
|
// send to all receiver event filters
|
|
|
|
if (sendThroughObjectEventFilters(receiver, event))
|
|
|
|
return true;
|
|
|
|
// deliver the event
|
|
|
|
return receiver->event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if an application object has not been created yet;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa closingDown()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QCoreApplication::startingUp()
|
|
|
|
{
|
|
|
|
return !QCoreApplicationPrivate::is_app_running;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the application objects are being destroyed;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa startingUp()
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool QCoreApplication::closingDown()
|
|
|
|
{
|
|
|
|
return QCoreApplicationPrivate::is_app_closing;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Processes all pending events for the calling thread according to
|
|
|
|
the specified \a flags until there are no more events to process.
|
|
|
|
|
|
|
|
You can call this function occasionally when your program is busy
|
|
|
|
performing a long operation (e.g. copying a file).
|
|
|
|
|
2013-11-11 08:15:24 +00:00
|
|
|
In the event that you are running a local loop which calls this function
|
2011-04-27 10:05:43 +00:00
|
|
|
continuously, without an event loop, the
|
|
|
|
\l{QEvent::DeferredDelete}{DeferredDelete} events will
|
|
|
|
not be processed. This can affect the behaviour of widgets,
|
|
|
|
e.g. QToolTip, that rely on \l{QEvent::DeferredDelete}{DeferredDelete}
|
|
|
|
events to function properly. An alternative would be to call
|
|
|
|
\l{QCoreApplication::sendPostedEvents()}{sendPostedEvents()} from
|
|
|
|
within that local loop.
|
|
|
|
|
|
|
|
Calling this function processes events only for the calling thread.
|
|
|
|
|
|
|
|
\threadsafe
|
|
|
|
|
|
|
|
\sa exec(), QTimer, QEventLoop::processEvents(), flush(), sendPostedEvents()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags)
|
|
|
|
{
|
|
|
|
QThreadData *data = QThreadData::current();
|
2013-03-18 14:19:44 +00:00
|
|
|
if (!data->hasEventDispatcher())
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
2013-03-18 14:19:44 +00:00
|
|
|
data->eventDispatcher.load()->processEvents(flags);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\overload processEvents()
|
|
|
|
|
|
|
|
Processes pending events for the calling thread for \a maxtime
|
|
|
|
milliseconds or until there are no more events to process,
|
|
|
|
whichever is shorter.
|
|
|
|
|
2015-11-20 09:27:40 +00:00
|
|
|
You can call this function occasionally when your program is busy
|
2011-04-27 10:05:43 +00:00
|
|
|
doing a long operation (e.g. copying a file).
|
|
|
|
|
|
|
|
Calling this function processes events only for the calling thread.
|
|
|
|
|
|
|
|
\threadsafe
|
|
|
|
|
|
|
|
\sa exec(), QTimer, QEventLoop::processEvents()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime)
|
|
|
|
{
|
|
|
|
QThreadData *data = QThreadData::current();
|
2013-03-18 14:19:44 +00:00
|
|
|
if (!data->hasEventDispatcher())
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
QElapsedTimer start;
|
|
|
|
start.start();
|
2013-03-18 14:19:44 +00:00
|
|
|
while (data->eventDispatcher.load()->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) {
|
2011-04-27 10:05:43 +00:00
|
|
|
if (start.elapsed() > maxtime)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
Main event loop wrappers
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
2015-11-20 09:27:40 +00:00
|
|
|
Enters the main event loop and waits until exit() is called. Returns
|
|
|
|
the value that was passed to exit() (which is 0 if exit() is called via
|
|
|
|
quit()).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
It is necessary to call this function to start event handling. The
|
|
|
|
main event loop receives events from the window system and
|
|
|
|
dispatches these to the application widgets.
|
|
|
|
|
2014-08-25 13:56:19 +00:00
|
|
|
To make your application perform idle processing (by executing a
|
2011-04-27 10:05:43 +00:00
|
|
|
special function whenever there are no pending events), use a
|
|
|
|
QTimer with 0 timeout. More advanced idle processing schemes can
|
|
|
|
be achieved using processEvents().
|
|
|
|
|
|
|
|
We recommend that you connect clean-up code to the
|
|
|
|
\l{QCoreApplication::}{aboutToQuit()} signal, instead of putting it in
|
|
|
|
your application's \c{main()} function because on some platforms the
|
2014-08-25 13:56:19 +00:00
|
|
|
exec() call may not return. For example, on Windows
|
2011-04-27 10:05:43 +00:00
|
|
|
when the user logs off, the system terminates the process after Qt
|
|
|
|
closes all top-level windows. Hence, there is no guarantee that the
|
|
|
|
application will have time to exit its event loop and execute code at
|
2014-08-25 13:56:19 +00:00
|
|
|
the end of the \c{main()} function after the exec()
|
2011-04-27 10:05:43 +00:00
|
|
|
call.
|
|
|
|
|
|
|
|
\sa quit(), exit(), processEvents(), QApplication::exec()
|
|
|
|
*/
|
|
|
|
int QCoreApplication::exec()
|
|
|
|
{
|
|
|
|
if (!QCoreApplicationPrivate::checkInstance("exec"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
QThreadData *threadData = self->d_func()->threadData;
|
|
|
|
if (threadData != QThreadData::current()) {
|
|
|
|
qWarning("%s::exec: Must be called from the main thread", self->metaObject()->className());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!threadData->eventLoops.isEmpty()) {
|
|
|
|
qWarning("QCoreApplication::exec: The event loop is already running");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
threadData->quitNow = false;
|
|
|
|
QEventLoop eventLoop;
|
|
|
|
self->d_func()->in_exec = true;
|
|
|
|
self->d_func()->aboutToQuitEmitted = false;
|
|
|
|
int returnCode = eventLoop.exec();
|
|
|
|
threadData->quitNow = false;
|
|
|
|
if (self) {
|
|
|
|
self->d_func()->in_exec = false;
|
|
|
|
if (!self->d_func()->aboutToQuitEmitted)
|
2012-10-18 12:29:06 +00:00
|
|
|
emit self->aboutToQuit(QPrivateSignal());
|
2011-04-27 10:05:43 +00:00
|
|
|
self->d_func()->aboutToQuitEmitted = true;
|
|
|
|
sendPostedEvents(0, QEvent::DeferredDelete);
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Tells the application to exit with a return code.
|
|
|
|
|
|
|
|
After this function has been called, the application leaves the
|
|
|
|
main event loop and returns from the call to exec(). The exec()
|
|
|
|
function returns \a returnCode. If the event loop is not running,
|
|
|
|
this function does nothing.
|
|
|
|
|
|
|
|
By convention, a \a returnCode of 0 means success, and any non-zero
|
|
|
|
value indicates an error.
|
|
|
|
|
|
|
|
Note that unlike the C library function of the same name, this
|
|
|
|
function \e does return to the caller -- it is event processing that
|
|
|
|
stops.
|
|
|
|
|
|
|
|
\sa quit(), exec()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::exit(int returnCode)
|
|
|
|
{
|
|
|
|
if (!self)
|
|
|
|
return;
|
|
|
|
QThreadData *data = self->d_func()->threadData;
|
|
|
|
data->quitNow = true;
|
|
|
|
for (int i = 0; i < data->eventLoops.size(); ++i) {
|
|
|
|
QEventLoop *eventLoop = data->eventLoops.at(i);
|
|
|
|
eventLoop->exit(returnCode);
|
|
|
|
}
|
2015-01-16 10:29:54 +00:00
|
|
|
#ifdef Q_OS_WINRT
|
|
|
|
qWarning("QCoreApplication::exit: It is not recommended to explicitly exit an application on Windows Store Apps");
|
|
|
|
ComPtr<ICoreApplication> app;
|
|
|
|
HRESULT hr = RoGetActivationFactory(Wrappers::HString::MakeReference(RuntimeClass_Windows_ApplicationModel_Core_CoreApplication).Get(),
|
|
|
|
IID_PPV_ARGS(&app));
|
|
|
|
RETURN_VOID_IF_FAILED("Could not acquire ICoreApplication object");
|
|
|
|
ComPtr<ICoreApplicationExit> appExit;
|
|
|
|
|
|
|
|
hr = app.As(&appExit);
|
|
|
|
RETURN_VOID_IF_FAILED("Could not acquire ICoreApplicationExit object");
|
|
|
|
hr = appExit->Exit();
|
|
|
|
RETURN_VOID_IF_FAILED("Could not exit application");
|
|
|
|
#endif // Q_OS_WINRT
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
QCoreApplication management of posted events
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
|
|
|
|
|
|
|
|
Sends event \a event directly to receiver \a receiver, using the
|
|
|
|
notify() function. Returns the value that was returned from the
|
|
|
|
event handler.
|
|
|
|
|
|
|
|
The event is \e not deleted when the event has been sent. The normal
|
|
|
|
approach is to create the event on the stack, for example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa postEvent(), notify()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.3
|
|
|
|
|
|
|
|
Adds the event \a event, with the object \a receiver as the
|
|
|
|
receiver of the event, to an event queue and returns immediately.
|
|
|
|
|
|
|
|
The event must be allocated on the heap since the post event queue
|
|
|
|
will take ownership of the event and delete it once it has been
|
|
|
|
posted. It is \e {not safe} to access the event after
|
|
|
|
it has been posted.
|
|
|
|
|
|
|
|
When control returns to the main event loop, all events that are
|
|
|
|
stored in the queue will be sent using the notify() function.
|
|
|
|
|
|
|
|
Events are sorted in descending \a priority order, i.e. events
|
|
|
|
with a high \a priority are queued before events with a lower \a
|
|
|
|
priority. The \a priority can be any integer value, i.e. between
|
|
|
|
INT_MAX and INT_MIN, inclusive; see Qt::EventPriority for more
|
|
|
|
details. Events with equal \a priority will be processed in the
|
|
|
|
order posted.
|
|
|
|
|
|
|
|
\threadsafe
|
|
|
|
|
|
|
|
\sa sendEvent(), notify(), sendPostedEvents(), Qt::EventPriority
|
|
|
|
*/
|
|
|
|
void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority)
|
|
|
|
{
|
|
|
|
if (receiver == 0) {
|
|
|
|
qWarning("QCoreApplication::postEvent: Unexpected null receiver");
|
|
|
|
delete event;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QThreadData * volatile * pdata = &receiver->d_func()->threadData;
|
|
|
|
QThreadData *data = *pdata;
|
|
|
|
if (!data) {
|
|
|
|
// posting during destruction? just delete the event to prevent a leak
|
|
|
|
delete event;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock the post event mutex
|
|
|
|
data->postEventList.mutex.lock();
|
|
|
|
|
|
|
|
// if object has moved to another thread, follow it
|
|
|
|
while (data != *pdata) {
|
|
|
|
data->postEventList.mutex.unlock();
|
|
|
|
|
|
|
|
data = *pdata;
|
|
|
|
if (!data) {
|
|
|
|
// posting during destruction? just delete the event to prevent a leak
|
|
|
|
delete event;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->postEventList.mutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutexUnlocker locker(&data->postEventList.mutex);
|
|
|
|
|
|
|
|
// if this is one of the compressible events, do compression
|
|
|
|
if (receiver->d_func()->postedEvents
|
|
|
|
&& self && self->compressEvent(event, receiver, &data->postEventList)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->type() == QEvent::DeferredDelete && data == QThreadData::current()) {
|
|
|
|
// remember the current running eventloop for DeferredDelete
|
|
|
|
// events posted in the receiver's thread
|
2012-05-14 11:27:28 +00:00
|
|
|
static_cast<QDeferredDeleteEvent *>(event)->level = data->loopLevel;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete the event on exceptions to protect against memory leaks till the event is
|
|
|
|
// properly owned in the postEventList
|
|
|
|
QScopedPointer<QEvent> eventDeleter(event);
|
2011-06-24 15:26:20 +00:00
|
|
|
data->postEventList.addEvent(QPostEvent(receiver, event, priority));
|
2011-04-27 10:05:43 +00:00
|
|
|
eventDeleter.take();
|
|
|
|
event->posted = true;
|
|
|
|
++receiver->d_func()->postedEvents;
|
|
|
|
data->canWait = false;
|
|
|
|
locker.unlock();
|
|
|
|
|
2013-03-18 14:19:44 +00:00
|
|
|
QAbstractEventDispatcher* dispatcher = data->eventDispatcher.loadAcquire();
|
|
|
|
if (dispatcher)
|
|
|
|
dispatcher->wakeUp();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if \a event was compressed away (possibly deleted) and should not be added to the list.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
|
|
|
|
{
|
2011-05-24 07:14:43 +00:00
|
|
|
#ifdef Q_OS_WIN
|
2011-04-27 10:05:43 +00:00
|
|
|
Q_ASSERT(event);
|
|
|
|
Q_ASSERT(receiver);
|
|
|
|
Q_ASSERT(postedEvents);
|
|
|
|
|
|
|
|
// compress posted timers to this object.
|
|
|
|
if (event->type() == QEvent::Timer && receiver->d_func()->postedEvents > 0) {
|
|
|
|
int timerId = ((QTimerEvent *) event)->timerId();
|
|
|
|
for (int i=0; i<postedEvents->size(); ++i) {
|
|
|
|
const QPostEvent &e = postedEvents->at(i);
|
|
|
|
if (e.receiver == receiver && e.event && e.event->type() == QEvent::Timer
|
|
|
|
&& ((QTimerEvent *) e.event)->timerId() == timerId) {
|
|
|
|
delete event;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if ((event->type() == QEvent::DeferredDelete
|
|
|
|
|| event->type() == QEvent::Quit)
|
|
|
|
&& receiver->d_func()->postedEvents > 0) {
|
|
|
|
for (int i = 0; i < postedEvents->size(); ++i) {
|
|
|
|
const QPostEvent &cur = postedEvents->at(i);
|
|
|
|
if (cur.receiver != receiver
|
|
|
|
|| cur.event == 0
|
|
|
|
|| cur.event->type() != event->type())
|
|
|
|
continue;
|
|
|
|
// found an event for this receiver
|
|
|
|
delete event;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Immediately dispatches all events which have been previously queued
|
|
|
|
with QCoreApplication::postEvent() and which are for the object \a receiver
|
|
|
|
and have the event type \a event_type.
|
|
|
|
|
|
|
|
Events from the window system are \e not dispatched by this
|
|
|
|
function, but by processEvents().
|
|
|
|
|
|
|
|
If \a receiver is null, the events of \a event_type are sent for all
|
|
|
|
objects. If \a event_type is 0, all the events are sent for \a receiver.
|
|
|
|
|
2015-11-20 09:27:40 +00:00
|
|
|
\note This method must be called from the thread in which its QObject
|
|
|
|
parameter, \a receiver, lives.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa flush(), postEvent()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::sendPostedEvents(QObject *receiver, int event_type)
|
|
|
|
{
|
|
|
|
QThreadData *data = QThreadData::current();
|
|
|
|
|
|
|
|
QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type,
|
|
|
|
QThreadData *data)
|
|
|
|
{
|
|
|
|
if (event_type == -1) {
|
|
|
|
// we were called by an obsolete event dispatcher.
|
|
|
|
event_type = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (receiver && receiver->d_func()->threadData != data) {
|
|
|
|
qWarning("QCoreApplication::sendPostedEvents: Cannot send "
|
|
|
|
"posted events for objects in another thread");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
++data->postEventList.recursion;
|
|
|
|
|
|
|
|
QMutexLocker locker(&data->postEventList.mutex);
|
|
|
|
|
|
|
|
// by default, we assume that the event dispatcher can go to sleep after
|
|
|
|
// processing all events. if any new events are posted while we send
|
|
|
|
// events, canWait will be set to false.
|
|
|
|
data->canWait = (data->postEventList.size() == 0);
|
|
|
|
|
|
|
|
if (data->postEventList.size() == 0 || (receiver && !receiver->d_func()->postedEvents)) {
|
|
|
|
--data->postEventList.recursion;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->canWait = true;
|
|
|
|
|
|
|
|
// okay. here is the tricky loop. be careful about optimizing
|
|
|
|
// this, it looks the way it does for good reasons.
|
|
|
|
int startOffset = data->postEventList.startOffset;
|
|
|
|
int &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset;
|
|
|
|
data->postEventList.insertionOffset = data->postEventList.size();
|
|
|
|
|
2011-06-23 12:11:50 +00:00
|
|
|
// Exception-safe cleaning up without the need for a try/catch block
|
|
|
|
struct CleanUp {
|
|
|
|
QObject *receiver;
|
|
|
|
int event_type;
|
|
|
|
QThreadData *data;
|
|
|
|
bool exceptionCaught;
|
|
|
|
|
|
|
|
inline CleanUp(QObject *receiver, int event_type, QThreadData *data) :
|
|
|
|
receiver(receiver), event_type(event_type), data(data), exceptionCaught(true)
|
|
|
|
{}
|
|
|
|
inline ~CleanUp()
|
|
|
|
{
|
|
|
|
if (exceptionCaught) {
|
|
|
|
// since we were interrupted, we need another pass to make sure we clean everything up
|
|
|
|
data->canWait = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
--data->postEventList.recursion;
|
2013-03-18 14:19:44 +00:00
|
|
|
if (!data->postEventList.recursion && !data->canWait && data->hasEventDispatcher())
|
|
|
|
data->eventDispatcher.load()->wakeUp();
|
2011-06-23 12:11:50 +00:00
|
|
|
|
|
|
|
// clear the global list, i.e. remove everything that was
|
|
|
|
// delivered.
|
|
|
|
if (!event_type && !receiver && data->postEventList.startOffset >= 0) {
|
|
|
|
const QPostEventList::iterator it = data->postEventList.begin();
|
|
|
|
data->postEventList.erase(it, it + data->postEventList.startOffset);
|
|
|
|
data->postEventList.insertionOffset -= data->postEventList.startOffset;
|
|
|
|
Q_ASSERT(data->postEventList.insertionOffset >= 0);
|
|
|
|
data->postEventList.startOffset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
CleanUp cleanup(receiver, event_type, data);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
while (i < data->postEventList.size()) {
|
|
|
|
// avoid live-lock
|
|
|
|
if (i >= data->postEventList.insertionOffset)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const QPostEvent &pe = data->postEventList.at(i);
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if (!pe.event)
|
|
|
|
continue;
|
|
|
|
if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) {
|
|
|
|
data->canWait = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pe.event->type() == QEvent::DeferredDelete) {
|
|
|
|
// DeferredDelete events are only sent when we are explicitly asked to
|
|
|
|
// (s.a. QEvent::DeferredDelete), and then only if the event loop that
|
|
|
|
// posted the event has returned.
|
2012-05-14 11:27:28 +00:00
|
|
|
int loopLevel = static_cast<QDeferredDeleteEvent *>(pe.event)->loopLevel();
|
2011-04-27 10:05:43 +00:00
|
|
|
const bool allowDeferredDelete =
|
2012-05-14 11:27:28 +00:00
|
|
|
(loopLevel > data->loopLevel
|
|
|
|
|| (!loopLevel && data->loopLevel > 0)
|
2011-04-27 10:05:43 +00:00
|
|
|
|| (event_type == QEvent::DeferredDelete
|
2012-05-14 11:27:28 +00:00
|
|
|
&& loopLevel == data->loopLevel));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!allowDeferredDelete) {
|
|
|
|
// cannot send deferred delete
|
|
|
|
if (!event_type && !receiver) {
|
Change event posting to use a QVector.
This provides a ~10% improvement to the newly introduced QCoreApplication
event_posting_benchmark (a simple synthetic benchmark of creating a bunch of
events, posting them, and sending the queue).
before:
********* Start testing of QCoreApplicationBenchmark *********
Config: Using QTest library 5.0.0, Qt 5.0.0
PASS : QCoreApplicationBenchmark::initTestCase()
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"1000":
0.82 msecs per iteration (total: 53, iterations: 64)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"10000":
8.6 msecs per iteration (total: 69, iterations: 8)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"100000":
84 msecs per iteration (total: 84, iterations: 1)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"1000000":
874 msecs per iteration (total: 874, iterations: 1)
PASS : QCoreApplicationBenchmark::signal_slot_benchmark()
PASS : QCoreApplicationBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of QCoreApplicationBenchmark *********
after:
********* Start testing of QCoreApplicationBenchmark *********
Config: Using QTest library 5.0.0, Qt 5.0.0
PASS : QCoreApplicationBenchmark::initTestCase()
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"1000 events":
0.781 msecs per iteration (total: 100, iterations: 128)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"10000 events":
7.8 msecs per iteration (total: 63, iterations: 8)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"100000 events":
75 msecs per iteration (total: 75, iterations: 1)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"1000000 events":
774 msecs per iteration (total: 774, iterations: 1)
PASS : QCoreApplicationBenchmark::event_posting_benchmark()
PASS : QCoreApplicationBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of QCoreApplicationBenchmark *********
Change-Id: Ibf56d9526b0a8cbaf171008da4104bb457628172
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
2011-12-09 14:05:10 +00:00
|
|
|
// we must copy it first; we want to re-post the event
|
|
|
|
// with the event pointer intact, but we can't delay
|
|
|
|
// nulling the event ptr until after re-posting, as
|
|
|
|
// addEvent may invalidate pe.
|
|
|
|
QPostEvent pe_copy = pe;
|
|
|
|
|
|
|
|
// null out the event so if sendPostedEvents recurses, it
|
|
|
|
// will ignore this one, as it's been re-posted.
|
2011-04-27 10:05:43 +00:00
|
|
|
const_cast<QPostEvent &>(pe).event = 0;
|
Change event posting to use a QVector.
This provides a ~10% improvement to the newly introduced QCoreApplication
event_posting_benchmark (a simple synthetic benchmark of creating a bunch of
events, posting them, and sending the queue).
before:
********* Start testing of QCoreApplicationBenchmark *********
Config: Using QTest library 5.0.0, Qt 5.0.0
PASS : QCoreApplicationBenchmark::initTestCase()
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"1000":
0.82 msecs per iteration (total: 53, iterations: 64)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"10000":
8.6 msecs per iteration (total: 69, iterations: 8)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"100000":
84 msecs per iteration (total: 84, iterations: 1)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"1000000":
874 msecs per iteration (total: 874, iterations: 1)
PASS : QCoreApplicationBenchmark::signal_slot_benchmark()
PASS : QCoreApplicationBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of QCoreApplicationBenchmark *********
after:
********* Start testing of QCoreApplicationBenchmark *********
Config: Using QTest library 5.0.0, Qt 5.0.0
PASS : QCoreApplicationBenchmark::initTestCase()
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"1000 events":
0.781 msecs per iteration (total: 100, iterations: 128)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"10000 events":
7.8 msecs per iteration (total: 63, iterations: 8)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"100000 events":
75 msecs per iteration (total: 75, iterations: 1)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"1000000 events":
774 msecs per iteration (total: 774, iterations: 1)
PASS : QCoreApplicationBenchmark::event_posting_benchmark()
PASS : QCoreApplicationBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of QCoreApplicationBenchmark *********
Change-Id: Ibf56d9526b0a8cbaf171008da4104bb457628172
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
2011-12-09 14:05:10 +00:00
|
|
|
|
|
|
|
// re-post the copied event so it isn't lost
|
|
|
|
data->postEventList.addEvent(pe_copy);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// first, we diddle the event so that we can deliver
|
|
|
|
// it, and that no one will try to touch it later.
|
|
|
|
pe.event->posted = false;
|
2013-07-09 13:31:26 +00:00
|
|
|
QEvent *e = pe.event;
|
2011-04-27 10:05:43 +00:00
|
|
|
QObject * r = pe.receiver;
|
|
|
|
|
|
|
|
--r->d_func()->postedEvents;
|
|
|
|
Q_ASSERT(r->d_func()->postedEvents >= 0);
|
|
|
|
|
|
|
|
// next, update the data structure so that we're ready
|
|
|
|
// for the next event.
|
|
|
|
const_cast<QPostEvent &>(pe).event = 0;
|
|
|
|
|
2011-06-23 12:11:50 +00:00
|
|
|
struct MutexUnlocker
|
|
|
|
{
|
|
|
|
QMutexLocker &m;
|
|
|
|
MutexUnlocker(QMutexLocker &m) : m(m) { m.unlock(); }
|
|
|
|
~MutexUnlocker() { m.relock(); }
|
|
|
|
};
|
|
|
|
MutexUnlocker unlocker(locker);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-07-09 13:31:26 +00:00
|
|
|
QScopedPointer<QEvent> event_deleter(e); // will delete the event (with the mutex unlocked)
|
|
|
|
|
2011-06-23 12:11:50 +00:00
|
|
|
// after all that work, it's time to deliver the event.
|
2013-07-09 13:31:26 +00:00
|
|
|
QCoreApplication::sendEvent(r, e);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// careful when adding anything below this point - the
|
|
|
|
// sendEvent() call might invalidate any invariants this
|
|
|
|
// function depends on.
|
|
|
|
}
|
|
|
|
|
2011-06-23 12:11:50 +00:00
|
|
|
cleanup.exceptionCaught = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.3
|
|
|
|
|
|
|
|
Removes all events of the given \a eventType that were posted
|
|
|
|
using postEvent() for \a receiver.
|
|
|
|
|
|
|
|
The events are \e not dispatched, instead they are removed from
|
|
|
|
the queue. You should never need to call this function. If you do
|
|
|
|
call it, be aware that killing events may cause \a receiver to
|
|
|
|
break one or more invariants.
|
|
|
|
|
|
|
|
If \a receiver is null, the events of \a eventType are removed for
|
|
|
|
all objects. If \a eventType is 0, all the events are removed for
|
2012-02-19 09:15:18 +00:00
|
|
|
\a receiver. You should never call this function with \a eventType
|
|
|
|
of 0. If you do call it in this way, be aware that killing events
|
|
|
|
may cause \a receiver to break one or more invariants.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\threadsafe
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QCoreApplication::removePostedEvents(QObject *receiver, int eventType)
|
|
|
|
{
|
|
|
|
QThreadData *data = receiver ? receiver->d_func()->threadData : QThreadData::current();
|
|
|
|
QMutexLocker locker(&data->postEventList.mutex);
|
|
|
|
|
|
|
|
// the QObject destructor calls this function directly. this can
|
|
|
|
// happen while the event loop is in the middle of posting events,
|
|
|
|
// and when we get here, we may not have any more posted events
|
|
|
|
// for this object.
|
|
|
|
if (receiver && !receiver->d_func()->postedEvents)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//we will collect all the posted events for the QObject
|
|
|
|
//and we'll delete after the mutex was unlocked
|
|
|
|
QVarLengthArray<QEvent*> events;
|
|
|
|
int n = data->postEventList.size();
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
const QPostEvent &pe = data->postEventList.at(i);
|
|
|
|
|
|
|
|
if ((!receiver || pe.receiver == receiver)
|
|
|
|
&& (pe.event && (eventType == 0 || pe.event->type() == eventType))) {
|
|
|
|
--pe.receiver->d_func()->postedEvents;
|
|
|
|
pe.event->posted = false;
|
|
|
|
events.append(pe.event);
|
|
|
|
const_cast<QPostEvent &>(pe).event = 0;
|
|
|
|
} else if (!data->postEventList.recursion) {
|
|
|
|
if (i != j)
|
Change event posting to use a QVector.
This provides a ~10% improvement to the newly introduced QCoreApplication
event_posting_benchmark (a simple synthetic benchmark of creating a bunch of
events, posting them, and sending the queue).
before:
********* Start testing of QCoreApplicationBenchmark *********
Config: Using QTest library 5.0.0, Qt 5.0.0
PASS : QCoreApplicationBenchmark::initTestCase()
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"1000":
0.82 msecs per iteration (total: 53, iterations: 64)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"10000":
8.6 msecs per iteration (total: 69, iterations: 8)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"100000":
84 msecs per iteration (total: 84, iterations: 1)
RESULT : QCoreApplicationBenchmark::signal_slot_benchmark():"1000000":
874 msecs per iteration (total: 874, iterations: 1)
PASS : QCoreApplicationBenchmark::signal_slot_benchmark()
PASS : QCoreApplicationBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of QCoreApplicationBenchmark *********
after:
********* Start testing of QCoreApplicationBenchmark *********
Config: Using QTest library 5.0.0, Qt 5.0.0
PASS : QCoreApplicationBenchmark::initTestCase()
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"1000 events":
0.781 msecs per iteration (total: 100, iterations: 128)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"10000 events":
7.8 msecs per iteration (total: 63, iterations: 8)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"100000 events":
75 msecs per iteration (total: 75, iterations: 1)
RESULT : QCoreApplicationBenchmark::event_posting_benchmark():"1000000 events":
774 msecs per iteration (total: 774, iterations: 1)
PASS : QCoreApplicationBenchmark::event_posting_benchmark()
PASS : QCoreApplicationBenchmark::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of QCoreApplicationBenchmark *********
Change-Id: Ibf56d9526b0a8cbaf171008da4104bb457628172
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
2011-12-09 14:05:10 +00:00
|
|
|
qSwap(data->postEventList[i], data->postEventList[j]);
|
2011-04-27 10:05:43 +00:00
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef QT_DEBUG
|
|
|
|
if (receiver && eventType == 0) {
|
|
|
|
Q_ASSERT(!receiver->d_func()->postedEvents);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!data->postEventList.recursion) {
|
|
|
|
// truncate list
|
|
|
|
data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
locker.unlock();
|
|
|
|
for (int i = 0; i < events.count(); ++i) {
|
|
|
|
delete events[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Removes \a event from the queue of posted events, and emits a
|
|
|
|
warning message if appropriate.
|
|
|
|
|
|
|
|
\warning This function can be \e really slow. Avoid using it, if
|
|
|
|
possible.
|
|
|
|
|
|
|
|
\threadsafe
|
|
|
|
*/
|
|
|
|
|
|
|
|
void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
|
|
|
|
{
|
|
|
|
if (!event || !event->posted)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QThreadData *data = QThreadData::current();
|
|
|
|
|
|
|
|
QMutexLocker locker(&data->postEventList.mutex);
|
|
|
|
|
|
|
|
if (data->postEventList.size() == 0) {
|
|
|
|
#if defined(QT_DEBUG)
|
|
|
|
qDebug("QCoreApplication::removePostedEvent: Internal error: %p %d is posted",
|
|
|
|
(void*)event, event->type());
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < data->postEventList.size(); ++i) {
|
|
|
|
const QPostEvent & pe = data->postEventList.at(i);
|
|
|
|
if (pe.event == event) {
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
qWarning("QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s",
|
|
|
|
event->type(),
|
|
|
|
pe.receiver->metaObject()->className(),
|
|
|
|
pe.receiver->objectName().toLocal8Bit().data());
|
|
|
|
#endif
|
|
|
|
--pe.receiver->d_func()->postedEvents;
|
|
|
|
pe.event->posted = false;
|
|
|
|
delete pe.event;
|
|
|
|
const_cast<QPostEvent &>(pe).event = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!\reimp
|
|
|
|
|
|
|
|
*/
|
|
|
|
bool QCoreApplication::event(QEvent *e)
|
|
|
|
{
|
|
|
|
if (e->type() == QEvent::Quit) {
|
|
|
|
quit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return QObject::event(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \enum QCoreApplication::Encoding
|
2012-05-16 21:09:56 +00:00
|
|
|
\obsolete
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-16 21:09:56 +00:00
|
|
|
This enum type used to define the 8-bit encoding of character string
|
|
|
|
arguments to translate(). This enum is now obsolete and UTF-8 will be
|
|
|
|
used in all cases.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-01-31 22:33:30 +00:00
|
|
|
\value UnicodeUTF8 UTF-8.
|
2012-08-16 13:00:31 +00:00
|
|
|
\omitvalue Latin1
|
|
|
|
\omitvalue DefaultCodec UTF-8.
|
|
|
|
\omitvalue CodecForTr
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-16 21:09:56 +00:00
|
|
|
\sa QObject::tr(), QString::fromUtf8()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-10-26 11:29:51 +00:00
|
|
|
void QCoreApplicationPrivate::ref()
|
|
|
|
{
|
|
|
|
quitLockRef.ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QCoreApplicationPrivate::deref()
|
|
|
|
{
|
2012-02-11 00:33:55 +00:00
|
|
|
if (!quitLockRef.deref())
|
|
|
|
maybeQuit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QCoreApplicationPrivate::maybeQuit()
|
|
|
|
{
|
|
|
|
if (quitLockRef.load() == 0 && in_exec && quitLockRefEnabled && shouldQuit())
|
|
|
|
QCoreApplication::postEvent(QCoreApplication::instance(), new QEvent(QEvent::Quit));
|
2011-10-26 11:29:51 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Tells the application to exit with return code 0 (success).
|
|
|
|
Equivalent to calling QCoreApplication::exit(0).
|
|
|
|
|
2015-08-24 07:26:57 +00:00
|
|
|
It's common to connect the QGuiApplication::lastWindowClosed() signal
|
2011-04-27 10:05:43 +00:00
|
|
|
to quit(), and you also often connect e.g. QAbstractButton::clicked() or
|
|
|
|
signals in QAction, QMenu, or QMenuBar to it.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-08-24 07:26:57 +00:00
|
|
|
\sa exit(), aboutToQuit(), QGuiApplication::lastWindowClosed()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void QCoreApplication::quit()
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn void QCoreApplication::aboutToQuit()
|
|
|
|
|
|
|
|
This signal is emitted when the application is about to quit the
|
|
|
|
main event loop, e.g. when the event loop level drops to zero.
|
|
|
|
This may happen either after a call to quit() from inside the
|
2015-11-20 09:27:40 +00:00
|
|
|
application or when the user shuts down the entire desktop session.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The signal is particularly useful if your application has to do some
|
|
|
|
last-second cleanup. Note that no user interaction is possible in
|
|
|
|
this state.
|
|
|
|
|
|
|
|
\sa quit()
|
|
|
|
*/
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif // QT_NO_QOBJECT
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#ifndef QT_NO_TRANSLATION
|
|
|
|
/*!
|
|
|
|
Adds the translation file \a translationFile to the list of
|
|
|
|
translation files to be used for translations.
|
|
|
|
|
|
|
|
Multiple translation files can be installed. Translations are
|
|
|
|
searched for in the reverse order in which they were installed,
|
|
|
|
so the most recently installed translation file is searched first
|
|
|
|
and the first translation file installed is searched last.
|
|
|
|
The search stops as soon as a translation containing a matching
|
|
|
|
string is found.
|
|
|
|
|
|
|
|
Installing or removing a QTranslator, or changing an installed QTranslator
|
|
|
|
generates a \l{QEvent::LanguageChange}{LanguageChange} event for the
|
|
|
|
QCoreApplication instance. A QApplication instance will propagate the event
|
|
|
|
to all toplevel windows, where a reimplementation of changeEvent can
|
|
|
|
re-translate the user interface by passing user-visible strings via the
|
|
|
|
tr() function to the respective property setters. User-interface classes
|
2012-08-24 21:17:13 +00:00
|
|
|
generated by Qt Designer provide a \c retranslateUi() function that can be
|
2011-04-27 10:05:43 +00:00
|
|
|
called.
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
The function returns \c true on success and false on failure.
|
2012-03-20 07:47:57 +00:00
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa removeTranslator(), translate(), QTranslator::load(), {Dynamic Translation}
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-20 07:47:57 +00:00
|
|
|
bool QCoreApplication::installTranslator(QTranslator *translationFile)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (!translationFile)
|
2012-03-20 07:47:57 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (!QCoreApplicationPrivate::checkInstance("installTranslator"))
|
2012-03-20 07:47:57 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplicationPrivate *d = self->d_func();
|
|
|
|
d->translators.prepend(translationFile);
|
|
|
|
|
|
|
|
#ifndef QT_NO_TRANSLATION_BUILDER
|
|
|
|
if (translationFile->isEmpty())
|
2012-03-20 07:47:57 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2011-04-27 10:05:43 +00:00
|
|
|
QEvent ev(QEvent::LanguageChange);
|
|
|
|
QCoreApplication::sendEvent(self, &ev);
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
|
|
|
|
2012-03-20 07:47:57 +00:00
|
|
|
return true;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Removes the translation file \a translationFile from the list of
|
|
|
|
translation files used by this application. (It does not delete the
|
|
|
|
translation file from the file system.)
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
The function returns \c true on success and false on failure.
|
2012-03-20 07:47:57 +00:00
|
|
|
|
2012-05-10 11:10:23 +00:00
|
|
|
\sa installTranslator(), translate(), QObject::tr()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-20 07:47:57 +00:00
|
|
|
bool QCoreApplication::removeTranslator(QTranslator *translationFile)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (!translationFile)
|
2012-03-20 07:47:57 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!QCoreApplicationPrivate::checkInstance("removeTranslator"))
|
2012-03-20 07:47:57 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
QCoreApplicationPrivate *d = self->d_func();
|
2012-11-01 16:50:44 +00:00
|
|
|
if (d->translators.removeAll(translationFile)) {
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2012-11-01 16:50:44 +00:00
|
|
|
if (!self->closingDown()) {
|
|
|
|
QEvent ev(QEvent::LanguageChange);
|
|
|
|
QCoreApplication::sendEvent(self, &ev);
|
|
|
|
}
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2012-03-20 07:47:57 +00:00
|
|
|
return true;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2012-03-20 07:47:57 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void replacePercentN(QString *result, int n)
|
|
|
|
{
|
|
|
|
if (n >= 0) {
|
|
|
|
int percentPos = 0;
|
|
|
|
int len = 0;
|
|
|
|
while ((percentPos = result->indexOf(QLatin1Char('%'), percentPos + len)) != -1) {
|
|
|
|
len = 1;
|
|
|
|
QString fmt;
|
|
|
|
if (result->at(percentPos + len) == QLatin1Char('L')) {
|
|
|
|
++len;
|
|
|
|
fmt = QLatin1String("%L1");
|
|
|
|
} else {
|
|
|
|
fmt = QLatin1String("%1");
|
|
|
|
}
|
|
|
|
if (result->at(percentPos + len) == QLatin1Char('n')) {
|
|
|
|
fmt = fmt.arg(n);
|
|
|
|
++len;
|
|
|
|
result->replace(percentPos, len, fmt);
|
|
|
|
len = fmt.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\reentrant
|
|
|
|
|
|
|
|
Returns the translation text for \a sourceText, by querying the
|
|
|
|
installed translation files. The translation files are searched
|
|
|
|
from the most recently installed file back to the first
|
|
|
|
installed file.
|
|
|
|
|
2012-05-16 21:09:56 +00:00
|
|
|
QObject::tr() provides this functionality more conveniently.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\a context is typically a class name (e.g., "MyDialog") and \a
|
|
|
|
sourceText is either English text or a short identifying text.
|
|
|
|
|
|
|
|
\a disambiguation is an identifying string, for when the same \a
|
|
|
|
sourceText is used in different roles within the same context. By
|
|
|
|
default, it is null.
|
|
|
|
|
|
|
|
See the \l QTranslator and \l QObject::tr() documentation for
|
|
|
|
more information about contexts, disambiguations and comments.
|
|
|
|
|
|
|
|
\a n is used in conjunction with \c %n to support plural forms.
|
|
|
|
See QObject::tr() for details.
|
|
|
|
|
|
|
|
If none of the translation files contain a translation for \a
|
|
|
|
sourceText in \a context, this function returns a QString
|
2012-05-16 21:09:56 +00:00
|
|
|
equivalent of \a sourceText.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
This function is not virtual. You can use alternative translation
|
|
|
|
techniques by subclassing \l QTranslator.
|
|
|
|
|
|
|
|
\warning This method is reentrant only if all translators are
|
|
|
|
installed \e before calling this method. Installing or removing
|
|
|
|
translators while performing translations is not supported. Doing
|
|
|
|
so will most likely result in crashes or other undesirable
|
|
|
|
behavior.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa QObject::tr(), installTranslator()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
QString QCoreApplication::translate(const char *context, const char *sourceText,
|
2012-05-16 21:09:56 +00:00
|
|
|
const char *disambiguation, int n)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
|
|
|
|
if (!sourceText)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
if (self && !self->d_func()->translators.isEmpty()) {
|
|
|
|
QList<QTranslator*>::ConstIterator it;
|
|
|
|
QTranslator *translationFile;
|
|
|
|
for (it = self->d_func()->translators.constBegin(); it != self->d_func()->translators.constEnd(); ++it) {
|
|
|
|
translationFile = *it;
|
|
|
|
result = translationFile->translate(context, sourceText, disambiguation, n);
|
2011-09-19 18:26:25 +00:00
|
|
|
if (!result.isNull())
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-16 21:09:56 +00:00
|
|
|
if (result.isNull())
|
|
|
|
result = QString::fromUtf8(sourceText);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
replacePercentN(&result, n);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-16 21:57:02 +00:00
|
|
|
/*! \fn static QString QCoreApplication::translate(const char *context, const char *key, const char *disambiguation, Encoding encoding, int n = -1)
|
2012-08-16 13:00:31 +00:00
|
|
|
|
|
|
|
\obsolete
|
|
|
|
*/
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// Declared in qglobal.h
|
|
|
|
QString qtTrId(const char *id, int n)
|
|
|
|
{
|
2012-05-16 21:09:56 +00:00
|
|
|
return QCoreApplication::translate(0, id, 0, n);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
|
|
|
|
{
|
|
|
|
return QCoreApplication::self
|
|
|
|
&& QCoreApplication::self->d_func()->translators.contains(translator);
|
|
|
|
}
|
|
|
|
|
2012-12-06 20:07:39 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
QString QCoreApplication::translate(const char *context, const char *sourceText,
|
|
|
|
const char *disambiguation, int n)
|
|
|
|
{
|
|
|
|
Q_UNUSED(context)
|
|
|
|
Q_UNUSED(disambiguation)
|
|
|
|
QString ret = QString::fromUtf8(sourceText);
|
|
|
|
if (n >= 0)
|
|
|
|
ret.replace(QLatin1String("%n"), QString::number(n));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif //QT_NO_TRANSLATE
|
|
|
|
|
2013-11-22 13:31:05 +00:00
|
|
|
// Makes it possible to point QCoreApplication to a custom location to ensure
|
|
|
|
// the directory is added to the patch, and qt.conf and deployed plugins are
|
|
|
|
// found from there. This is for use cases in which QGuiApplication is
|
|
|
|
// instantiated by a library and not by an application executable, for example,
|
|
|
|
// Active X servers.
|
|
|
|
|
|
|
|
void QCoreApplicationPrivate::setApplicationFilePath(const QString &path)
|
|
|
|
{
|
|
|
|
if (QCoreApplicationPrivate::cachedApplicationFilePath)
|
|
|
|
*QCoreApplicationPrivate::cachedApplicationFilePath = path;
|
|
|
|
else
|
|
|
|
QCoreApplicationPrivate::cachedApplicationFilePath = new QString(path);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Returns the directory that contains the application executable.
|
|
|
|
|
2011-12-20 06:44:19 +00:00
|
|
|
For example, if you have installed Qt in the \c{C:\Qt}
|
2011-04-27 10:05:43 +00:00
|
|
|
directory, and you run the \c{regexp} example, this function will
|
2011-12-20 06:44:19 +00:00
|
|
|
return "C:/Qt/examples/tools/regexp".
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-11-20 09:27:40 +00:00
|
|
|
On OS X and iOS this will point to the directory actually containing
|
|
|
|
the executable, which may be inside an application bundle (if the
|
2011-04-27 10:05:43 +00:00
|
|
|
application is bundled).
|
|
|
|
|
|
|
|
\warning On Linux, this function will try to get the path from the
|
|
|
|
\c {/proc} file system. If that fails, it assumes that \c
|
|
|
|
{argv[0]} contains the absolute file name of the executable. The
|
|
|
|
function also assumes that the current directory has not been
|
|
|
|
changed by the application.
|
|
|
|
|
|
|
|
\sa applicationFilePath()
|
|
|
|
*/
|
|
|
|
QString QCoreApplication::applicationDirPath()
|
|
|
|
{
|
|
|
|
if (!self) {
|
|
|
|
qWarning("QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QCoreApplicationPrivate *d = self->d_func();
|
|
|
|
if (d->cachedApplicationDirPath.isNull())
|
|
|
|
d->cachedApplicationDirPath = QFileInfo(applicationFilePath()).path();
|
|
|
|
return d->cachedApplicationDirPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the file path of the application executable.
|
|
|
|
|
|
|
|
For example, if you have installed Qt in the \c{/usr/local/qt}
|
|
|
|
directory, and you run the \c{regexp} example, this function will
|
|
|
|
return "/usr/local/qt/examples/tools/regexp/regexp".
|
|
|
|
|
|
|
|
\warning On Linux, this function will try to get the path from the
|
|
|
|
\c {/proc} file system. If that fails, it assumes that \c
|
|
|
|
{argv[0]} contains the absolute file name of the executable. The
|
|
|
|
function also assumes that the current directory has not been
|
|
|
|
changed by the application.
|
|
|
|
|
|
|
|
\sa applicationDirPath()
|
|
|
|
*/
|
|
|
|
QString QCoreApplication::applicationFilePath()
|
|
|
|
{
|
|
|
|
if (!self) {
|
|
|
|
qWarning("QCoreApplication::applicationFilePath: Please instantiate the QApplication object first");
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QCoreApplicationPrivate *d = self->d_func();
|
2013-10-08 22:55:39 +00:00
|
|
|
|
2015-06-11 08:23:52 +00:00
|
|
|
if (d->argc) {
|
|
|
|
static const char *procName = d->argv[0];
|
|
|
|
if (qstrcmp(procName, d->argv[0]) != 0) {
|
|
|
|
// clear the cache if the procname changes, so we reprocess it.
|
|
|
|
QCoreApplicationPrivate::clearApplicationFilePath();
|
|
|
|
procName = d->argv[0];
|
|
|
|
}
|
2013-10-08 22:55:39 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 13:31:05 +00:00
|
|
|
if (QCoreApplicationPrivate::cachedApplicationFilePath)
|
|
|
|
return *QCoreApplicationPrivate::cachedApplicationFilePath;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-05-24 07:14:43 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2013-11-22 13:31:05 +00:00
|
|
|
QCoreApplicationPrivate::setApplicationFilePath(QFileInfo(qAppFileName()).filePath());
|
|
|
|
return *QCoreApplicationPrivate::cachedApplicationFilePath;
|
2011-10-21 07:55:40 +00:00
|
|
|
#elif defined(Q_OS_MAC)
|
2011-04-27 10:05:43 +00:00
|
|
|
QString qAppFileName_str = qAppFileName();
|
|
|
|
if(!qAppFileName_str.isEmpty()) {
|
|
|
|
QFileInfo fi(qAppFileName_str);
|
2013-11-22 13:31:05 +00:00
|
|
|
if (fi.exists()) {
|
|
|
|
QCoreApplicationPrivate::setApplicationFilePath(fi.canonicalFilePath());
|
|
|
|
return *QCoreApplicationPrivate::cachedApplicationFilePath;
|
|
|
|
}
|
|
|
|
return QString();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-01-23 20:21:40 +00:00
|
|
|
#if defined( Q_OS_UNIX )
|
2013-08-07 11:59:27 +00:00
|
|
|
# if defined(Q_OS_LINUX) && (!defined(Q_OS_ANDROID) || defined(Q_OS_ANDROID_NO_SDK))
|
2011-04-27 10:05:43 +00:00
|
|
|
// Try looking for a /proc/<pid>/exe symlink first which points to
|
|
|
|
// the absolute path of the executable
|
|
|
|
QFileInfo pfi(QString::fromLatin1("/proc/%1/exe").arg(getpid()));
|
|
|
|
if (pfi.exists() && pfi.isSymLink()) {
|
2013-11-22 13:31:05 +00:00
|
|
|
QCoreApplicationPrivate::setApplicationFilePath(pfi.canonicalFilePath());
|
|
|
|
return *QCoreApplicationPrivate::cachedApplicationFilePath;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
# endif
|
2013-05-27 08:23:07 +00:00
|
|
|
if (!arguments().isEmpty()) {
|
|
|
|
QString argv0 = QFile::decodeName(arguments().at(0).toLocal8Bit());
|
|
|
|
QString absPath;
|
|
|
|
|
|
|
|
if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) {
|
|
|
|
/*
|
|
|
|
If argv0 starts with a slash, it is already an absolute
|
|
|
|
file path.
|
|
|
|
*/
|
|
|
|
absPath = argv0;
|
|
|
|
} else if (argv0.contains(QLatin1Char('/'))) {
|
|
|
|
/*
|
|
|
|
If argv0 contains one or more slashes, it is a file path
|
|
|
|
relative to the current directory.
|
|
|
|
*/
|
|
|
|
absPath = QDir::current().absoluteFilePath(argv0);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
Otherwise, the file path has to be determined using the
|
|
|
|
PATH environment variable.
|
|
|
|
*/
|
|
|
|
absPath = QStandardPaths::findExecutable(argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
absPath = QDir::cleanPath(absPath);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-05-27 08:23:07 +00:00
|
|
|
QFileInfo fi(absPath);
|
2013-11-22 13:31:05 +00:00
|
|
|
if (fi.exists()) {
|
|
|
|
QCoreApplicationPrivate::setApplicationFilePath(fi.canonicalFilePath());
|
|
|
|
return *QCoreApplicationPrivate::cachedApplicationFilePath;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 13:31:05 +00:00
|
|
|
return QString();
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
2013-11-22 13:31:05 +00:00
|
|
|
Q_UNREACHABLE();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.4
|
|
|
|
|
|
|
|
Returns the current process ID for the application.
|
|
|
|
*/
|
|
|
|
qint64 QCoreApplication::applicationPid()
|
|
|
|
{
|
2013-09-13 09:22:45 +00:00
|
|
|
#if defined(Q_OS_WIN)
|
2011-04-27 10:05:43 +00:00
|
|
|
return GetCurrentProcessId();
|
|
|
|
#elif defined(Q_OS_VXWORKS)
|
|
|
|
return (pid_t) taskIdCurrent;
|
|
|
|
#else
|
|
|
|
return getpid();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 4.1
|
|
|
|
|
|
|
|
Returns the list of command-line arguments.
|
|
|
|
|
|
|
|
Usually arguments().at(0) is the program name, arguments().at(1)
|
|
|
|
is the first argument, and arguments().last() is the last
|
|
|
|
argument. See the note below about Windows.
|
|
|
|
|
|
|
|
Calling this function is slow - you should store the result in a variable
|
|
|
|
when parsing the command line.
|
|
|
|
|
|
|
|
\warning On Unix, this list is built from the argc and argv parameters passed
|
|
|
|
to the constructor in the main() function. The string-data in argv is
|
|
|
|
interpreted using QString::fromLocal8Bit(); hence it is not possible to
|
|
|
|
pass, for example, Japanese command line arguments on a system that runs in a
|
|
|
|
Latin1 locale. Most modern Unix systems do not have this limitation, as they are
|
|
|
|
Unicode-based.
|
|
|
|
|
2015-01-09 13:52:02 +00:00
|
|
|
On Windows, the list is built from the argc and argv parameters only if
|
|
|
|
modified argv/argc parameters are passed to the constructor. In that case,
|
|
|
|
encoding problems might occur.
|
|
|
|
|
|
|
|
Otherwise, the arguments() are constructed from the return value of
|
2011-04-27 10:05:43 +00:00
|
|
|
\l{http://msdn2.microsoft.com/en-us/library/ms683156(VS.85).aspx}{GetCommandLine()}.
|
|
|
|
As a result of this, the string given by arguments().at(0) might not be
|
|
|
|
the program name on Windows, depending on how the application was started.
|
|
|
|
|
2013-08-24 09:15:19 +00:00
|
|
|
\sa applicationFilePath(), QCommandLineParser
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QStringList QCoreApplication::arguments()
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
|
|
|
|
if (!self) {
|
|
|
|
qWarning("QCoreApplication::arguments: Please instantiate the QApplication object first");
|
|
|
|
return list;
|
|
|
|
}
|
2012-05-16 07:39:39 +00:00
|
|
|
const int ac = self->d_func()->argc;
|
|
|
|
char ** const av = self->d_func()->argv;
|
|
|
|
list.reserve(ac);
|
|
|
|
|
2013-09-13 09:22:45 +00:00
|
|
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
2012-05-16 07:39:39 +00:00
|
|
|
// On Windows, it is possible to pass Unicode arguments on
|
|
|
|
// the command line. To restore those, we split the command line
|
|
|
|
// and filter out arguments that were deleted by derived application
|
|
|
|
// classes by index.
|
2011-04-27 10:05:43 +00:00
|
|
|
QString cmdline = QString::fromWCharArray(GetCommandLine());
|
|
|
|
|
|
|
|
#if defined(Q_OS_WINCE)
|
|
|
|
wchar_t tempFilename[MAX_PATH+1];
|
|
|
|
if (GetModuleFileName(0, tempFilename, MAX_PATH)) {
|
|
|
|
tempFilename[MAX_PATH] = 0;
|
|
|
|
cmdline.prepend(QLatin1Char('\"') + QString::fromWCharArray(tempFilename) + QLatin1String("\" "));
|
|
|
|
}
|
|
|
|
#endif // Q_OS_WINCE
|
|
|
|
|
2015-02-09 16:34:06 +00:00
|
|
|
const QCoreApplicationPrivate *d = self->d_func();
|
|
|
|
if (d->origArgv) {
|
2015-01-09 13:52:02 +00:00
|
|
|
const QStringList allArguments = qWinCmdArgs(cmdline);
|
2015-02-09 16:34:06 +00:00
|
|
|
Q_ASSERT(allArguments.size() == d->origArgc);
|
|
|
|
for (int i = 0; i < d->origArgc; ++i) {
|
|
|
|
if (contains(ac, av, d->origArgv[i]))
|
2015-01-09 13:52:02 +00:00
|
|
|
list.append(allArguments.at(i));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
} // Fall back to rebuilding from argv/argc when a modified argv was passed.
|
|
|
|
#endif // defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
2012-05-16 07:39:39 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int a = 0; a < ac; ++a) {
|
|
|
|
list << QString::fromLocal8Bit(av[a]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\property QCoreApplication::organizationName
|
|
|
|
\brief the name of the organization that wrote this application
|
|
|
|
|
|
|
|
The value is used by the QSettings class when it is constructed
|
|
|
|
using the empty constructor. This saves having to repeat this
|
|
|
|
information each time a QSettings object is created.
|
|
|
|
|
2014-08-25 13:56:19 +00:00
|
|
|
On Mac, QSettings uses \l {QCoreApplication::}{organizationDomain()} as the organization
|
2011-04-27 10:05:43 +00:00
|
|
|
if it's not an empty string; otherwise it uses
|
|
|
|
organizationName(). On all other platforms, QSettings uses
|
|
|
|
organizationName() as the organization.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa organizationDomain, applicationName
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-25 21:37:37 +00:00
|
|
|
/*!
|
|
|
|
\fn void QCoreApplication::organizationNameChanged()
|
|
|
|
\internal
|
|
|
|
|
|
|
|
While not useful from C++ due to how organizationName is normally set once on
|
|
|
|
startup, this is still needed for QML so that bindings are reevaluated after
|
|
|
|
that initial change.
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
void QCoreApplication::setOrganizationName(const QString &orgName)
|
|
|
|
{
|
2013-02-25 21:37:37 +00:00
|
|
|
if (coreappdata()->orgName == orgName)
|
|
|
|
return;
|
2011-04-27 10:05:43 +00:00
|
|
|
coreappdata()->orgName = orgName;
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2013-02-25 21:37:37 +00:00
|
|
|
if (QCoreApplication::self)
|
|
|
|
emit QCoreApplication::self->organizationNameChanged();
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QCoreApplication::organizationName()
|
|
|
|
{
|
|
|
|
return coreappdata()->orgName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\property QCoreApplication::organizationDomain
|
|
|
|
\brief the Internet domain of the organization that wrote this application
|
|
|
|
|
|
|
|
The value is used by the QSettings class when it is constructed
|
|
|
|
using the empty constructor. This saves having to repeat this
|
|
|
|
information each time a QSettings object is created.
|
|
|
|
|
|
|
|
On Mac, QSettings uses organizationDomain() as the organization
|
|
|
|
if it's not an empty string; otherwise it uses organizationName().
|
|
|
|
On all other platforms, QSettings uses organizationName() as the
|
|
|
|
organization.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa organizationName, applicationName, applicationVersion
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2013-02-25 21:37:37 +00:00
|
|
|
/*!
|
|
|
|
\fn void QCoreApplication::organizationDomainChanged()
|
|
|
|
\internal
|
|
|
|
|
|
|
|
Primarily for QML, see organizationNameChanged.
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
void QCoreApplication::setOrganizationDomain(const QString &orgDomain)
|
|
|
|
{
|
2013-02-25 21:37:37 +00:00
|
|
|
if (coreappdata()->orgDomain == orgDomain)
|
|
|
|
return;
|
2011-04-27 10:05:43 +00:00
|
|
|
coreappdata()->orgDomain = orgDomain;
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2013-02-25 21:37:37 +00:00
|
|
|
if (QCoreApplication::self)
|
|
|
|
emit QCoreApplication::self->organizationDomainChanged();
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QCoreApplication::organizationDomain()
|
|
|
|
{
|
|
|
|
return coreappdata()->orgDomain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\property QCoreApplication::applicationName
|
|
|
|
\brief the name of this application
|
|
|
|
|
|
|
|
The value is used by the QSettings class when it is constructed
|
|
|
|
using the empty constructor. This saves having to repeat this
|
|
|
|
information each time a QSettings object is created.
|
|
|
|
|
2012-03-20 17:01:20 +00:00
|
|
|
If not set, the application name defaults to the executable name (since 5.0).
|
|
|
|
|
2013-09-17 15:50:28 +00:00
|
|
|
\sa organizationName, organizationDomain, applicationVersion, applicationFilePath()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2013-02-25 21:37:37 +00:00
|
|
|
/*!
|
|
|
|
\fn void QCoreApplication::applicationNameChanged()
|
|
|
|
\internal
|
|
|
|
|
|
|
|
Primarily for QML, see organizationNameChanged.
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
void QCoreApplication::setApplicationName(const QString &application)
|
|
|
|
{
|
2015-03-29 20:09:56 +00:00
|
|
|
coreappdata()->applicationNameSet = !application.isEmpty();
|
2015-02-22 18:35:43 +00:00
|
|
|
QString newAppName = application;
|
|
|
|
if (newAppName.isEmpty() && QCoreApplication::self)
|
|
|
|
newAppName = QCoreApplication::self->d_func()->appName();
|
|
|
|
if (coreappdata()->application == newAppName)
|
2013-02-25 21:37:37 +00:00
|
|
|
return;
|
2015-02-22 18:35:43 +00:00
|
|
|
coreappdata()->application = newAppName;
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2013-02-25 21:37:37 +00:00
|
|
|
if (QCoreApplication::self)
|
|
|
|
emit QCoreApplication::self->applicationNameChanged();
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QCoreApplication::applicationName()
|
2012-03-20 17:01:20 +00:00
|
|
|
{
|
2015-02-22 18:35:43 +00:00
|
|
|
return coreappdata() ? coreappdata()->application : QString();
|
2012-03-20 17:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exported for QDesktopServices (Qt4 behavior compatibility)
|
|
|
|
Q_CORE_EXPORT QString qt_applicationName_noFallback()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2015-03-29 20:09:56 +00:00
|
|
|
return coreappdata()->applicationNameSet ? coreappdata()->application : QString();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\property QCoreApplication::applicationVersion
|
|
|
|
\since 4.4
|
|
|
|
\brief the version of this application
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa applicationName, organizationName, organizationDomain
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2013-02-25 21:37:37 +00:00
|
|
|
/*!
|
|
|
|
\fn void QCoreApplication::applicationVersionChanged()
|
|
|
|
\internal
|
|
|
|
|
|
|
|
Primarily for QML, see organizationNameChanged.
|
|
|
|
*/
|
2011-04-27 10:05:43 +00:00
|
|
|
void QCoreApplication::setApplicationVersion(const QString &version)
|
|
|
|
{
|
2013-02-25 21:37:37 +00:00
|
|
|
if (coreappdata()->applicationVersion == version)
|
|
|
|
return;
|
2011-04-27 10:05:43 +00:00
|
|
|
coreappdata()->applicationVersion = version;
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
2013-02-25 21:37:37 +00:00
|
|
|
if (QCoreApplication::self)
|
|
|
|
emit QCoreApplication::self->applicationVersionChanged();
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QCoreApplication::applicationVersion()
|
|
|
|
{
|
|
|
|
return coreappdata()->applicationVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef QT_NO_LIBRARY
|
|
|
|
|
|
|
|
Q_GLOBAL_STATIC_WITH_ARGS(QMutex, libraryPathMutex, (QMutex::Recursive))
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a list of paths that the application will search when
|
|
|
|
dynamically loading libraries.
|
|
|
|
|
2015-03-09 16:25:28 +00:00
|
|
|
The return value of this function may change when a QCoreApplication
|
|
|
|
is created. It is not recommended to call it before creating a
|
|
|
|
QCoreApplication. The directory of the application executable (\b not
|
|
|
|
the working directory) is part of the list if it is known. In order
|
|
|
|
to make it known a QCoreApplication has to be constructed as it will
|
|
|
|
use \c {argv[0]} to find it.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Qt provides default library paths, but they can also be set using
|
|
|
|
a \l{Using qt.conf}{qt.conf} file. Paths specified in this file
|
2015-03-09 16:25:28 +00:00
|
|
|
will override default values. Note that if the qt.conf file is in
|
|
|
|
the directory of the application executable, it may not be found
|
|
|
|
until a QCoreApplication is created. If it is not found when calling
|
|
|
|
this function, the default library paths will be used.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-03-09 16:25:28 +00:00
|
|
|
The list will include the installation directory for plugins if
|
2011-04-27 10:05:43 +00:00
|
|
|
it exists (the default installation directory for plugins is \c
|
|
|
|
INSTALL/plugins, where \c INSTALL is the directory where Qt was
|
2015-03-09 16:25:28 +00:00
|
|
|
installed). The colon separated entries of the \c QT_PLUGIN_PATH
|
|
|
|
environment variable are always added. The plugin installation
|
|
|
|
directory (and its existence) may change when the directory of
|
|
|
|
the application executable becomes known.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
If you want to iterate over the list, you can use the \l foreach
|
|
|
|
pseudo-keyword:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa setLibraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary,
|
|
|
|
{How to Create Qt Plugins}
|
|
|
|
*/
|
|
|
|
QStringList QCoreApplication::libraryPaths()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(libraryPathMutex());
|
2015-03-09 12:45:44 +00:00
|
|
|
|
|
|
|
if (coreappdata()->manual_libpaths)
|
|
|
|
return *(coreappdata()->manual_libpaths);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!coreappdata()->app_libpaths) {
|
2015-03-26 09:20:11 +00:00
|
|
|
QStringList *app_libpaths = new QStringList;
|
|
|
|
coreappdata()->app_libpaths.reset(app_libpaths);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const QByteArray libPathEnv = qgetenv("QT_PLUGIN_PATH");
|
|
|
|
if (!libPathEnv.isEmpty()) {
|
2015-07-28 19:22:30 +00:00
|
|
|
QStringList paths = QFile::decodeName(libPathEnv).split(QDir::listSeparator(), QString::SkipEmptyParts);
|
2011-04-27 10:05:43 +00:00
|
|
|
for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
|
|
|
|
QString canonicalPath = QDir(*it).canonicalPath();
|
|
|
|
if (!canonicalPath.isEmpty()
|
|
|
|
&& !app_libpaths->contains(canonicalPath)) {
|
|
|
|
app_libpaths->append(canonicalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 09:49:55 +00:00
|
|
|
|
|
|
|
QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath);
|
|
|
|
if (QFile::exists(installPathPlugins)) {
|
|
|
|
// Make sure we convert from backslashes to slashes.
|
|
|
|
installPathPlugins = QDir(installPathPlugins).canonicalPath();
|
|
|
|
if (!app_libpaths->contains(installPathPlugins))
|
|
|
|
app_libpaths->append(installPathPlugins);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If QCoreApplication is not yet instantiated,
|
|
|
|
// make sure we add the application path when we construct the QCoreApplication
|
|
|
|
if (self) self->d_func()->appendApplicationPathToLibraryPaths();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
return *(coreappdata()->app_libpaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
|
|
Sets the list of directories to search when loading libraries to
|
|
|
|
\a paths. All existing paths will be deleted and the path list
|
|
|
|
will consist of the paths given in \a paths.
|
|
|
|
|
2015-03-09 12:45:44 +00:00
|
|
|
The library paths are reset to the default when an instance of
|
|
|
|
QCoreApplication is destructed.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa libraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary
|
|
|
|
*/
|
|
|
|
void QCoreApplication::setLibraryPaths(const QStringList &paths)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(libraryPathMutex());
|
2015-03-09 12:45:44 +00:00
|
|
|
|
|
|
|
// setLibraryPaths() is considered a "remove everything and then add some new ones" operation.
|
|
|
|
// When the application is constructed it should still amend the paths. So we keep the originals
|
|
|
|
// around, and even create them if they don't exist, yet.
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!coreappdata()->app_libpaths)
|
2015-03-09 12:45:44 +00:00
|
|
|
libraryPaths();
|
|
|
|
|
2015-03-26 09:20:11 +00:00
|
|
|
if (coreappdata()->manual_libpaths)
|
|
|
|
*(coreappdata()->manual_libpaths) = paths;
|
|
|
|
else
|
|
|
|
coreappdata()->manual_libpaths.reset(new QStringList(paths));
|
2015-03-09 12:45:44 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
locker.unlock();
|
|
|
|
QFactoryLoader::refreshAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Prepends \a path to the beginning of the library path list, ensuring that
|
|
|
|
it is searched for libraries first. If \a path is empty or already in the
|
|
|
|
path list, the path list is not changed.
|
|
|
|
|
|
|
|
The default path list consists of a single entry, the installation
|
|
|
|
directory for plugins. The default installation directory for plugins
|
|
|
|
is \c INSTALL/plugins, where \c INSTALL is the directory where Qt was
|
|
|
|
installed.
|
|
|
|
|
2015-03-09 12:45:44 +00:00
|
|
|
The library paths are reset to the default when an instance of
|
|
|
|
QCoreApplication is destructed.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa removeLibraryPath(), libraryPaths(), setLibraryPaths()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::addLibraryPath(const QString &path)
|
|
|
|
{
|
|
|
|
if (path.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2015-03-09 12:45:44 +00:00
|
|
|
QString canonicalPath = QDir(path).canonicalPath();
|
|
|
|
if (canonicalPath.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QMutexLocker locker(libraryPathMutex());
|
|
|
|
|
2015-03-26 09:20:11 +00:00
|
|
|
QStringList *libpaths = coreappdata()->manual_libpaths.data();
|
2015-03-09 12:45:44 +00:00
|
|
|
if (libpaths) {
|
|
|
|
if (libpaths->contains(canonicalPath))
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// make sure that library paths are initialized
|
|
|
|
libraryPaths();
|
2015-03-26 09:20:11 +00:00
|
|
|
QStringList *app_libpaths = coreappdata()->app_libpaths.data();
|
2015-03-09 12:45:44 +00:00
|
|
|
if (app_libpaths->contains(canonicalPath))
|
|
|
|
return;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-03-26 09:20:11 +00:00
|
|
|
coreappdata()->manual_libpaths.reset(libpaths = new QStringList(*app_libpaths));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2015-03-09 12:45:44 +00:00
|
|
|
|
|
|
|
libpaths->prepend(canonicalPath);
|
|
|
|
locker.unlock();
|
|
|
|
QFactoryLoader::refreshAll();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Removes \a path from the library path list. If \a path is empty or not
|
|
|
|
in the path list, the list is not changed.
|
|
|
|
|
2015-03-09 12:45:44 +00:00
|
|
|
The library paths are reset to the default when an instance of
|
|
|
|
QCoreApplication is destructed.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa addLibraryPath(), libraryPaths(), setLibraryPaths()
|
|
|
|
*/
|
|
|
|
void QCoreApplication::removeLibraryPath(const QString &path)
|
|
|
|
{
|
|
|
|
if (path.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2015-03-09 12:45:44 +00:00
|
|
|
QString canonicalPath = QDir(path).canonicalPath();
|
|
|
|
if (canonicalPath.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QMutexLocker locker(libraryPathMutex());
|
|
|
|
|
2015-03-26 09:20:11 +00:00
|
|
|
QStringList *libpaths = coreappdata()->manual_libpaths.data();
|
2015-03-09 12:45:44 +00:00
|
|
|
if (libpaths) {
|
|
|
|
if (libpaths->removeAll(canonicalPath) == 0)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// make sure that library paths is initialized
|
|
|
|
libraryPaths();
|
2015-03-26 09:20:11 +00:00
|
|
|
QStringList *app_libpaths = coreappdata()->app_libpaths.data();
|
2015-03-09 12:45:44 +00:00
|
|
|
if (!app_libpaths->contains(canonicalPath))
|
|
|
|
return;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-03-26 09:20:11 +00:00
|
|
|
coreappdata()->manual_libpaths.reset(libpaths = new QStringList(*app_libpaths));
|
2015-03-09 12:45:44 +00:00
|
|
|
libpaths->removeAll(canonicalPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
locker.unlock();
|
2011-04-27 10:05:43 +00:00
|
|
|
QFactoryLoader::refreshAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //QT_NO_LIBRARY
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#ifndef QT_NO_QOBJECT
|
|
|
|
|
2012-06-23 19:48:53 +00:00
|
|
|
/*!
|
|
|
|
Installs an event filter \a filterObj for all native events
|
|
|
|
received by the application in the main thread.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-08-25 13:56:19 +00:00
|
|
|
The event filter \a filterObj receives events via its \l {QAbstractNativeEventFilter::}{nativeEventFilter()}
|
2012-06-23 19:48:53 +00:00
|
|
|
function, which is called for all native events received in the main thread.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-11-20 09:27:40 +00:00
|
|
|
The QAbstractNativeEventFilter::nativeEventFilter() function should
|
|
|
|
return true if the event should be filtered, i.e. stopped. It should
|
|
|
|
return false to allow normal Qt processing to continue: the native
|
|
|
|
event can then be translated into a QEvent and handled by the standard
|
|
|
|
Qt \l{QEvent} {event} filtering, e.g. QObject::installEventFilter().
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-06-23 19:48:53 +00:00
|
|
|
If multiple event filters are installed, the filter that was
|
|
|
|
installed last is activated first.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\note The filter function set here receives native messages,
|
2012-06-23 19:48:53 +00:00
|
|
|
i.e. MSG or XCB event structs.
|
|
|
|
|
2015-11-20 09:27:40 +00:00
|
|
|
\note Native event filters will be disabled in the application when the
|
2015-08-31 11:16:21 +00:00
|
|
|
Qt::AA_PluginApplication attribute is set.
|
2014-09-01 12:53:14 +00:00
|
|
|
|
2015-01-21 14:42:19 +00:00
|
|
|
For maximum portability, you should always try to use QEvent
|
2012-06-23 19:48:53 +00:00
|
|
|
and QObject::installEventFilter() whenever possible.
|
|
|
|
|
|
|
|
\sa QObject::installEventFilter()
|
|
|
|
|
|
|
|
\since 5.0
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2012-06-23 19:48:53 +00:00
|
|
|
void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2015-08-31 11:16:21 +00:00
|
|
|
if (QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
|
|
|
|
qWarning("Native event filters are not applied when the Qt::AA_PluginApplication attribute is set");
|
2014-09-01 12:53:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-23 19:48:53 +00:00
|
|
|
QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread);
|
|
|
|
if (!filterObj || !eventDispatcher)
|
|
|
|
return;
|
|
|
|
eventDispatcher->installNativeEventFilter(filterObj);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2012-10-16 10:55:49 +00:00
|
|
|
Removes an event \a filterObject from this object. The
|
2012-06-23 19:48:53 +00:00
|
|
|
request is ignored if such an event filter has not been installed.
|
|
|
|
|
|
|
|
All event filters for this object are automatically removed when
|
|
|
|
this object is destroyed.
|
|
|
|
|
|
|
|
It is always safe to remove an event filter, even during event
|
|
|
|
filter activation (i.e. from the nativeEventFilter() function).
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-09-04 04:48:13 +00:00
|
|
|
\sa installNativeEventFilter()
|
2012-06-23 19:48:53 +00:00
|
|
|
\since 5.0
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
2012-10-16 10:55:49 +00:00
|
|
|
void QCoreApplication::removeNativeEventFilter(QAbstractNativeEventFilter *filterObject)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2012-06-23 19:48:53 +00:00
|
|
|
QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance();
|
2012-10-16 10:55:49 +00:00
|
|
|
if (!filterObject || !eventDispatcher)
|
2012-06-23 19:48:53 +00:00
|
|
|
return;
|
2012-10-16 10:55:49 +00:00
|
|
|
eventDispatcher->removeNativeEventFilter(filterObject);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
Deprecate {QCoreApp,QAbstractEventDispatcher}::hasPendingEvents()
This function has a flawed design. It was flawed when it was added in
Qt 3.0.
A "false" return value is racy: any other thread running may post events
to the current thread, thus making the result stale. That includes Qt
starts for its own purposes when it comes to the main thread, like the
Scene Graph thread, the QProcessManager thread, the Windows
QAdoptedThread watcher thread, the Windows pipe writer thread, etc.
A "true" return is stable only if the selected thread is stopped, which
includes selecting the current thread (the case of QCoreApplication).
For that reason, this method should not be public, but a protected one
so that a public static could call it. But even that would not solve the
race condition from the previous paragraph (hence why
QCoreApplication::hasPendingEvents being deprecated too).
And, to top all of that off, all but one of the implementations access
the GUI thread's event loop counter in a non-thread-safe manner. I've
changed the documentation to restrict to the only currently-working use-
application.
[ChangeLog][QtCore][Event loop] QCoreApplication::hasPendingEvents and
QAbstractEventDispatcher::hasPendingEvents are now deprecated. Please
refer to the documentation for more information.
Task-number: QTBUG-36611
Change-Id: Iac61f307e9672839944ae2f75abb1aea30c419f6
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2014-02-03 23:37:42 +00:00
|
|
|
\deprecated
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
This function returns \c true if there are pending events; otherwise
|
|
|
|
returns \c false. Pending events can be either from the window
|
2011-04-27 10:05:43 +00:00
|
|
|
system or posted events using postEvent().
|
|
|
|
|
Deprecate {QCoreApp,QAbstractEventDispatcher}::hasPendingEvents()
This function has a flawed design. It was flawed when it was added in
Qt 3.0.
A "false" return value is racy: any other thread running may post events
to the current thread, thus making the result stale. That includes Qt
starts for its own purposes when it comes to the main thread, like the
Scene Graph thread, the QProcessManager thread, the Windows
QAdoptedThread watcher thread, the Windows pipe writer thread, etc.
A "true" return is stable only if the selected thread is stopped, which
includes selecting the current thread (the case of QCoreApplication).
For that reason, this method should not be public, but a protected one
so that a public static could call it. But even that would not solve the
race condition from the previous paragraph (hence why
QCoreApplication::hasPendingEvents being deprecated too).
And, to top all of that off, all but one of the implementations access
the GUI thread's event loop counter in a non-thread-safe manner. I've
changed the documentation to restrict to the only currently-working use-
application.
[ChangeLog][QtCore][Event loop] QCoreApplication::hasPendingEvents and
QAbstractEventDispatcher::hasPendingEvents are now deprecated. Please
refer to the documentation for more information.
Task-number: QTBUG-36611
Change-Id: Iac61f307e9672839944ae2f75abb1aea30c419f6
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
2014-02-03 23:37:42 +00:00
|
|
|
\note this function is not thread-safe. It may only be called in the main
|
|
|
|
thread and only if there are no other threads running in the application
|
|
|
|
(including threads Qt starts for its own purposes).
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
\sa QAbstractEventDispatcher::hasPendingEvents()
|
|
|
|
*/
|
2014-11-25 08:47:43 +00:00
|
|
|
#if QT_DEPRECATED_SINCE(5, 3)
|
2011-04-27 10:05:43 +00:00
|
|
|
bool QCoreApplication::hasPendingEvents()
|
|
|
|
{
|
|
|
|
QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance();
|
|
|
|
if (eventDispatcher)
|
|
|
|
return eventDispatcher->hasPendingEvents();
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-25 08:47:43 +00:00
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-09-08 15:40:55 +00:00
|
|
|
/*!
|
|
|
|
Returns a pointer to the event dispatcher object for the main thread. If no
|
|
|
|
event dispatcher exists for the thread, this function returns 0.
|
|
|
|
*/
|
|
|
|
QAbstractEventDispatcher *QCoreApplication::eventDispatcher()
|
|
|
|
{
|
|
|
|
if (QCoreApplicationPrivate::theMainThread)
|
2015-08-01 13:50:00 +00:00
|
|
|
return QCoreApplicationPrivate::theMainThread.load()->eventDispatcher();
|
2011-09-08 15:40:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets the event dispatcher for the main thread to \a eventDispatcher. This
|
|
|
|
is only possible as long as there is no event dispatcher installed yet. That
|
|
|
|
is, before QCoreApplication has been instantiated. This method takes
|
|
|
|
ownership of the object.
|
|
|
|
*/
|
|
|
|
void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
|
|
|
|
{
|
|
|
|
QThread *mainThread = QCoreApplicationPrivate::theMainThread;
|
|
|
|
if (!mainThread)
|
|
|
|
mainThread = QThread::currentThread(); // will also setup theMainThread
|
|
|
|
mainThread->setEventDispatcher(eventDispatcher);
|
|
|
|
}
|
|
|
|
|
2012-12-06 19:02:08 +00:00
|
|
|
#endif // QT_NO_QOBJECT
|
|
|
|
|
2013-01-14 11:58:31 +00:00
|
|
|
/*!
|
|
|
|
\macro Q_COREAPP_STARTUP_FUNCTION(QtStartUpFunction ptr)
|
|
|
|
\since 5.1
|
|
|
|
\relates QCoreApplication
|
|
|
|
\reentrant
|
|
|
|
|
|
|
|
Adds a global function that will be called from the QCoreApplication
|
|
|
|
constructor. This macro is normally used to initialize libraries
|
|
|
|
for program-wide functionality, without requiring the application to
|
|
|
|
call into the library for initialization.
|
|
|
|
|
|
|
|
The function specified by \a ptr should take no arguments and should
|
|
|
|
return nothing. For example:
|
|
|
|
|
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 3
|
|
|
|
|
|
|
|
Note that the startup function will run at the end of the QCoreApplication constructor,
|
|
|
|
before any GUI initialization. If GUI code is required in the function,
|
|
|
|
use a timer (or a queued invocation) to perform the initialization later on,
|
|
|
|
from the event loop.
|
|
|
|
|
|
|
|
If QCoreApplication is deleted and another QCoreApplication is created,
|
|
|
|
the startup function will be invoked again.
|
|
|
|
*/
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn void qAddPostRoutine(QtCleanUpFunction ptr)
|
|
|
|
\relates QCoreApplication
|
|
|
|
|
2013-12-29 04:51:27 +00:00
|
|
|
Adds a global routine that will be called from the QCoreApplication
|
2011-04-27 10:05:43 +00:00
|
|
|
destructor. This function is normally used to add cleanup routines
|
|
|
|
for program-wide functionality.
|
|
|
|
|
2013-12-29 04:51:27 +00:00
|
|
|
The cleanup routines are called in the reverse order of their addition.
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
The function specified by \a ptr should take no arguments and should
|
|
|
|
return nothing. For example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 4
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2014-08-25 13:56:19 +00:00
|
|
|
Note that for an application- or module-wide cleanup, qaddPostRoutine()
|
|
|
|
is often not suitable. For example, if the program is split into dynamically
|
|
|
|
loaded modules, the relevant module may be unloaded long before the
|
|
|
|
QCoreApplication destructor is called. In such cases, if using qaddPostRoutine()
|
|
|
|
is still desirable, qRemovePostRoutine() can be used to prevent a routine
|
|
|
|
from being called by the QCoreApplication destructor. For example, if that
|
2013-12-29 04:51:27 +00:00
|
|
|
routine was called before the module was unloaded.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
For modules and libraries, using a reference-counted
|
|
|
|
initialization manager or Qt's parent-child deletion mechanism may
|
|
|
|
be better. Here is an example of a private class that uses the
|
|
|
|
parent-child mechanism to call a cleanup function at the right
|
|
|
|
time:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 5
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
By selecting the right parent object, this can often be made to
|
|
|
|
clean up the module's data at the right moment.
|
2013-12-29 04:51:27 +00:00
|
|
|
|
|
|
|
\sa qRemovePostRoutine()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn void qRemovePostRoutine(QtCleanUpFunction ptr)
|
|
|
|
\relates QCoreApplication
|
|
|
|
\since 5.3
|
|
|
|
|
|
|
|
Removes the cleanup routine specified by \a ptr from the list of
|
|
|
|
routines called by the QCoreApplication destructor. The routine
|
|
|
|
must have been previously added to the list by a call to
|
|
|
|
qAddPostRoutine(), otherwise this function has no effect.
|
|
|
|
|
|
|
|
\sa qAddPostRoutine()
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\macro Q_DECLARE_TR_FUNCTIONS(context)
|
|
|
|
\relates QCoreApplication
|
|
|
|
|
|
|
|
The Q_DECLARE_TR_FUNCTIONS() macro declares and implements two
|
|
|
|
translation functions, \c tr() and \c trUtf8(), with these
|
|
|
|
signatures:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 6
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
This macro is useful if you want to use QObject::tr() or
|
|
|
|
QObject::trUtf8() in classes that don't inherit from QObject.
|
|
|
|
|
|
|
|
Q_DECLARE_TR_FUNCTIONS() must appear at the very top of the
|
|
|
|
class definition (before the first \c{public:} or \c{protected:}).
|
|
|
|
For example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_kernel_qcoreapplication.cpp 7
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The \a context parameter is normally the class name, but it can
|
2015-11-20 09:27:40 +00:00
|
|
|
be any text.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa Q_OBJECT, QObject::tr(), QObject::trUtf8()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|