author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qcoreapplication.h" |
|
43 |
#include "qcoreapplication_p.h" |
|
44 |
||
45 |
#include "qabstracteventdispatcher.h" |
|
46 |
#include "qcoreevent.h" |
|
47 |
#include "qeventloop.h" |
|
48 |
#include "qcorecmdlineargs_p.h" |
|
49 |
#include <qdatastream.h> |
|
50 |
#include <qdebug.h> |
|
51 |
#include <qdir.h> |
|
52 |
#include <qfile.h> |
|
53 |
#include <qfileinfo.h> |
|
54 |
#include <qhash.h> |
|
55 |
#include <private/qprocess_p.h> |
|
56 |
#include <qtextcodec.h> |
|
57 |
#include <qthread.h> |
|
58 |
#include <qthreadpool.h> |
|
59 |
#include <qthreadstorage.h> |
|
60 |
#include <private/qthread_p.h> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
61 |
#include <qelapsedtimer.h> |
0 | 62 |
#include <qlibraryinfo.h> |
63 |
#include <qvarlengtharray.h> |
|
64 |
#include <private/qfactoryloader_p.h> |
|
65 |
#include <private/qfunctions_p.h> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
66 |
#include <private/qlocale_p.h> |
0 | 67 |
|
68 |
#ifdef Q_OS_SYMBIAN |
|
69 |
# include <exception> |
|
70 |
# include <f32file.h> |
|
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
71 |
# include <e32ldr.h> |
0 | 72 |
# include "qeventdispatcher_symbian_p.h" |
73 |
# include "private/qcore_symbian_p.h" |
|
74 |
#elif defined(Q_OS_UNIX) |
|
75 |
# if !defined(QT_NO_GLIB) |
|
76 |
# include "qeventdispatcher_glib_p.h" |
|
77 |
# endif |
|
78 |
# include "qeventdispatcher_unix_p.h" |
|
79 |
#endif |
|
80 |
||
81 |
#ifdef Q_OS_WIN |
|
82 |
# include "qeventdispatcher_win_p.h" |
|
83 |
#endif |
|
84 |
||
85 |
#ifdef Q_OS_MAC |
|
86 |
# include "qcore_mac_p.h" |
|
87 |
#endif |
|
88 |
||
89 |
#include <stdlib.h> |
|
90 |
||
91 |
#ifdef Q_OS_UNIX |
|
92 |
# include <locale.h> |
|
93 |
#endif |
|
94 |
||
95 |
#ifdef Q_OS_VXWORKS |
|
96 |
# include <taskLib.h> |
|
97 |
#endif |
|
98 |
||
99 |
QT_BEGIN_NAMESPACE |
|
100 |
||
101 |
class QMutexUnlocker |
|
102 |
{ |
|
103 |
public: |
|
104 |
inline explicit QMutexUnlocker(QMutex *m) |
|
105 |
: mtx(m) |
|
106 |
{ } |
|
107 |
inline ~QMutexUnlocker() { unlock(); } |
|
108 |
inline void unlock() { if (mtx) mtx->unlock(); mtx = 0; } |
|
109 |
||
110 |
private: |
|
111 |
Q_DISABLE_COPY(QMutexUnlocker) |
|
112 |
||
113 |
QMutex *mtx; |
|
114 |
}; |
|
115 |
||
116 |
#ifdef Q_OS_SYMBIAN |
|
117 |
typedef TDriveNumber (*SystemDriveFunc)(RFs&); |
|
118 |
static SystemDriveFunc PtrGetSystemDrive=0; |
|
119 |
#endif |
|
120 |
||
121 |
#if defined(Q_WS_WIN) || defined(Q_WS_MAC) |
|
122 |
extern QString qAppFileName(); |
|
123 |
#endif |
|
124 |
||
125 |
#if !defined(Q_OS_WIN) |
|
126 |
#ifdef Q_OS_MAC |
|
127 |
QString QCoreApplicationPrivate::macMenuBarName() |
|
128 |
{ |
|
129 |
QString bundleName; |
|
130 |
CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), CFSTR("CFBundleName")); |
|
131 |
if (string) |
|
132 |
bundleName = QCFString::toQString(static_cast<CFStringRef>(string)); |
|
133 |
return bundleName; |
|
134 |
} |
|
135 |
#endif |
|
136 |
QString QCoreApplicationPrivate::appName() const |
|
137 |
{ |
|
138 |
static QString applName; |
|
139 |
#ifdef Q_OS_MAC |
|
140 |
applName = macMenuBarName(); |
|
141 |
#endif |
|
142 |
if (applName.isEmpty() && argv[0]) { |
|
143 |
char *p = strrchr(argv[0], '/'); |
|
144 |
applName = QString::fromLocal8Bit(p ? p + 1 : argv[0]); |
|
145 |
} |
|
146 |
return applName; |
|
147 |
} |
|
148 |
#endif |
|
149 |
||
150 |
bool QCoreApplicationPrivate::checkInstance(const char *function) |
|
151 |
{ |
|
152 |
bool b = (QCoreApplication::self != 0); |
|
153 |
if (!b) |
|
154 |
qWarning("QApplication::%s: Please instantiate the QApplication object first", function); |
|
155 |
return b; |
|
156 |
} |
|
157 |
||
158 |
// Support for introspection |
|
159 |
||
160 |
QSignalSpyCallbackSet Q_CORE_EXPORT qt_signal_spy_callback_set = { 0, 0, 0, 0 }; |
|
161 |
||
162 |
void qt_register_signal_spy_callbacks(const QSignalSpyCallbackSet &callback_set) |
|
163 |
{ |
|
164 |
qt_signal_spy_callback_set = callback_set; |
|
165 |
} |
|
166 |
||
167 |
extern "C" void Q_CORE_EXPORT qt_startup_hook() |
|
168 |
{ |
|
169 |
} |
|
170 |
||
171 |
typedef QList<QtCleanUpFunction> QVFuncList; |
|
172 |
Q_GLOBAL_STATIC(QVFuncList, postRList) |
|
173 |
||
174 |
void qAddPostRoutine(QtCleanUpFunction p) |
|
175 |
{ |
|
176 |
QVFuncList *list = postRList(); |
|
177 |
if (!list) |
|
178 |
return; |
|
179 |
list->prepend(p); |
|
180 |
} |
|
181 |
||
182 |
void qRemovePostRoutine(QtCleanUpFunction p) |
|
183 |
{ |
|
184 |
QVFuncList *list = postRList(); |
|
185 |
if (!list) |
|
186 |
return; |
|
187 |
list->removeAll(p); |
|
188 |
} |
|
189 |
||
190 |
void Q_CORE_EXPORT qt_call_post_routines() |
|
191 |
{ |
|
192 |
QVFuncList *list = 0; |
|
193 |
QT_TRY { |
|
194 |
list = postRList(); |
|
195 |
} QT_CATCH(const std::bad_alloc &) { |
|
196 |
// ignore - if we can't allocate a post routine list, |
|
197 |
// there's a high probability that there's no post |
|
198 |
// routine to be executed :) |
|
199 |
} |
|
200 |
if (!list) |
|
201 |
return; |
|
202 |
while (!list->isEmpty()) |
|
203 |
(list->takeFirst())(); |
|
204 |
} |
|
205 |
||
206 |
||
207 |
// app starting up if false |
|
208 |
bool QCoreApplicationPrivate::is_app_running = false; |
|
209 |
// app closing down if true |
|
210 |
bool QCoreApplicationPrivate::is_app_closing = false; |
|
211 |
// initialized in qcoreapplication and in qtextstream autotest when setlocale is called. |
|
212 |
Q_CORE_EXPORT bool qt_locale_initialized = false; |
|
213 |
||
214 |
||
215 |
Q_CORE_EXPORT uint qGlobalPostedEventsCount() |
|
216 |
{ |
|
217 |
QThreadData *currentThreadData = QThreadData::current(); |
|
218 |
return currentThreadData->postEventList.size() - currentThreadData->postEventList.startOffset; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
void qt_set_current_thread_to_main_thread() |
|
223 |
{ |
|
224 |
QCoreApplicationPrivate::theMainThread = QThread::currentThread(); |
|
225 |
} |
|
226 |
||
227 |
||
228 |
||
229 |
QCoreApplication *QCoreApplication::self = 0; |
|
230 |
QAbstractEventDispatcher *QCoreApplicationPrivate::eventDispatcher = 0; |
|
231 |
uint QCoreApplicationPrivate::attribs; |
|
232 |
||
233 |
#ifdef Q_OS_UNIX |
|
234 |
Qt::HANDLE qt_application_thread_id = 0; |
|
235 |
#endif |
|
236 |
||
237 |
struct QCoreApplicationData { |
|
238 |
QCoreApplicationData() { |
|
239 |
#ifndef QT_NO_LIBRARY |
|
240 |
app_libpaths = 0; |
|
241 |
#endif |
|
242 |
} |
|
243 |
~QCoreApplicationData() { |
|
244 |
#ifndef QT_NO_LIBRARY |
|
245 |
delete app_libpaths; |
|
246 |
#endif |
|
247 |
||
248 |
// cleanup the QAdoptedThread created for the main() thread |
|
249 |
if (QCoreApplicationPrivate::theMainThread) { |
|
250 |
QThreadData *data = QThreadData::get2(QCoreApplicationPrivate::theMainThread); |
|
251 |
QCoreApplicationPrivate::theMainThread = 0; |
|
252 |
data->deref(); // deletes the data and the adopted thread |
|
253 |
} |
|
254 |
} |
|
255 |
QString orgName, orgDomain, application; |
|
256 |
QString applicationVersion; |
|
257 |
||
258 |
#ifndef QT_NO_LIBRARY |
|
259 |
QStringList *app_libpaths; |
|
260 |
#endif |
|
261 |
||
262 |
}; |
|
263 |
||
264 |
Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata) |
|
265 |
||
266 |
QCoreApplicationPrivate::QCoreApplicationPrivate(int &aargc, char **aargv) |
|
267 |
: QObjectPrivate(), argc(aargc), argv(aargv), application_type(0), eventFilter(0), |
|
268 |
in_exec(false), aboutToQuitEmitted(false) |
|
269 |
{ |
|
270 |
static const char *const empty = ""; |
|
271 |
if (argc == 0 || argv == 0) { |
|
272 |
argc = 0; |
|
273 |
argv = (char **)∅ // ouch! careful with QCoreApplication::argv()! |
|
274 |
} |
|
275 |
QCoreApplicationPrivate::is_app_closing = false; |
|
276 |
||
277 |
#ifdef Q_OS_UNIX |
|
278 |
qt_application_thread_id = QThread::currentThreadId(); |
|
279 |
#endif |
|
280 |
||
281 |
// note: this call to QThread::currentThread() may end up setting theMainThread! |
|
282 |
if (QThread::currentThread() != theMainThread) |
|
283 |
qWarning("WARNING: QApplication was not created in the main() thread."); |
|
284 |
} |
|
285 |
||
286 |
QCoreApplicationPrivate::~QCoreApplicationPrivate() |
|
287 |
{ |
|
288 |
if (threadData) { |
|
289 |
#ifndef QT_NO_THREAD |
|
290 |
void *data = &threadData->tls; |
|
291 |
QThreadStorageData::finish((void **)data); |
|
292 |
#endif |
|
293 |
||
294 |
// need to clear the state of the mainData, just in case a new QCoreApplication comes along. |
|
295 |
QMutexLocker locker(&threadData->postEventList.mutex); |
|
296 |
for (int i = 0; i < threadData->postEventList.size(); ++i) { |
|
297 |
const QPostEvent &pe = threadData->postEventList.at(i); |
|
298 |
if (pe.event) { |
|
299 |
--pe.receiver->d_func()->postedEvents; |
|
300 |
pe.event->posted = false; |
|
301 |
delete pe.event; |
|
302 |
} |
|
303 |
} |
|
304 |
threadData->postEventList.clear(); |
|
305 |
threadData->postEventList.recursion = 0; |
|
306 |
threadData->quitNow = false; |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
void QCoreApplicationPrivate::createEventDispatcher() |
|
311 |
{ |
|
312 |
Q_Q(QCoreApplication); |
|
313 |
#if defined(Q_OS_SYMBIAN) |
|
314 |
eventDispatcher = new QEventDispatcherSymbian(q); |
|
315 |
#elif defined(Q_OS_UNIX) |
|
316 |
# if !defined(QT_NO_GLIB) |
|
317 |
if (qgetenv("QT_NO_GLIB").isEmpty() && QEventDispatcherGlib::versionSupported()) |
|
318 |
eventDispatcher = new QEventDispatcherGlib(q); |
|
319 |
else |
|
320 |
# endif |
|
321 |
eventDispatcher = new QEventDispatcherUNIX(q); |
|
322 |
#elif defined(Q_OS_WIN) |
|
323 |
eventDispatcher = new QEventDispatcherWin32(q); |
|
324 |
#else |
|
325 |
# error "QEventDispatcher not yet ported to this platform" |
|
326 |
#endif |
|
327 |
} |
|
328 |
||
329 |
QThread *QCoreApplicationPrivate::theMainThread = 0; |
|
330 |
QThread *QCoreApplicationPrivate::mainThread() |
|
331 |
{ |
|
332 |
Q_ASSERT(theMainThread != 0); |
|
333 |
return theMainThread; |
|
334 |
} |
|
335 |
||
336 |
#if !defined (QT_NO_DEBUG) || defined (QT_MAC_FRAMEWORK_BUILD) |
|
337 |
void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver) |
|
338 |
{ |
|
339 |
QThread *currentThread = QThread::currentThread(); |
|
340 |
QThread *thr = receiver->thread(); |
|
341 |
Q_ASSERT_X(currentThread == thr || !thr, |
|
342 |
"QCoreApplication::sendEvent", |
|
343 |
QString::fromLatin1("Cannot send events to objects owned by a different thread. " |
|
344 |
"Current thread %1. Receiver '%2' (of type '%3') was created in thread %4") |
|
345 |
.arg(QString::number((quintptr) currentThread, 16)) |
|
346 |
.arg(receiver->objectName()) |
|
347 |
.arg(QLatin1String(receiver->metaObject()->className())) |
|
348 |
.arg(QString::number((quintptr) thr, 16)) |
|
349 |
.toLocal8Bit().data()); |
|
350 |
Q_UNUSED(currentThread); |
|
351 |
Q_UNUSED(thr); |
|
352 |
} |
|
353 |
#elif defined(Q_OS_SYMBIAN) && defined (QT_NO_DEBUG) |
|
354 |
// no implementation in release builds, but keep the symbol present |
|
355 |
void QCoreApplicationPrivate::checkReceiverThread(QObject * /* receiver */) |
|
356 |
{ |
|
357 |
} |
|
358 |
#endif |
|
359 |
||
360 |
void QCoreApplicationPrivate::appendApplicationPathToLibraryPaths() |
|
361 |
{ |
|
362 |
#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) |
|
363 |
QStringList *app_libpaths = coreappdata()->app_libpaths; |
|
364 |
Q_ASSERT(app_libpaths); |
|
365 |
# if defined(Q_OS_SYMBIAN) |
|
366 |
QString app_location( QCoreApplication::applicationDirPath() ); |
|
367 |
// File existence check for application's private dir requires additional '\' or |
|
368 |
// platform security will not allow it. |
|
369 |
if (app_location != QLibraryInfo::location(QLibraryInfo::PluginsPath) && QFile::exists(app_location + QLatin1Char('\\')) && !app_libpaths->contains(app_location)) |
|
370 |
# else |
|
371 |
QString app_location( QCoreApplication::applicationFilePath() ); |
|
372 |
app_location.truncate(app_location.lastIndexOf(QLatin1Char('/'))); |
|
373 |
app_location = QDir(app_location).canonicalPath(); |
|
374 |
if (QFile::exists(app_location) && !app_libpaths->contains(app_location)) |
|
375 |
# endif |
|
376 |
app_libpaths->append(app_location); |
|
377 |
#endif |
|
378 |
} |
|
379 |
||
380 |
QString qAppName() |
|
381 |
{ |
|
382 |
if (!QCoreApplicationPrivate::checkInstance("qAppName")) |
|
383 |
return QString(); |
|
384 |
return QCoreApplication::instance()->d_func()->appName(); |
|
385 |
} |
|
386 |
||
387 |
/*! |
|
388 |
\class QCoreApplication |
|
389 |
\brief The QCoreApplication class provides an event loop for console Qt |
|
390 |
applications. |
|
391 |
||
392 |
This class is used by non-GUI applications to provide their event |
|
393 |
loop. For non-GUI application that uses Qt, there should be exactly |
|
394 |
one QCoreApplication object. For GUI applications, see |
|
395 |
QApplication. |
|
396 |
||
397 |
QCoreApplication contains the main event loop, where all events |
|
398 |
from the operating system (e.g., timer and network events) and |
|
399 |
other sources are processed and dispatched. It also handles the |
|
400 |
application's initialization and finalization, as well as |
|
401 |
system-wide and application-wide settings. |
|
402 |
||
403 |
\section1 The Event Loop and Event Handling |
|
404 |
||
405 |
The event loop is started with a call to exec(). Long running |
|
406 |
operations can call processEvents() to keep the application |
|
407 |
responsive. |
|
408 |
||
409 |
Some Qt classes, such as QString, can be used without a |
|
410 |
QCoreApplication object. However, in general, we recommend that |
|
411 |
you create a QCoreApplication or a QApplication object in your \c |
|
412 |
main() function as early as possible. exit() will not return |
|
413 |
until the event loop exits; e.g., when quit() is called. |
|
414 |
||
415 |
Several static convenience functions are also provided. The |
|
416 |
QCoreApplication object is available from instance(). Events can |
|
417 |
be sent or posted using sendEvent(), postEvent(), and |
|
418 |
sendPostedEvents(). Pending events can be removed with |
|
419 |
removePostedEvents() or flushed with flush(). |
|
420 |
||
421 |
The class provides a quit() slot and an aboutToQuit() signal. |
|
422 |
||
423 |
\section1 Application and Library Paths |
|
424 |
||
425 |
An application has an applicationDirPath() and an |
|
426 |
applicationFilePath(). Library paths (see QLibrary) can be retrieved |
|
427 |
with libraryPaths() and manipulated by setLibraryPaths(), addLibraryPath(), |
|
428 |
and removeLibraryPath(). |
|
429 |
||
430 |
\section1 Internationalization and Translations |
|
431 |
||
432 |
Translation files can be added or removed |
|
433 |
using installTranslator() and removeTranslator(). Application |
|
434 |
strings can be translated using translate(). The QObject::tr() |
|
435 |
and QObject::trUtf8() functions are implemented in terms of |
|
436 |
translate(). |
|
437 |
||
438 |
\section1 Accessing Command Line Arguments |
|
439 |
||
440 |
The command line arguments which are passed to QCoreApplication's |
|
441 |
constructor should be accessed using the arguments() function. |
|
442 |
Note that some arguments supplied by the user may have been |
|
443 |
processed and removed by QCoreApplication. |
|
444 |
||
445 |
In cases where command line arguments need to be obtained using the |
|
446 |
argv() function, you must convert them from the local string encoding |
|
447 |
using QString::fromLocal8Bit(). |
|
448 |
||
449 |
\section1 Locale Settings |
|
450 |
||
451 |
On Unix/Linux Qt is configured to use the system locale settings by |
|
452 |
default. This can cause a conflict when using POSIX functions, for |
|
453 |
instance, when converting between data types such as floats and |
|
454 |
strings, since the notation may differ between locales. To get |
|
455 |
around this problem, call the POSIX function \c{setlocale(LC_NUMERIC,"C")} |
|
456 |
right after initializing QApplication or QCoreApplication to reset |
|
457 |
the locale that is used for number formatting to "C"-locale. |
|
458 |
||
459 |
\sa QApplication, QAbstractEventDispatcher, QEventLoop, |
|
460 |
{Semaphores Example}, {Wait Conditions Example} |
|
461 |
*/ |
|
462 |
||
463 |
/*! |
|
464 |
\fn static QCoreApplication *QCoreApplication::instance() |
|
465 |
||
466 |
Returns a pointer to the application's QCoreApplication (or |
|
467 |
QApplication) instance. |
|
468 |
||
469 |
If no instance has been allocated, \c null is returned. |
|
470 |
*/ |
|
471 |
||
472 |
/*!\internal |
|
473 |
*/ |
|
474 |
QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p) |
|
475 |
: QObject(p, 0) |
|
476 |
{ |
|
477 |
init(); |
|
478 |
// note: it is the subclasses' job to call |
|
479 |
// QCoreApplicationPrivate::eventDispatcher->startingUp(); |
|
480 |
} |
|
481 |
||
482 |
/*! |
|
483 |
Flushes the platform specific event queues. |
|
484 |
||
485 |
If you are doing graphical changes inside a loop that does not |
|
486 |
return to the event loop on asynchronous window systems like X11 |
|
487 |
or double buffered window systems like Mac OS X, and you want to |
|
488 |
visualize these changes immediately (e.g. Splash Screens), call |
|
489 |
this function. |
|
490 |
||
491 |
\sa sendPostedEvents() |
|
492 |
*/ |
|
493 |
void QCoreApplication::flush() |
|
494 |
{ |
|
495 |
if (self && self->d_func()->eventDispatcher) |
|
496 |
self->d_func()->eventDispatcher->flush(); |
|
497 |
} |
|
498 |
||
499 |
/*! |
|
500 |
Constructs a Qt kernel application. Kernel applications are |
|
501 |
applications without a graphical user interface. These type of |
|
502 |
applications are used at the console or as server processes. |
|
503 |
||
504 |
The \a argc and \a argv arguments are processed by the application, |
|
505 |
and made available in a more convenient form by the arguments() |
|
506 |
function. |
|
507 |
||
508 |
\warning The data referred to by \a argc and \a argv must stay valid |
|
509 |
for the entire lifetime of the QCoreApplication object. In addition, |
|
510 |
\a argc must be greater than zero and \a argv must contain at least |
|
511 |
one valid character string. |
|
512 |
*/ |
|
513 |
QCoreApplication::QCoreApplication(int &argc, char **argv) |
|
514 |
: QObject(*new QCoreApplicationPrivate(argc, argv)) |
|
515 |
{ |
|
516 |
init(); |
|
517 |
QCoreApplicationPrivate::eventDispatcher->startingUp(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
518 |
#if defined(Q_OS_SYMBIAN) && !defined(QT_NO_LIBRARY) |
0 | 519 |
// Refresh factoryloader, as text codecs are requested during lib path |
520 |
// resolving process and won't be therefore properly loaded. |
|
521 |
// Unknown if this is symbian specific issue. |
|
522 |
QFactoryLoader::refreshAll(); |
|
523 |
#endif |
|
524 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
525 |
#if defined(Q_OS_SYMBIAN) && !defined(QT_NO_SYSTEMLOCALE) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
526 |
d_func()->symbianInit(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
527 |
#endif |
0 | 528 |
} |
529 |
||
530 |
// ### move to QCoreApplicationPrivate constructor? |
|
531 |
void QCoreApplication::init() |
|
532 |
{ |
|
533 |
Q_D(QCoreApplication); |
|
534 |
||
535 |
#ifdef Q_OS_UNIX |
|
536 |
setlocale(LC_ALL, ""); // use correct char set mapping |
|
537 |
qt_locale_initialized = true; |
|
538 |
#endif |
|
539 |
||
540 |
Q_ASSERT_X(!self, "QCoreApplication", "there should be only one application object"); |
|
541 |
QCoreApplication::self = this; |
|
542 |
||
543 |
#ifndef QT_NO_THREAD |
|
544 |
QThread::initialize(); |
|
545 |
#endif |
|
546 |
||
547 |
// use the event dispatcher created by the app programmer (if any) |
|
548 |
if (!QCoreApplicationPrivate::eventDispatcher) |
|
549 |
QCoreApplicationPrivate::eventDispatcher = d->threadData->eventDispatcher; |
|
550 |
// otherwise we create one |
|
551 |
if (!QCoreApplicationPrivate::eventDispatcher) |
|
552 |
d->createEventDispatcher(); |
|
553 |
Q_ASSERT(QCoreApplicationPrivate::eventDispatcher != 0); |
|
554 |
||
555 |
if (!QCoreApplicationPrivate::eventDispatcher->parent()) |
|
556 |
QCoreApplicationPrivate::eventDispatcher->moveToThread(d->threadData->thread); |
|
557 |
||
558 |
d->threadData->eventDispatcher = QCoreApplicationPrivate::eventDispatcher; |
|
559 |
||
560 |
#if !defined(QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS) |
|
561 |
if (!coreappdata()->app_libpaths) { |
|
562 |
// make sure that library paths is initialized |
|
563 |
libraryPaths(); |
|
564 |
} else { |
|
565 |
d->appendApplicationPathToLibraryPaths(); |
|
566 |
} |
|
567 |
#endif |
|
568 |
||
569 |
#if defined(Q_OS_UNIX) && !(defined(QT_NO_PROCESS)) |
|
570 |
// Make sure the process manager thread object is created in the main |
|
571 |
// thread. |
|
572 |
QProcessPrivate::initializeProcessManager(); |
|
573 |
#endif |
|
574 |
||
575 |
#ifdef QT_EVAL |
|
576 |
extern void qt_core_eval_init(uint); |
|
577 |
qt_core_eval_init(d->application_type); |
|
578 |
#endif |
|
579 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
580 |
#if defined(Q_OS_SYMBIAN) \ |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
581 |
&& defined(Q_CC_NOKIAX86) \ |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
582 |
&& defined(QT_DEBUG) |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
583 |
/** |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
584 |
* Prevent the executable from being locked in the Symbian emulator. The |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
585 |
* code dramatically simplifies debugging on Symbian, but beyond that has |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
586 |
* no impact. |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
587 |
* |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
588 |
* Force the ZLazyUnloadTimer to fire and therefore unload code segments |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
589 |
* immediately. The code affects Symbian's file server and on the other |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
590 |
* hand needs only to be run once in each emulator run. |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
591 |
*/ |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
592 |
{ |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
593 |
RLoader loader; |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
594 |
CleanupClosePushL(loader); |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
595 |
User::LeaveIfError(loader.Connect()); |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
596 |
User::LeaveIfError(loader.CancelLazyDllUnload()); |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
597 |
CleanupStack::PopAndDestroy(&loader); |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
598 |
} |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
599 |
#endif |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
600 |
|
0 | 601 |
qt_startup_hook(); |
602 |
} |
|
603 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
604 |
#if defined(Q_OS_SYMBIAN) && !defined(QT_NO_SYSTEMLOCALE) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
605 |
void QCoreApplicationPrivate::symbianInit() |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
606 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
607 |
if (!environmentChangeNotifier) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
608 |
environmentChangeNotifier.reset(new QEnvironmentChangeNotifier); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
609 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
610 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
611 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
612 |
|
0 | 613 |
/*! |
614 |
Destroys the QCoreApplication object. |
|
615 |
*/ |
|
616 |
QCoreApplication::~QCoreApplication() |
|
617 |
{ |
|
618 |
qt_call_post_routines(); |
|
619 |
||
620 |
self = 0; |
|
621 |
QCoreApplicationPrivate::is_app_closing = true; |
|
622 |
QCoreApplicationPrivate::is_app_running = false; |
|
623 |
||
624 |
#if !defined(QT_NO_THREAD) |
|
625 |
#if !defined(QT_NO_CONCURRENT) |
|
626 |
// Synchronize and stop the global thread pool threads. |
|
627 |
QThreadPool *globalThreadPool = 0; |
|
628 |
QT_TRY { |
|
629 |
globalThreadPool = QThreadPool::globalInstance(); |
|
630 |
} QT_CATCH (...) { |
|
631 |
// swallow the exception, since destructors shouldn't throw |
|
632 |
} |
|
633 |
if (globalThreadPool) |
|
634 |
globalThreadPool->waitForDone(); |
|
635 |
#endif |
|
636 |
QThread::cleanup(); |
|
637 |
#endif |
|
638 |
||
639 |
d_func()->threadData->eventDispatcher = 0; |
|
640 |
if (QCoreApplicationPrivate::eventDispatcher) |
|
641 |
QCoreApplicationPrivate::eventDispatcher->closingDown(); |
|
642 |
QCoreApplicationPrivate::eventDispatcher = 0; |
|
643 |
||
644 |
#ifndef QT_NO_LIBRARY |
|
645 |
delete coreappdata()->app_libpaths; |
|
646 |
coreappdata()->app_libpaths = 0; |
|
647 |
#endif |
|
648 |
} |
|
649 |
||
650 |
||
651 |
/*! |
|
652 |
Sets the attribute \a attribute if \a on is true; |
|
653 |
otherwise clears the attribute. |
|
654 |
||
655 |
One of the attributes that can be set with this method is |
|
656 |
Qt::AA_ImmediateWidgetCreation. It tells Qt to create toplevel |
|
657 |
windows immediately. Normally, resources for widgets are allocated |
|
658 |
on demand to improve efficiency and minimize resource usage. |
|
659 |
Therefore, if it is important to minimize resource consumption, do |
|
660 |
not set this attribute. |
|
661 |
||
662 |
\sa testAttribute() |
|
663 |
*/ |
|
664 |
void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute, bool on) |
|
665 |
{ |
|
666 |
if (on) |
|
667 |
QCoreApplicationPrivate::attribs |= 1 << attribute; |
|
668 |
else |
|
669 |
QCoreApplicationPrivate::attribs &= ~(1 << attribute); |
|
670 |
#ifdef Q_OS_MAC |
|
671 |
// Turn on the no native menubar here, since we used to |
|
672 |
// do this implicitly. We DO NOT flip it off if someone sets |
|
673 |
// it to false. |
|
674 |
// Ideally, we'd have magic that would be something along the lines of |
|
675 |
// "follow MacPluginApplication" unless explicitly set. |
|
676 |
// Considering this attribute isn't only at the beginning |
|
677 |
// it's unlikely it will ever be a problem, but I want |
|
678 |
// to have the behavior documented here. |
|
679 |
if (attribute == Qt::AA_MacPluginApplication && on |
|
680 |
&& !testAttribute(Qt::AA_DontUseNativeMenuBar)) { |
|
681 |
setAttribute(Qt::AA_DontUseNativeMenuBar, true); |
|
682 |
} |
|
683 |
#endif |
|
684 |
} |
|
685 |
||
686 |
/*! |
|
687 |
Returns true if attribute \a attribute is set; |
|
688 |
otherwise returns false. |
|
689 |
||
690 |
\sa setAttribute() |
|
691 |
*/ |
|
692 |
bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute) |
|
693 |
{ |
|
694 |
return QCoreApplicationPrivate::testAttribute(attribute); |
|
695 |
} |
|
696 |
||
697 |
||
698 |
/*! |
|
699 |
\internal |
|
700 |
||
701 |
This function is here to make it possible for Qt extensions to |
|
702 |
hook into event notification without subclassing QApplication |
|
703 |
*/ |
|
704 |
bool QCoreApplication::notifyInternal(QObject *receiver, QEvent *event) |
|
705 |
{ |
|
706 |
// Make it possible for Qt Jambi and QSA to hook into events even |
|
707 |
// though QApplication is subclassed... |
|
708 |
bool result = false; |
|
709 |
void *cbdata[] = { receiver, event, &result }; |
|
710 |
if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) { |
|
711 |
return result; |
|
712 |
} |
|
713 |
||
714 |
// Qt enforces the rule that events can only be sent to objects in |
|
715 |
// the current thread, so receiver->d_func()->threadData is |
|
716 |
// equivalent to QThreadData::current(), just without the function |
|
717 |
// call overhead. |
|
718 |
QObjectPrivate *d = receiver->d_func(); |
|
719 |
QThreadData *threadData = d->threadData; |
|
720 |
++threadData->loopLevel; |
|
721 |
||
722 |
#ifdef QT_JAMBI_BUILD |
|
723 |
int deleteWatch = 0; |
|
724 |
int *oldDeleteWatch = QObjectPrivate::setDeleteWatch(d, &deleteWatch); |
|
725 |
||
726 |
bool inEvent = d->inEventHandler; |
|
727 |
d->inEventHandler = true; |
|
728 |
#endif |
|
729 |
||
730 |
bool returnValue; |
|
731 |
QT_TRY { |
|
732 |
returnValue = notify(receiver, event); |
|
733 |
} QT_CATCH (...) { |
|
734 |
--threadData->loopLevel; |
|
735 |
QT_RETHROW; |
|
736 |
} |
|
737 |
||
738 |
#ifdef QT_JAMBI_BUILD |
|
739 |
// Restore the previous state if the object was not deleted.. |
|
740 |
if (!deleteWatch) { |
|
741 |
d->inEventHandler = inEvent; |
|
742 |
} |
|
743 |
QObjectPrivate::resetDeleteWatch(d, oldDeleteWatch, deleteWatch); |
|
744 |
#endif |
|
745 |
--threadData->loopLevel; |
|
746 |
return returnValue; |
|
747 |
} |
|
748 |
||
749 |
||
750 |
/*! |
|
751 |
Sends \a event to \a receiver: \a {receiver}->event(\a event). |
|
752 |
Returns the value that is returned from the receiver's event |
|
753 |
handler. Note that this function is called for all events sent to |
|
754 |
any object in any thread. |
|
755 |
||
756 |
For certain types of events (e.g. mouse and key events), |
|
757 |
the event will be propagated to the receiver's parent and so on up to |
|
758 |
the top-level object if the receiver is not interested in the event |
|
759 |
(i.e., it returns false). |
|
760 |
||
761 |
There are five different ways that events can be processed; |
|
762 |
reimplementing this virtual function is just one of them. All five |
|
763 |
approaches are listed below: |
|
764 |
\list 1 |
|
765 |
\i Reimplementing paintEvent(), mousePressEvent() and so |
|
766 |
on. This is the commonest, easiest and least powerful way. |
|
767 |
||
768 |
\i Reimplementing this function. This is very powerful, providing |
|
769 |
complete control; but only one subclass can be active at a time. |
|
770 |
||
771 |
\i Installing an event filter on QCoreApplication::instance(). Such |
|
772 |
an event filter is able to process all events for all widgets, so |
|
773 |
it's just as powerful as reimplementing notify(); furthermore, it's |
|
774 |
possible to have more than one application-global event filter. |
|
775 |
Global event filters even see mouse events for |
|
776 |
\l{QWidget::isEnabled()}{disabled widgets}. Note that application |
|
777 |
event filters are only called for objects that live in the main |
|
778 |
thread. |
|
779 |
||
780 |
\i Reimplementing QObject::event() (as QWidget does). If you do |
|
781 |
this you get Tab key presses, and you get to see the events before |
|
782 |
any widget-specific event filters. |
|
783 |
||
784 |
\i Installing an event filter on the object. Such an event filter gets all |
|
785 |
the events, including Tab and Shift+Tab key press events, as long as they |
|
786 |
do not change the focus widget. |
|
787 |
\endlist |
|
788 |
||
789 |
\sa QObject::event(), installEventFilter() |
|
790 |
*/ |
|
791 |
||
792 |
bool QCoreApplication::notify(QObject *receiver, QEvent *event) |
|
793 |
{ |
|
794 |
Q_D(QCoreApplication); |
|
795 |
// no events are delivered after ~QCoreApplication() has started |
|
796 |
if (QCoreApplicationPrivate::is_app_closing) |
|
797 |
return true; |
|
798 |
||
799 |
if (receiver == 0) { // serious error |
|
800 |
qWarning("QCoreApplication::notify: Unexpected null receiver"); |
|
801 |
return true; |
|
802 |
} |
|
803 |
||
804 |
#ifndef QT_NO_DEBUG |
|
805 |
d->checkReceiverThread(receiver); |
|
806 |
#endif |
|
807 |
||
808 |
#ifdef QT3_SUPPORT |
|
809 |
if (event->type() == QEvent::ChildRemoved && !receiver->d_func()->pendingChildInsertedEvents.isEmpty()) |
|
810 |
receiver->d_func()->removePendingChildInsertedEvents(static_cast<QChildEvent *>(event)->child()); |
|
811 |
#endif // QT3_SUPPORT |
|
812 |
||
813 |
return receiver->isWidgetType() ? false : d->notify_helper(receiver, event); |
|
814 |
} |
|
815 |
||
816 |
bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event) |
|
817 |
{ |
|
818 |
if (receiver->d_func()->threadData == this->threadData) { |
|
819 |
// application event filters are only called for objects in the GUI thread |
|
820 |
for (int i = 0; i < eventFilters.size(); ++i) { |
|
821 |
register QObject *obj = eventFilters.at(i); |
|
822 |
if (!obj) |
|
823 |
continue; |
|
824 |
if (obj->d_func()->threadData != threadData) { |
|
825 |
qWarning("QCoreApplication: Application event filter cannot be in a different thread."); |
|
826 |
continue; |
|
827 |
} |
|
828 |
if (obj->eventFilter(receiver, event)) |
|
829 |
return true; |
|
830 |
} |
|
831 |
} |
|
832 |
return false; |
|
833 |
} |
|
834 |
||
835 |
bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event) |
|
836 |
{ |
|
837 |
Q_Q(QCoreApplication); |
|
838 |
if (receiver != q) { |
|
839 |
for (int i = 0; i < receiver->d_func()->eventFilters.size(); ++i) { |
|
840 |
register QObject *obj = receiver->d_func()->eventFilters.at(i); |
|
841 |
if (!obj) |
|
842 |
continue; |
|
843 |
if (obj->d_func()->threadData != receiver->d_func()->threadData) { |
|
844 |
qWarning("QCoreApplication: Object event filter cannot be in a different thread."); |
|
845 |
continue; |
|
846 |
} |
|
847 |
if (obj->eventFilter(receiver, event)) |
|
848 |
return true; |
|
849 |
} |
|
850 |
} |
|
851 |
return false; |
|
852 |
} |
|
853 |
||
854 |
/*!\internal |
|
855 |
||
856 |
Helper function called by notify() |
|
857 |
*/ |
|
858 |
bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event) |
|
859 |
{ |
|
860 |
// send to all application event filters |
|
861 |
if (sendThroughApplicationEventFilters(receiver, event)) |
|
862 |
return true; |
|
863 |
// send to all receiver event filters |
|
864 |
if (sendThroughObjectEventFilters(receiver, event)) |
|
865 |
return true; |
|
866 |
// deliver the event |
|
867 |
return receiver->event(event); |
|
868 |
} |
|
869 |
||
870 |
/*! |
|
871 |
Returns true if an application object has not been created yet; |
|
872 |
otherwise returns false. |
|
873 |
||
874 |
\sa closingDown() |
|
875 |
*/ |
|
876 |
||
877 |
bool QCoreApplication::startingUp() |
|
878 |
{ |
|
879 |
return !QCoreApplicationPrivate::is_app_running; |
|
880 |
} |
|
881 |
||
882 |
/*! |
|
883 |
Returns true if the application objects are being destroyed; |
|
884 |
otherwise returns false. |
|
885 |
||
886 |
\sa startingUp() |
|
887 |
*/ |
|
888 |
||
889 |
bool QCoreApplication::closingDown() |
|
890 |
{ |
|
891 |
return QCoreApplicationPrivate::is_app_closing; |
|
892 |
} |
|
893 |
||
894 |
||
895 |
/*! |
|
896 |
Processes all pending events for the calling thread according to |
|
897 |
the specified \a flags until there are no more events to process. |
|
898 |
||
899 |
You can call this function occasionally when your program is busy |
|
900 |
performing a long operation (e.g. copying a file). |
|
901 |
||
902 |
In event you are running a local loop which calls this function |
|
903 |
continuously, without an event loop, the |
|
904 |
\l{QEvent::DeferredDelete}{DeferredDelete} events will |
|
905 |
not be processed. This can affect the behaviour of widgets, |
|
906 |
e.g. QToolTip, that rely on \l{QEvent::DeferredDelete}{DeferredDelete} |
|
907 |
events to function properly. An alternative would be to call |
|
908 |
\l{QCoreApplication::sendPostedEvents()}{sendPostedEvents()} from |
|
909 |
within that local loop. |
|
910 |
||
911 |
Calling this function processes events only for the calling thread. |
|
912 |
||
913 |
\threadsafe |
|
914 |
||
915 |
\sa exec(), QTimer, QEventLoop::processEvents(), flush(), sendPostedEvents() |
|
916 |
*/ |
|
917 |
void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags) |
|
918 |
{ |
|
919 |
QThreadData *data = QThreadData::current(); |
|
920 |
if (!data->eventDispatcher) |
|
921 |
return; |
|
922 |
if (flags & QEventLoop::DeferredDeletion) |
|
923 |
QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
|
924 |
data->eventDispatcher->processEvents(flags); |
|
925 |
} |
|
926 |
||
927 |
/*! |
|
928 |
\overload processEvents() |
|
929 |
||
930 |
Processes pending events for the calling thread for \a maxtime |
|
931 |
milliseconds or until there are no more events to process, |
|
932 |
whichever is shorter. |
|
933 |
||
934 |
You can call this function occasionally when you program is busy |
|
935 |
doing a long operation (e.g. copying a file). |
|
936 |
||
937 |
Calling this function processes events only for the calling thread. |
|
938 |
||
939 |
\threadsafe |
|
940 |
||
941 |
\sa exec(), QTimer, QEventLoop::processEvents() |
|
942 |
*/ |
|
943 |
void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, int maxtime) |
|
944 |
{ |
|
945 |
QThreadData *data = QThreadData::current(); |
|
946 |
if (!data->eventDispatcher) |
|
947 |
return; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
948 |
QElapsedTimer start; |
0 | 949 |
start.start(); |
950 |
if (flags & QEventLoop::DeferredDeletion) |
|
951 |
QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
|
952 |
while (data->eventDispatcher->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) { |
|
953 |
if (start.elapsed() > maxtime) |
|
954 |
break; |
|
955 |
if (flags & QEventLoop::DeferredDeletion) |
|
956 |
QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
|
957 |
} |
|
958 |
} |
|
959 |
||
960 |
/***************************************************************************** |
|
961 |
Main event loop wrappers |
|
962 |
*****************************************************************************/ |
|
963 |
||
964 |
/*! |
|
965 |
Enters the main event loop and waits until exit() is called. |
|
966 |
Returns the value that was set to exit() (which is 0 if exit() is |
|
967 |
called via quit()). |
|
968 |
||
969 |
It is necessary to call this function to start event handling. The |
|
970 |
main event loop receives events from the window system and |
|
971 |
dispatches these to the application widgets. |
|
972 |
||
973 |
To make your application perform idle processing (i.e. executing a |
|
974 |
special function whenever there are no pending events), use a |
|
975 |
QTimer with 0 timeout. More advanced idle processing schemes can |
|
976 |
be achieved using processEvents(). |
|
977 |
||
978 |
We recommend that you connect clean-up code to the |
|
979 |
\l{QCoreApplication::}{aboutToQuit()} signal, instead of putting it in |
|
980 |
your application's \c{main()} function because on some platforms the |
|
981 |
QCoreApplication::exec() call may not return. For example, on Windows |
|
982 |
when the user logs off, the system terminates the process after Qt |
|
983 |
closes all top-level windows. Hence, there is no guarantee that the |
|
984 |
application will have time to exit its event loop and execute code at |
|
985 |
the end of the \c{main()} function after the QCoreApplication::exec() |
|
986 |
call. |
|
987 |
||
988 |
\sa quit(), exit(), processEvents(), QApplication::exec() |
|
989 |
*/ |
|
990 |
int QCoreApplication::exec() |
|
991 |
{ |
|
992 |
if (!QCoreApplicationPrivate::checkInstance("exec")) |
|
993 |
return -1; |
|
994 |
||
995 |
QThreadData *threadData = self->d_func()->threadData; |
|
996 |
if (threadData != QThreadData::current()) { |
|
997 |
qWarning("%s::exec: Must be called from the main thread", self->metaObject()->className()); |
|
998 |
return -1; |
|
999 |
} |
|
1000 |
if (!threadData->eventLoops.isEmpty()) { |
|
1001 |
qWarning("QCoreApplication::exec: The event loop is already running"); |
|
1002 |
return -1; |
|
1003 |
} |
|
1004 |
||
1005 |
threadData->quitNow = false; |
|
1006 |
QEventLoop eventLoop; |
|
1007 |
self->d_func()->in_exec = true; |
|
1008 |
self->d_func()->aboutToQuitEmitted = false; |
|
1009 |
int returnCode = eventLoop.exec(); |
|
1010 |
threadData->quitNow = false; |
|
1011 |
if (self) { |
|
1012 |
self->d_func()->in_exec = false; |
|
1013 |
if (!self->d_func()->aboutToQuitEmitted) |
|
1014 |
emit self->aboutToQuit(); |
|
1015 |
self->d_func()->aboutToQuitEmitted = true; |
|
1016 |
sendPostedEvents(0, QEvent::DeferredDelete); |
|
1017 |
} |
|
1018 |
||
1019 |
return returnCode; |
|
1020 |
} |
|
1021 |
||
1022 |
/*! |
|
1023 |
Tells the application to exit with a return code. |
|
1024 |
||
1025 |
After this function has been called, the application leaves the |
|
1026 |
main event loop and returns from the call to exec(). The exec() |
|
1027 |
function returns \a returnCode. If the event loop is not running, |
|
1028 |
this function does nothing. |
|
1029 |
||
1030 |
By convention, a \a returnCode of 0 means success, and any non-zero |
|
1031 |
value indicates an error. |
|
1032 |
||
1033 |
Note that unlike the C library function of the same name, this |
|
1034 |
function \e does return to the caller -- it is event processing that |
|
1035 |
stops. |
|
1036 |
||
1037 |
\sa quit(), exec() |
|
1038 |
*/ |
|
1039 |
void QCoreApplication::exit(int returnCode) |
|
1040 |
{ |
|
1041 |
if (!self) |
|
1042 |
return; |
|
1043 |
QThreadData *data = self->d_func()->threadData; |
|
1044 |
data->quitNow = true; |
|
1045 |
for (int i = 0; i < data->eventLoops.size(); ++i) { |
|
1046 |
QEventLoop *eventLoop = data->eventLoops.at(i); |
|
1047 |
eventLoop->exit(returnCode); |
|
1048 |
} |
|
1049 |
} |
|
1050 |
||
1051 |
/***************************************************************************** |
|
1052 |
QCoreApplication management of posted events |
|
1053 |
*****************************************************************************/ |
|
1054 |
||
1055 |
/*! |
|
1056 |
\fn bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event) |
|
1057 |
||
1058 |
Sends event \a event directly to receiver \a receiver, using the |
|
1059 |
notify() function. Returns the value that was returned from the |
|
1060 |
event handler. |
|
1061 |
||
1062 |
The event is \e not deleted when the event has been sent. The normal |
|
1063 |
approach is to create the event on the stack, for example: |
|
1064 |
||
1065 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 0 |
|
1066 |
||
1067 |
\sa postEvent(), notify() |
|
1068 |
*/ |
|
1069 |
||
1070 |
/*! |
|
1071 |
Adds the event \a event, with the object \a receiver as the |
|
1072 |
receiver of the event, to an event queue and returns immediately. |
|
1073 |
||
1074 |
The event must be allocated on the heap since the post event queue |
|
1075 |
will take ownership of the event and delete it once it has been |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
1076 |
posted. It is \e {not safe} to access the event after |
0 | 1077 |
it has been posted. |
1078 |
||
1079 |
When control returns to the main event loop, all events that are |
|
1080 |
stored in the queue will be sent using the notify() function. |
|
1081 |
||
1082 |
Events are processed in the order posted. For more control over |
|
1083 |
the processing order, use the postEvent() overload below, which |
|
1084 |
takes a priority argument. This function posts all event with a |
|
1085 |
Qt::NormalEventPriority. |
|
1086 |
||
1087 |
\threadsafe |
|
1088 |
||
1089 |
\sa sendEvent(), notify(), sendPostedEvents() |
|
1090 |
*/ |
|
1091 |
||
1092 |
void QCoreApplication::postEvent(QObject *receiver, QEvent *event) |
|
1093 |
{ |
|
1094 |
postEvent(receiver, event, Qt::NormalEventPriority); |
|
1095 |
} |
|
1096 |
||
1097 |
||
1098 |
/*! |
|
1099 |
\overload postEvent() |
|
1100 |
\since 4.3 |
|
1101 |
||
1102 |
Adds the event \a event, with the object \a receiver as the |
|
1103 |
receiver of the event, to an event queue and returns immediately. |
|
1104 |
||
1105 |
The event must be allocated on the heap since the post event queue |
|
1106 |
will take ownership of the event and delete it once it has been |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
1107 |
posted. It is \e {not safe} to access the event after |
0 | 1108 |
it has been posted. |
1109 |
||
1110 |
When control returns to the main event loop, all events that are |
|
1111 |
stored in the queue will be sent using the notify() function. |
|
1112 |
||
1113 |
Events are sorted in descending \a priority order, i.e. events |
|
1114 |
with a high \a priority are queued before events with a lower \a |
|
1115 |
priority. The \a priority can be any integer value, i.e. between |
|
1116 |
INT_MAX and INT_MIN, inclusive; see Qt::EventPriority for more |
|
1117 |
details. Events with equal \a priority will be processed in the |
|
1118 |
order posted. |
|
1119 |
||
1120 |
\threadsafe |
|
1121 |
||
1122 |
\sa sendEvent(), notify(), sendPostedEvents(), Qt::EventPriority |
|
1123 |
*/ |
|
1124 |
void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority) |
|
1125 |
{ |
|
1126 |
if (receiver == 0) { |
|
1127 |
qWarning("QCoreApplication::postEvent: Unexpected null receiver"); |
|
1128 |
delete event; |
|
1129 |
return; |
|
1130 |
} |
|
1131 |
||
1132 |
QThreadData * volatile * pdata = &receiver->d_func()->threadData; |
|
1133 |
QThreadData *data = *pdata; |
|
1134 |
if (!data) { |
|
1135 |
// posting during destruction? just delete the event to prevent a leak |
|
1136 |
delete event; |
|
1137 |
return; |
|
1138 |
} |
|
1139 |
||
1140 |
// lock the post event mutex |
|
1141 |
data->postEventList.mutex.lock(); |
|
1142 |
||
1143 |
// if object has moved to another thread, follow it |
|
1144 |
while (data != *pdata) { |
|
1145 |
data->postEventList.mutex.unlock(); |
|
1146 |
||
1147 |
data = *pdata; |
|
1148 |
if (!data) { |
|
1149 |
// posting during destruction? just delete the event to prevent a leak |
|
1150 |
delete event; |
|
1151 |
return; |
|
1152 |
} |
|
1153 |
||
1154 |
data->postEventList.mutex.lock(); |
|
1155 |
} |
|
1156 |
||
1157 |
QMutexUnlocker locker(&data->postEventList.mutex); |
|
1158 |
||
1159 |
// if this is one of the compressible events, do compression |
|
1160 |
if (receiver->d_func()->postedEvents |
|
1161 |
&& self && self->compressEvent(event, receiver, &data->postEventList)) { |
|
1162 |
return; |
|
1163 |
} |
|
1164 |
||
1165 |
if (event->type() == QEvent::DeferredDelete && data == QThreadData::current()) { |
|
1166 |
// remember the current running eventloop for DeferredDelete |
|
1167 |
// events posted in the receiver's thread |
|
1168 |
event->d = reinterpret_cast<QEventPrivate *>(quintptr(data->loopLevel)); |
|
1169 |
} |
|
1170 |
||
1171 |
// delete the event on exceptions to protect against memory leaks till the event is |
|
1172 |
// properly owned in the postEventList |
|
1173 |
QScopedPointer<QEvent> eventDeleter(event); |
|
1174 |
if (data->postEventList.isEmpty() || data->postEventList.last().priority >= priority) { |
|
1175 |
// optimization: we can simply append if the last event in |
|
1176 |
// the queue has higher or equal priority |
|
1177 |
data->postEventList.append(QPostEvent(receiver, event, priority)); |
|
1178 |
} else { |
|
1179 |
// insert event in descending priority order, using upper |
|
1180 |
// bound for a given priority (to ensure proper ordering |
|
1181 |
// of events with the same priority) |
|
1182 |
QPostEventList::iterator begin = data->postEventList.begin() |
|
1183 |
+ data->postEventList.insertionOffset, |
|
1184 |
end = data->postEventList.end(); |
|
1185 |
QPostEventList::iterator at = qUpperBound(begin, end, priority); |
|
1186 |
data->postEventList.insert(at, QPostEvent(receiver, event, priority)); |
|
1187 |
} |
|
1188 |
eventDeleter.take(); |
|
1189 |
event->posted = true; |
|
1190 |
++receiver->d_func()->postedEvents; |
|
1191 |
data->canWait = false; |
|
1192 |
locker.unlock(); |
|
1193 |
||
1194 |
if (data->eventDispatcher) |
|
1195 |
data->eventDispatcher->wakeUp(); |
|
1196 |
} |
|
1197 |
||
1198 |
/*! |
|
1199 |
\internal |
|
1200 |
Returns true if \a event was compressed away (possibly deleted) and should not be added to the list. |
|
1201 |
*/ |
|
1202 |
bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents) |
|
1203 |
{ |
|
1204 |
#ifdef Q_WS_WIN |
|
1205 |
Q_ASSERT(event); |
|
1206 |
Q_ASSERT(receiver); |
|
1207 |
Q_ASSERT(postedEvents); |
|
1208 |
||
1209 |
// compress posted timers to this object. |
|
1210 |
if (event->type() == QEvent::Timer && receiver->d_func()->postedEvents > 0) { |
|
1211 |
int timerId = ((QTimerEvent *) event)->timerId(); |
|
1212 |
for (int i=0; i<postedEvents->size(); ++i) { |
|
1213 |
const QPostEvent &e = postedEvents->at(i); |
|
1214 |
if (e.receiver == receiver && e.event && e.event->type() == QEvent::Timer |
|
1215 |
&& ((QTimerEvent *) e.event)->timerId() == timerId) { |
|
1216 |
delete event; |
|
1217 |
return true; |
|
1218 |
} |
|
1219 |
} |
|
1220 |
} else |
|
1221 |
#endif |
|
1222 |
if ((event->type() == QEvent::DeferredDelete |
|
1223 |
|| event->type() == QEvent::Quit) |
|
1224 |
&& receiver->d_func()->postedEvents > 0) { |
|
1225 |
for (int i = 0; i < postedEvents->size(); ++i) { |
|
1226 |
const QPostEvent &cur = postedEvents->at(i); |
|
1227 |
if (cur.receiver != receiver |
|
1228 |
|| cur.event == 0 |
|
1229 |
|| cur.event->type() != event->type()) |
|
1230 |
continue; |
|
1231 |
// found an event for this receiver |
|
1232 |
delete event; |
|
1233 |
return true; |
|
1234 |
} |
|
1235 |
} |
|
1236 |
return false; |
|
1237 |
} |
|
1238 |
||
1239 |
/*! |
|
1240 |
\fn void QCoreApplication::sendPostedEvents() |
|
1241 |
\overload sendPostedEvents() |
|
1242 |
||
1243 |
Dispatches all posted events, i.e. empties the event queue. |
|
1244 |
*/ |
|
1245 |
||
1246 |
/*! |
|
1247 |
Immediately dispatches all events which have been previously queued |
|
1248 |
with QCoreApplication::postEvent() and which are for the object \a receiver |
|
1249 |
and have the event type \a event_type. |
|
1250 |
||
1251 |
Events from the window system are \e not dispatched by this |
|
1252 |
function, but by processEvents(). |
|
1253 |
||
1254 |
If \a receiver is null, the events of \a event_type are sent for all |
|
1255 |
objects. If \a event_type is 0, all the events are sent for \a receiver. |
|
1256 |
||
1257 |
\note This method must be called from the same thread as its QObject parameter, \a receiver. |
|
1258 |
||
1259 |
\sa flush(), postEvent() |
|
1260 |
*/ |
|
1261 |
||
1262 |
void QCoreApplication::sendPostedEvents(QObject *receiver, int event_type) |
|
1263 |
{ |
|
1264 |
QThreadData *data = QThreadData::current(); |
|
1265 |
||
1266 |
QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data); |
|
1267 |
} |
|
1268 |
||
1269 |
void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type, |
|
1270 |
QThreadData *data) |
|
1271 |
{ |
|
1272 |
if (event_type == -1) { |
|
1273 |
// we were called by an obsolete event dispatcher. |
|
1274 |
event_type = 0; |
|
1275 |
} |
|
1276 |
||
1277 |
if (receiver && receiver->d_func()->threadData != data) { |
|
1278 |
qWarning("QCoreApplication::sendPostedEvents: Cannot send " |
|
1279 |
"posted events for objects in another thread"); |
|
1280 |
return; |
|
1281 |
} |
|
1282 |
||
1283 |
++data->postEventList.recursion; |
|
1284 |
||
1285 |
#ifdef QT3_SUPPORT |
|
1286 |
if (event_type == QEvent::ChildInserted) { |
|
1287 |
if (receiver) { |
|
1288 |
// optimize sendPostedEvents(w, QEvent::ChildInserted) calls away |
|
1289 |
receiver->d_func()->sendPendingChildInsertedEvents(); |
|
1290 |
--data->postEventList.recursion; |
|
1291 |
return; |
|
1292 |
} |
|
1293 |
||
1294 |
// ChildInserted events are sent in response to *Request |
|
1295 |
event_type = QEvent::ChildInsertedRequest; |
|
1296 |
} |
|
1297 |
#endif |
|
1298 |
||
1299 |
QMutexLocker locker(&data->postEventList.mutex); |
|
1300 |
||
1301 |
// by default, we assume that the event dispatcher can go to sleep after |
|
1302 |
// processing all events. if any new events are posted while we send |
|
1303 |
// events, canWait will be set to false. |
|
1304 |
data->canWait = (data->postEventList.size() == 0); |
|
1305 |
||
1306 |
if (data->postEventList.size() == 0 || (receiver && !receiver->d_func()->postedEvents)) { |
|
1307 |
--data->postEventList.recursion; |
|
1308 |
return; |
|
1309 |
} |
|
1310 |
||
1311 |
data->canWait = true; |
|
1312 |
||
1313 |
// okay. here is the tricky loop. be careful about optimizing |
|
1314 |
// this, it looks the way it does for good reasons. |
|
1315 |
int startOffset = data->postEventList.startOffset; |
|
1316 |
int &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset; |
|
1317 |
data->postEventList.insertionOffset = data->postEventList.size(); |
|
1318 |
||
1319 |
while (i < data->postEventList.size()) { |
|
1320 |
// avoid live-lock |
|
1321 |
if (i >= data->postEventList.insertionOffset) |
|
1322 |
break; |
|
1323 |
||
1324 |
const QPostEvent &pe = data->postEventList.at(i); |
|
1325 |
++i; |
|
1326 |
||
1327 |
if (!pe.event) |
|
1328 |
continue; |
|
1329 |
if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) { |
|
1330 |
data->canWait = false; |
|
1331 |
continue; |
|
1332 |
} |
|
1333 |
||
1334 |
if (pe.event->type() == QEvent::DeferredDelete) { |
|
1335 |
// DeferredDelete events are only sent when we are explicitly asked to |
|
1336 |
// (s.a. QEvent::DeferredDelete), and then only if the event loop that |
|
1337 |
// posted the event has returned. |
|
1338 |
const bool allowDeferredDelete = |
|
1339 |
(quintptr(pe.event->d) > unsigned(data->loopLevel) |
|
1340 |
|| (!quintptr(pe.event->d) && data->loopLevel > 0) |
|
1341 |
|| (event_type == QEvent::DeferredDelete |
|
1342 |
&& quintptr(pe.event->d) == unsigned(data->loopLevel))); |
|
1343 |
if (!allowDeferredDelete) { |
|
1344 |
// cannot send deferred delete |
|
1345 |
if (!event_type && !receiver) { |
|
1346 |
// don't lose the event |
|
1347 |
data->postEventList.append(pe); |
|
1348 |
const_cast<QPostEvent &>(pe).event = 0; |
|
1349 |
} |
|
1350 |
continue; |
|
1351 |
} |
|
1352 |
} |
|
1353 |
||
1354 |
// first, we diddle the event so that we can deliver |
|
1355 |
// it, and that no one will try to touch it later. |
|
1356 |
pe.event->posted = false; |
|
1357 |
QEvent * e = pe.event; |
|
1358 |
QObject * r = pe.receiver; |
|
1359 |
||
1360 |
--r->d_func()->postedEvents; |
|
1361 |
Q_ASSERT(r->d_func()->postedEvents >= 0); |
|
1362 |
||
1363 |
// next, update the data structure so that we're ready |
|
1364 |
// for the next event. |
|
1365 |
const_cast<QPostEvent &>(pe).event = 0; |
|
1366 |
||
1367 |
locker.unlock(); |
|
1368 |
// after all that work, it's time to deliver the event. |
|
1369 |
#ifdef QT_NO_EXCEPTIONS |
|
1370 |
QCoreApplication::sendEvent(r, e); |
|
1371 |
#else |
|
1372 |
try { |
|
1373 |
QCoreApplication::sendEvent(r, e); |
|
1374 |
} catch (...) { |
|
1375 |
delete e; |
|
1376 |
locker.relock(); |
|
1377 |
||
1378 |
// since we were interrupted, we need another pass to make sure we clean everything up |
|
1379 |
data->canWait = false; |
|
1380 |
||
1381 |
// uglehack: copied from below |
|
1382 |
--data->postEventList.recursion; |
|
1383 |
if (!data->postEventList.recursion && !data->canWait && data->eventDispatcher) |
|
1384 |
data->eventDispatcher->wakeUp(); |
|
1385 |
throw; // rethrow |
|
1386 |
} |
|
1387 |
#endif |
|
1388 |
||
1389 |
delete e; |
|
1390 |
locker.relock(); |
|
1391 |
||
1392 |
// careful when adding anything below this point - the |
|
1393 |
// sendEvent() call might invalidate any invariants this |
|
1394 |
// function depends on. |
|
1395 |
} |
|
1396 |
||
1397 |
--data->postEventList.recursion; |
|
1398 |
if (!data->postEventList.recursion && !data->canWait && data->eventDispatcher) |
|
1399 |
data->eventDispatcher->wakeUp(); |
|
1400 |
||
1401 |
// clear the global list, i.e. remove everything that was |
|
1402 |
// delivered. |
|
1403 |
if (!event_type && !receiver && data->postEventList.startOffset >= 0) { |
|
1404 |
const QPostEventList::iterator it = data->postEventList.begin(); |
|
1405 |
data->postEventList.erase(it, it + data->postEventList.startOffset); |
|
1406 |
data->postEventList.insertionOffset -= data->postEventList.startOffset; |
|
1407 |
Q_ASSERT(data->postEventList.insertionOffset >= 0); |
|
1408 |
data->postEventList.startOffset = 0; |
|
1409 |
} |
|
1410 |
} |
|
1411 |
||
1412 |
/*! |
|
1413 |
Removes all events posted using postEvent() for \a receiver. |
|
1414 |
||
1415 |
The events are \e not dispatched, instead they are removed from the |
|
1416 |
queue. You should never need to call this function. If you do call it, |
|
1417 |
be aware that killing events may cause \a receiver to break one or |
|
1418 |
more invariants. |
|
1419 |
||
1420 |
\threadsafe |
|
1421 |
*/ |
|
1422 |
||
1423 |
void QCoreApplication::removePostedEvents(QObject *receiver) |
|
1424 |
{ |
|
1425 |
removePostedEvents(receiver, 0); |
|
1426 |
} |
|
1427 |
||
1428 |
/*! |
|
1429 |
\overload removePostedEvents() |
|
1430 |
\since 4.3 |
|
1431 |
||
1432 |
Removes all events of the given \a eventType that were posted |
|
1433 |
using postEvent() for \a receiver. |
|
1434 |
||
1435 |
The events are \e not dispatched, instead they are removed from |
|
1436 |
the queue. You should never need to call this function. If you do |
|
1437 |
call it, be aware that killing events may cause \a receiver to |
|
1438 |
break one or more invariants. |
|
1439 |
||
1440 |
If \a receiver is null, the events of \a eventType are removed for |
|
1441 |
all objects. If \a eventType is 0, all the events are removed for |
|
1442 |
\a receiver. |
|
1443 |
||
1444 |
\threadsafe |
|
1445 |
*/ |
|
1446 |
||
1447 |
void QCoreApplication::removePostedEvents(QObject *receiver, int eventType) |
|
1448 |
{ |
|
1449 |
#ifdef QT3_SUPPORT |
|
1450 |
if (eventType == QEvent::ChildInserted) |
|
1451 |
eventType = QEvent::ChildInsertedRequest; |
|
1452 |
#endif |
|
1453 |
||
1454 |
QThreadData *data = receiver ? receiver->d_func()->threadData : QThreadData::current(); |
|
1455 |
QMutexLocker locker(&data->postEventList.mutex); |
|
1456 |
||
1457 |
// the QObject destructor calls this function directly. this can |
|
1458 |
// happen while the event loop is in the middle of posting events, |
|
1459 |
// and when we get here, we may not have any more posted events |
|
1460 |
// for this object. |
|
1461 |
if (receiver && !receiver->d_func()->postedEvents) |
|
1462 |
return; |
|
1463 |
||
1464 |
//we will collect all the posted events for the QObject |
|
1465 |
//and we'll delete after the mutex was unlocked |
|
1466 |
QVarLengthArray<QEvent*> events; |
|
1467 |
int n = data->postEventList.size(); |
|
1468 |
int j = 0; |
|
1469 |
||
1470 |
for (int i = 0; i < n; ++i) { |
|
1471 |
const QPostEvent &pe = data->postEventList.at(i); |
|
1472 |
||
1473 |
if ((!receiver || pe.receiver == receiver) |
|
1474 |
&& (pe.event && (eventType == 0 || pe.event->type() == eventType))) { |
|
1475 |
--pe.receiver->d_func()->postedEvents; |
|
1476 |
#ifdef QT3_SUPPORT |
|
1477 |
if (pe.event->type() == QEvent::ChildInsertedRequest) |
|
1478 |
pe.receiver->d_func()->removePendingChildInsertedEvents(0); |
|
1479 |
#endif |
|
1480 |
pe.event->posted = false; |
|
1481 |
events.append(pe.event); |
|
1482 |
const_cast<QPostEvent &>(pe).event = 0; |
|
1483 |
} else if (!data->postEventList.recursion) { |
|
1484 |
if (i != j) |
|
1485 |
data->postEventList.swap(i, j); |
|
1486 |
++j; |
|
1487 |
} |
|
1488 |
} |
|
1489 |
||
1490 |
#ifdef QT_DEBUG |
|
1491 |
if (receiver && eventType == 0) { |
|
1492 |
Q_ASSERT(!receiver->d_func()->postedEvents); |
|
1493 |
} |
|
1494 |
#endif |
|
1495 |
||
1496 |
if (!data->postEventList.recursion) { |
|
1497 |
// truncate list |
|
1498 |
data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end()); |
|
1499 |
} |
|
1500 |
||
1501 |
locker.unlock(); |
|
1502 |
for (int i = 0; i < events.count(); ++i) { |
|
1503 |
delete events[i]; |
|
1504 |
} |
|
1505 |
} |
|
1506 |
||
1507 |
/*! |
|
1508 |
Removes \a event from the queue of posted events, and emits a |
|
1509 |
warning message if appropriate. |
|
1510 |
||
1511 |
\warning This function can be \e really slow. Avoid using it, if |
|
1512 |
possible. |
|
1513 |
||
1514 |
\threadsafe |
|
1515 |
*/ |
|
1516 |
||
1517 |
void QCoreApplicationPrivate::removePostedEvent(QEvent * event) |
|
1518 |
{ |
|
1519 |
if (!event || !event->posted) |
|
1520 |
return; |
|
1521 |
||
1522 |
QThreadData *data = QThreadData::current(); |
|
1523 |
||
1524 |
QMutexLocker locker(&data->postEventList.mutex); |
|
1525 |
||
1526 |
if (data->postEventList.size() == 0) { |
|
1527 |
#if defined(QT_DEBUG) |
|
1528 |
qDebug("QCoreApplication::removePostedEvent: Internal error: %p %d is posted", |
|
1529 |
(void*)event, event->type()); |
|
1530 |
return; |
|
1531 |
#endif |
|
1532 |
} |
|
1533 |
||
1534 |
for (int i = 0; i < data->postEventList.size(); ++i) { |
|
1535 |
const QPostEvent & pe = data->postEventList.at(i); |
|
1536 |
if (pe.event == event) { |
|
1537 |
#ifndef QT_NO_DEBUG |
|
1538 |
qWarning("QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s", |
|
1539 |
event->type(), |
|
1540 |
pe.receiver->metaObject()->className(), |
|
1541 |
pe.receiver->objectName().toLocal8Bit().data()); |
|
1542 |
#endif |
|
1543 |
--pe.receiver->d_func()->postedEvents; |
|
1544 |
pe.event->posted = false; |
|
1545 |
delete pe.event; |
|
1546 |
const_cast<QPostEvent &>(pe).event = 0; |
|
1547 |
return; |
|
1548 |
} |
|
1549 |
} |
|
1550 |
} |
|
1551 |
||
1552 |
/*!\reimp |
|
1553 |
||
1554 |
*/ |
|
1555 |
bool QCoreApplication::event(QEvent *e) |
|
1556 |
{ |
|
1557 |
if (e->type() == QEvent::Quit) { |
|
1558 |
quit(); |
|
1559 |
return true; |
|
1560 |
} |
|
1561 |
return QObject::event(e); |
|
1562 |
} |
|
1563 |
||
1564 |
/*! \enum QCoreApplication::Encoding |
|
1565 |
||
1566 |
This enum type defines the 8-bit encoding of character string |
|
1567 |
arguments to translate(): |
|
1568 |
||
1569 |
\value CodecForTr The encoding specified by |
|
1570 |
QTextCodec::codecForTr() (Latin-1 if none has |
|
1571 |
been set). |
|
1572 |
\value UnicodeUTF8 UTF-8. |
|
1573 |
\value DefaultCodec (Obsolete) Use CodecForTr instead. |
|
1574 |
||
1575 |
\sa QObject::tr(), QObject::trUtf8(), QString::fromUtf8() |
|
1576 |
*/ |
|
1577 |
||
1578 |
/*! |
|
1579 |
Tells the application to exit with return code 0 (success). |
|
1580 |
Equivalent to calling QCoreApplication::exit(0). |
|
1581 |
||
1582 |
It's common to connect the QApplication::lastWindowClosed() signal |
|
1583 |
to quit(), and you also often connect e.g. QAbstractButton::clicked() or |
|
1584 |
signals in QAction, QMenu, or QMenuBar to it. |
|
1585 |
||
1586 |
Example: |
|
1587 |
||
1588 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 1 |
|
1589 |
||
1590 |
\sa exit(), aboutToQuit(), QApplication::lastWindowClosed() |
|
1591 |
*/ |
|
1592 |
||
1593 |
void QCoreApplication::quit() |
|
1594 |
{ |
|
1595 |
exit(0); |
|
1596 |
} |
|
1597 |
||
1598 |
/*! |
|
1599 |
\fn void QCoreApplication::aboutToQuit() |
|
1600 |
||
1601 |
This signal is emitted when the application is about to quit the |
|
1602 |
main event loop, e.g. when the event loop level drops to zero. |
|
1603 |
This may happen either after a call to quit() from inside the |
|
1604 |
application or when the users shuts down the entire desktop session. |
|
1605 |
||
1606 |
The signal is particularly useful if your application has to do some |
|
1607 |
last-second cleanup. Note that no user interaction is possible in |
|
1608 |
this state. |
|
1609 |
||
1610 |
\sa quit() |
|
1611 |
*/ |
|
1612 |
||
1613 |
#ifndef QT_NO_TRANSLATION |
|
1614 |
/*! |
|
1615 |
Adds the translation file \a translationFile to the list of |
|
1616 |
translation files to be used for translations. |
|
1617 |
||
1618 |
Multiple translation files can be installed. Translations are |
|
1619 |
searched for in the reverse order in which they were installed, |
|
1620 |
so the most recently installed translation file is searched first |
|
1621 |
and the first translation file installed is searched last. |
|
1622 |
The search stops as soon as a translation containing a matching |
|
1623 |
string is found. |
|
1624 |
||
1625 |
Installing or removing a QTranslator, or changing an installed QTranslator |
|
1626 |
generates a \l{QEvent::LanguageChange}{LanguageChange} event for the |
|
1627 |
QCoreApplication instance. A QApplication instance will propagate the event |
|
1628 |
to all toplevel windows, where a reimplementation of changeEvent can |
|
1629 |
re-translate the user interface by passing user-visible strings via the |
|
1630 |
tr() function to the respective property setters. User-interface classes |
|
1631 |
generated by \l{Qt Designer} provide a \c retranslateUi() function that can be |
|
1632 |
called. |
|
1633 |
||
1634 |
\sa removeTranslator() translate() QTranslator::load() {Dynamic Translation} |
|
1635 |
*/ |
|
1636 |
||
1637 |
void QCoreApplication::installTranslator(QTranslator *translationFile) |
|
1638 |
{ |
|
1639 |
if (!translationFile) |
|
1640 |
return; |
|
1641 |
||
1642 |
if (!QCoreApplicationPrivate::checkInstance("installTranslator")) |
|
1643 |
return; |
|
1644 |
QCoreApplicationPrivate *d = self->d_func(); |
|
1645 |
d->translators.prepend(translationFile); |
|
1646 |
||
1647 |
#ifndef QT_NO_TRANSLATION_BUILDER |
|
1648 |
if (translationFile->isEmpty()) |
|
1649 |
return; |
|
1650 |
#endif |
|
1651 |
||
1652 |
QEvent ev(QEvent::LanguageChange); |
|
1653 |
QCoreApplication::sendEvent(self, &ev); |
|
1654 |
} |
|
1655 |
||
1656 |
/*! |
|
1657 |
Removes the translation file \a translationFile from the list of |
|
1658 |
translation files used by this application. (It does not delete the |
|
1659 |
translation file from the file system.) |
|
1660 |
||
1661 |
\sa installTranslator() translate(), QObject::tr() |
|
1662 |
*/ |
|
1663 |
||
1664 |
void QCoreApplication::removeTranslator(QTranslator *translationFile) |
|
1665 |
{ |
|
1666 |
if (!translationFile) |
|
1667 |
return; |
|
1668 |
if (!QCoreApplicationPrivate::checkInstance("removeTranslator")) |
|
1669 |
return; |
|
1670 |
QCoreApplicationPrivate *d = self->d_func(); |
|
1671 |
if (d->translators.removeAll(translationFile) && !self->closingDown()) { |
|
1672 |
QEvent ev(QEvent::LanguageChange); |
|
1673 |
QCoreApplication::sendEvent(self, &ev); |
|
1674 |
} |
|
1675 |
} |
|
1676 |
||
1677 |
/*! |
|
1678 |
\overload translate() |
|
1679 |
*/ |
|
1680 |
QString QCoreApplication::translate(const char *context, const char *sourceText, |
|
1681 |
const char *disambiguation, Encoding encoding) |
|
1682 |
{ |
|
1683 |
return translate(context, sourceText, disambiguation, encoding, -1); |
|
1684 |
} |
|
1685 |
||
1686 |
static void replacePercentN(QString *result, int n) |
|
1687 |
{ |
|
1688 |
if (n >= 0) { |
|
1689 |
int percentPos = 0; |
|
1690 |
int len = 0; |
|
1691 |
while ((percentPos = result->indexOf(QLatin1Char('%'), percentPos + len)) != -1) { |
|
1692 |
len = 1; |
|
1693 |
QString fmt; |
|
1694 |
if (result->at(percentPos + len) == QLatin1Char('L')) { |
|
1695 |
++len; |
|
1696 |
fmt = QLatin1String("%L1"); |
|
1697 |
} else { |
|
1698 |
fmt = QLatin1String("%1"); |
|
1699 |
} |
|
1700 |
if (result->at(percentPos + len) == QLatin1Char('n')) { |
|
1701 |
fmt = fmt.arg(n); |
|
1702 |
++len; |
|
1703 |
result->replace(percentPos, len, fmt); |
|
1704 |
len = fmt.length(); |
|
1705 |
} |
|
1706 |
} |
|
1707 |
} |
|
1708 |
} |
|
1709 |
||
1710 |
/*! |
|
1711 |
\reentrant |
|
1712 |
\since 4.5 |
|
1713 |
||
1714 |
Returns the translation text for \a sourceText, by querying the |
|
1715 |
installed translation files. The translation files are searched |
|
1716 |
from the most recently installed file back to the first |
|
1717 |
installed file. |
|
1718 |
||
1719 |
QObject::tr() and QObject::trUtf8() provide this functionality |
|
1720 |
more conveniently. |
|
1721 |
||
1722 |
\a context is typically a class name (e.g., "MyDialog") and \a |
|
1723 |
sourceText is either English text or a short identifying text. |
|
1724 |
||
1725 |
\a disambiguation is an identifying string, for when the same \a |
|
1726 |
sourceText is used in different roles within the same context. By |
|
1727 |
default, it is null. |
|
1728 |
||
1729 |
See the \l QTranslator and \l QObject::tr() documentation for |
|
1730 |
more information about contexts, disambiguations and comments. |
|
1731 |
||
1732 |
\a encoding indicates the 8-bit encoding of character strings. |
|
1733 |
||
1734 |
\a n is used in conjunction with \c %n to support plural forms. |
|
1735 |
See QObject::tr() for details. |
|
1736 |
||
1737 |
If none of the translation files contain a translation for \a |
|
1738 |
sourceText in \a context, this function returns a QString |
|
1739 |
equivalent of \a sourceText. The encoding of \a sourceText is |
|
1740 |
specified by \e encoding; it defaults to CodecForTr. |
|
1741 |
||
1742 |
This function is not virtual. You can use alternative translation |
|
1743 |
techniques by subclassing \l QTranslator. |
|
1744 |
||
1745 |
\warning This method is reentrant only if all translators are |
|
1746 |
installed \e before calling this method. Installing or removing |
|
1747 |
translators while performing translations is not supported. Doing |
|
1748 |
so will most likely result in crashes or other undesirable |
|
1749 |
behavior. |
|
1750 |
||
1751 |
\sa QObject::tr() installTranslator() QTextCodec::codecForTr() |
|
1752 |
*/ |
|
1753 |
||
1754 |
||
1755 |
QString QCoreApplication::translate(const char *context, const char *sourceText, |
|
1756 |
const char *disambiguation, Encoding encoding, int n) |
|
1757 |
{ |
|
1758 |
QString result; |
|
1759 |
||
1760 |
if (!sourceText) |
|
1761 |
return result; |
|
1762 |
||
1763 |
if (self && !self->d_func()->translators.isEmpty()) { |
|
1764 |
QList<QTranslator*>::ConstIterator it; |
|
1765 |
QTranslator *translationFile; |
|
1766 |
for (it = self->d_func()->translators.constBegin(); it != self->d_func()->translators.constEnd(); ++it) { |
|
1767 |
translationFile = *it; |
|
1768 |
result = translationFile->translate(context, sourceText, disambiguation, n); |
|
1769 |
if (!result.isEmpty()) |
|
1770 |
break; |
|
1771 |
} |
|
1772 |
} |
|
1773 |
||
1774 |
if (result.isEmpty()) { |
|
1775 |
#ifdef QT_NO_TEXTCODEC |
|
1776 |
Q_UNUSED(encoding) |
|
1777 |
#else |
|
1778 |
if (encoding == UnicodeUTF8) |
|
1779 |
result = QString::fromUtf8(sourceText); |
|
1780 |
else if (QTextCodec::codecForTr() != 0) |
|
1781 |
result = QTextCodec::codecForTr()->toUnicode(sourceText); |
|
1782 |
else |
|
1783 |
#endif |
|
1784 |
result = QString::fromLatin1(sourceText); |
|
1785 |
} |
|
1786 |
||
1787 |
replacePercentN(&result, n); |
|
1788 |
return result; |
|
1789 |
} |
|
1790 |
||
1791 |
// Declared in qglobal.h |
|
1792 |
QString qtTrId(const char *id, int n) |
|
1793 |
{ |
|
1794 |
return QCoreApplication::translate(0, id, 0, QCoreApplication::UnicodeUTF8, n); |
|
1795 |
} |
|
1796 |
||
1797 |
bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator) |
|
1798 |
{ |
|
1799 |
return QCoreApplication::self |
|
1800 |
&& QCoreApplication::self->d_func()->translators.contains(translator); |
|
1801 |
} |
|
1802 |
||
1803 |
#endif //QT_NO_TRANSLATE |
|
1804 |
||
1805 |
/*! |
|
1806 |
Returns the directory that contains the application executable. |
|
1807 |
||
1808 |
For example, if you have installed Qt in the \c{C:\Trolltech\Qt} |
|
1809 |
directory, and you run the \c{regexp} example, this function will |
|
1810 |
return "C:/Trolltech/Qt/examples/tools/regexp". |
|
1811 |
||
1812 |
On Mac OS X this will point to the directory actually containing the |
|
1813 |
executable, which may be inside of an application bundle (if the |
|
1814 |
application is bundled). |
|
1815 |
||
1816 |
\warning On Linux, this function will try to get the path from the |
|
1817 |
\c {/proc} file system. If that fails, it assumes that \c |
|
1818 |
{argv[0]} contains the absolute file name of the executable. The |
|
1819 |
function also assumes that the current directory has not been |
|
1820 |
changed by the application. |
|
1821 |
||
1822 |
In Symbian this function will return the application private directory, |
|
1823 |
not the path to executable itself, as those are always in \c {/sys/bin}. |
|
1824 |
If the application is in a read only drive, i.e. ROM, then the private path |
|
1825 |
on the system drive will be returned. |
|
1826 |
||
1827 |
\sa applicationFilePath() |
|
1828 |
*/ |
|
1829 |
QString QCoreApplication::applicationDirPath() |
|
1830 |
{ |
|
1831 |
if (!self) { |
|
1832 |
qWarning("QCoreApplication::applicationDirPath: Please instantiate the QApplication object first"); |
|
1833 |
return QString(); |
|
1834 |
} |
|
1835 |
||
1836 |
QCoreApplicationPrivate *d = self->d_func(); |
|
1837 |
if (d->cachedApplicationDirPath.isNull()) |
|
1838 |
#if defined(Q_OS_SYMBIAN) |
|
1839 |
{ |
|
1840 |
QString appPath; |
|
1841 |
RFs& fs = qt_s60GetRFs(); |
|
1842 |
TChar driveChar; |
|
1843 |
QChar qDriveChar; |
|
1844 |
driveChar = (RProcess().FileName())[0]; |
|
1845 |
||
1846 |
//Check if the process is installed in a read only drive (typically ROM), |
|
1847 |
//and use the system drive (typically C:) if so. |
|
1848 |
TInt drive; |
|
1849 |
TDriveInfo driveInfo; |
|
1850 |
TInt err = fs.CharToDrive(driveChar, drive); |
|
1851 |
if (err == KErrNone) { |
|
1852 |
err = fs.Drive(driveInfo, drive); |
|
1853 |
} |
|
1854 |
if (err != KErrNone || (driveInfo.iDriveAtt & KDriveAttRom) || (driveInfo.iMediaAtt |
|
1855 |
& KMediaAttWriteProtected)) { |
|
1856 |
if(!PtrGetSystemDrive) |
|
1857 |
PtrGetSystemDrive = reinterpret_cast<SystemDriveFunc>(qt_resolveS60PluginFunc(S60Plugin_GetSystemDrive)); |
|
1858 |
Q_ASSERT(PtrGetSystemDrive); |
|
1859 |
drive = PtrGetSystemDrive(fs); |
|
1860 |
fs.DriveToChar(drive, driveChar); |
|
1861 |
} |
|
1862 |
||
1863 |
qDriveChar = QChar(QLatin1Char(driveChar)).toUpper(); |
|
1864 |
||
1865 |
TFileName privatePath; |
|
1866 |
fs.PrivatePath(privatePath); |
|
1867 |
appPath = qt_TDesC2QString(privatePath); |
|
1868 |
appPath.prepend(QLatin1Char(':')).prepend(qDriveChar); |
|
1869 |
||
1870 |
// Create the appPath if it doesn't exist. Non-existing appPath will cause |
|
1871 |
// Platform Security violations later on if the app doesn't have AllFiles capability. |
|
1872 |
err = fs.CreatePrivatePath(drive); |
|
1873 |
if (err != KErrNone) |
|
1874 |
qWarning("QCoreApplication::applicationDirPath: Failed to create private path."); |
|
1875 |
||
1876 |
d->cachedApplicationDirPath = QFileInfo(appPath).path(); |
|
1877 |
} |
|
1878 |
#else |
|
1879 |
d->cachedApplicationDirPath = QFileInfo(applicationFilePath()).path(); |
|
1880 |
#endif |
|
1881 |
return d->cachedApplicationDirPath; |
|
1882 |
} |
|
1883 |
||
1884 |
/*! |
|
1885 |
Returns the file path of the application executable. |
|
1886 |
||
1887 |
For example, if you have installed Qt in the \c{/usr/local/qt} |
|
1888 |
directory, and you run the \c{regexp} example, this function will |
|
1889 |
return "/usr/local/qt/examples/tools/regexp/regexp". |
|
1890 |
||
1891 |
\warning On Linux, this function will try to get the path from the |
|
1892 |
\c {/proc} file system. If that fails, it assumes that \c |
|
1893 |
{argv[0]} contains the absolute file name of the executable. The |
|
1894 |
function also assumes that the current directory has not been |
|
1895 |
changed by the application. |
|
1896 |
||
1897 |
\sa applicationDirPath() |
|
1898 |
*/ |
|
1899 |
QString QCoreApplication::applicationFilePath() |
|
1900 |
{ |
|
1901 |
if (!self) { |
|
1902 |
qWarning("QCoreApplication::applicationFilePath: Please instantiate the QApplication object first"); |
|
1903 |
return QString(); |
|
1904 |
} |
|
1905 |
||
1906 |
QCoreApplicationPrivate *d = self->d_func(); |
|
1907 |
if (!d->cachedApplicationFilePath.isNull()) |
|
1908 |
return d->cachedApplicationFilePath; |
|
1909 |
||
1910 |
#if defined(Q_WS_WIN) |
|
1911 |
d->cachedApplicationFilePath = QFileInfo(qAppFileName()).filePath(); |
|
1912 |
return d->cachedApplicationFilePath; |
|
1913 |
#elif defined(Q_WS_MAC) |
|
1914 |
QString qAppFileName_str = qAppFileName(); |
|
1915 |
if(!qAppFileName_str.isEmpty()) { |
|
1916 |
QFileInfo fi(qAppFileName_str); |
|
1917 |
d->cachedApplicationFilePath = fi.exists() ? fi.canonicalFilePath() : QString(); |
|
1918 |
return d->cachedApplicationFilePath; |
|
1919 |
} |
|
1920 |
#endif |
|
1921 |
#if defined(Q_OS_SYMBIAN) |
|
1922 |
QString appPath; |
|
1923 |
RProcess proc; |
|
1924 |
TInt err = proc.Open(proc.Id()); |
|
1925 |
if (err == KErrNone) { |
|
1926 |
TFileName procName = proc.FileName(); |
|
1927 |
appPath.append(QString(reinterpret_cast<const QChar*>(procName.Ptr()), procName.Length())); |
|
1928 |
proc.Close(); |
|
1929 |
} |
|
1930 |
||
1931 |
d->cachedApplicationFilePath = appPath; |
|
1932 |
return d->cachedApplicationFilePath; |
|
1933 |
||
1934 |
#elif defined( Q_OS_UNIX ) |
|
1935 |
# ifdef Q_OS_LINUX |
|
1936 |
// Try looking for a /proc/<pid>/exe symlink first which points to |
|
1937 |
// the absolute path of the executable |
|
1938 |
QFileInfo pfi(QString::fromLatin1("/proc/%1/exe").arg(getpid())); |
|
1939 |
if (pfi.exists() && pfi.isSymLink()) { |
|
1940 |
d->cachedApplicationFilePath = pfi.canonicalFilePath(); |
|
1941 |
return d->cachedApplicationFilePath; |
|
1942 |
} |
|
1943 |
# endif |
|
1944 |
||
1945 |
QString argv0 = QFile::decodeName(QByteArray(argv()[0])); |
|
1946 |
QString absPath; |
|
1947 |
||
1948 |
if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) { |
|
1949 |
/* |
|
1950 |
If argv0 starts with a slash, it is already an absolute |
|
1951 |
file path. |
|
1952 |
*/ |
|
1953 |
absPath = argv0; |
|
1954 |
} else if (argv0.contains(QLatin1Char('/'))) { |
|
1955 |
/* |
|
1956 |
If argv0 contains one or more slashes, it is a file path |
|
1957 |
relative to the current directory. |
|
1958 |
*/ |
|
1959 |
absPath = QDir::current().absoluteFilePath(argv0); |
|
1960 |
} else { |
|
1961 |
/* |
|
1962 |
Otherwise, the file path has to be determined using the |
|
1963 |
PATH environment variable. |
|
1964 |
*/ |
|
1965 |
QByteArray pEnv = qgetenv("PATH"); |
|
1966 |
QDir currentDir = QDir::current(); |
|
1967 |
QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1Char(':')); |
|
1968 |
for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) { |
|
1969 |
if ((*p).isEmpty()) |
|
1970 |
continue; |
|
1971 |
QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0); |
|
1972 |
QFileInfo candidate_fi(candidate); |
|
1973 |
if (candidate_fi.exists() && !candidate_fi.isDir()) { |
|
1974 |
absPath = candidate; |
|
1975 |
break; |
|
1976 |
} |
|
1977 |
} |
|
1978 |
} |
|
1979 |
||
1980 |
absPath = QDir::cleanPath(absPath); |
|
1981 |
||
1982 |
QFileInfo fi(absPath); |
|
1983 |
d->cachedApplicationFilePath = fi.exists() ? fi.canonicalFilePath() : QString(); |
|
1984 |
return d->cachedApplicationFilePath; |
|
1985 |
#endif |
|
1986 |
} |
|
1987 |
||
1988 |
/*! |
|
1989 |
\since 4.4 |
|
1990 |
||
1991 |
Returns the current process ID for the application. |
|
1992 |
*/ |
|
1993 |
qint64 QCoreApplication::applicationPid() |
|
1994 |
{ |
|
1995 |
#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) |
|
1996 |
return GetCurrentProcessId(); |
|
1997 |
#elif defined(Q_OS_VXWORKS) |
|
1998 |
return (pid_t) taskIdCurrent; |
|
1999 |
#else |
|
2000 |
return getpid(); |
|
2001 |
#endif |
|
2002 |
} |
|
2003 |
||
2004 |
/*! |
|
2005 |
\obsolete |
|
2006 |
||
2007 |
Use arguments().size() instead. |
|
2008 |
*/ |
|
2009 |
int QCoreApplication::argc() |
|
2010 |
{ |
|
2011 |
if (!self) { |
|
2012 |
qWarning("QCoreApplication::argc: Please instantiate the QApplication object first"); |
|
2013 |
return 0; |
|
2014 |
} |
|
2015 |
return self->d_func()->argc; |
|
2016 |
} |
|
2017 |
||
2018 |
||
2019 |
/*! |
|
2020 |
\obsolete |
|
2021 |
||
2022 |
Use arguments() instead. |
|
2023 |
*/ |
|
2024 |
char **QCoreApplication::argv() |
|
2025 |
{ |
|
2026 |
if (!self) { |
|
2027 |
qWarning("QCoreApplication::argv: Please instantiate the QApplication object first"); |
|
2028 |
return 0; |
|
2029 |
} |
|
2030 |
return self->d_func()->argv; |
|
2031 |
} |
|
2032 |
||
2033 |
/*! |
|
2034 |
\since 4.1 |
|
2035 |
||
2036 |
Returns the list of command-line arguments. |
|
2037 |
||
2038 |
Usually arguments().at(0) is the program name, arguments().at(1) |
|
2039 |
is the first argument, and arguments().last() is the last |
|
2040 |
argument. See the note below about Windows. |
|
2041 |
||
2042 |
Calling this function is slow - you should store the result in a variable |
|
2043 |
when parsing the command line. |
|
2044 |
||
2045 |
\warning On Unix, this list is built from the argc and argv parameters passed |
|
2046 |
to the constructor in the main() function. The string-data in argv is |
|
2047 |
interpreted using QString::fromLocal8Bit(); hence it is not possible to |
|
2048 |
pass, for example, Japanese command line arguments on a system that runs in a |
|
2049 |
Latin1 locale. Most modern Unix systems do not have this limitation, as they are |
|
2050 |
Unicode-based. |
|
2051 |
||
2052 |
On NT-based Windows, this limitation does not apply either. |
|
2053 |
On Windows, the arguments() are not built from the contents of argv/argc, as |
|
2054 |
the content does not support Unicode. Instead, the arguments() are constructed |
|
2055 |
from the return value of |
|
2056 |
\l{http://msdn2.microsoft.com/en-us/library/ms683156(VS.85).aspx}{GetCommandLine()}. |
|
2057 |
As a result of this, the string given by arguments().at(0) might not be |
|
2058 |
the program name on Windows, depending on how the application was started. |
|
2059 |
||
2060 |
\sa applicationFilePath() |
|
2061 |
*/ |
|
2062 |
||
2063 |
QStringList QCoreApplication::arguments() |
|
2064 |
{ |
|
2065 |
QStringList list; |
|
2066 |
||
2067 |
if (!self) { |
|
2068 |
qWarning("QCoreApplication::arguments: Please instantiate the QApplication object first"); |
|
2069 |
return list; |
|
2070 |
} |
|
2071 |
#ifdef Q_OS_WIN |
|
2072 |
QString cmdline = QString::fromWCharArray(GetCommandLine()); |
|
2073 |
||
2074 |
#if defined(Q_OS_WINCE) |
|
2075 |
wchar_t tempFilename[MAX_PATH+1]; |
|
2076 |
if (GetModuleFileName(0, tempFilename, MAX_PATH)) { |
|
2077 |
tempFilename[MAX_PATH] = 0; |
|
2078 |
cmdline.prepend(QLatin1Char('\"') + QString::fromWCharArray(tempFilename) + QLatin1String("\" ")); |
|
2079 |
} |
|
2080 |
#endif // Q_OS_WINCE |
|
2081 |
||
2082 |
list = qWinCmdArgs(cmdline); |
|
2083 |
if (self->d_func()->application_type) { // GUI app? Skip known - see qapplication.cpp |
|
2084 |
QStringList stripped; |
|
2085 |
for (int a = 0; a < list.count(); ++a) { |
|
2086 |
QString arg = list.at(a); |
|
2087 |
QByteArray l1arg = arg.toLatin1(); |
|
2088 |
if (l1arg == "-qdevel" || |
|
2089 |
l1arg == "-qdebug" || |
|
2090 |
l1arg == "-reverse" || |
|
2091 |
l1arg == "-stylesheet" || |
|
2092 |
l1arg == "-widgetcount") |
|
2093 |
; |
|
2094 |
else if (l1arg.startsWith("-style=")) |
|
2095 |
; |
|
2096 |
else if (l1arg == "-style" || |
|
2097 |
l1arg == "-session" || |
|
2098 |
l1arg == "-graphicssystem" || |
|
2099 |
l1arg == "-testability") |
|
2100 |
++a; |
|
2101 |
else |
|
2102 |
stripped += arg; |
|
2103 |
} |
|
2104 |
list = stripped; |
|
2105 |
} |
|
2106 |
#else |
|
2107 |
const int ac = self->d_func()->argc; |
|
2108 |
char ** const av = self->d_func()->argv; |
|
2109 |
for (int a = 0; a < ac; ++a) { |
|
2110 |
list << QString::fromLocal8Bit(av[a]); |
|
2111 |
} |
|
2112 |
#endif |
|
2113 |
||
2114 |
return list; |
|
2115 |
} |
|
2116 |
||
2117 |
/*! |
|
2118 |
\property QCoreApplication::organizationName |
|
2119 |
\brief the name of the organization that wrote this application |
|
2120 |
||
2121 |
The value is used by the QSettings class when it is constructed |
|
2122 |
using the empty constructor. This saves having to repeat this |
|
2123 |
information each time a QSettings object is created. |
|
2124 |
||
2125 |
On Mac, QSettings uses organizationDomain() as the organization |
|
2126 |
if it's not an empty string; otherwise it uses |
|
2127 |
organizationName(). On all other platforms, QSettings uses |
|
2128 |
organizationName() as the organization. |
|
2129 |
||
2130 |
\sa organizationDomain applicationName |
|
2131 |
*/ |
|
2132 |
||
2133 |
void QCoreApplication::setOrganizationName(const QString &orgName) |
|
2134 |
{ |
|
2135 |
coreappdata()->orgName = orgName; |
|
2136 |
} |
|
2137 |
||
2138 |
QString QCoreApplication::organizationName() |
|
2139 |
{ |
|
2140 |
return coreappdata()->orgName; |
|
2141 |
} |
|
2142 |
||
2143 |
/*! |
|
2144 |
\property QCoreApplication::organizationDomain |
|
2145 |
\brief the Internet domain of the organization that wrote this application |
|
2146 |
||
2147 |
The value is used by the QSettings class when it is constructed |
|
2148 |
using the empty constructor. This saves having to repeat this |
|
2149 |
information each time a QSettings object is created. |
|
2150 |
||
2151 |
On Mac, QSettings uses organizationDomain() as the organization |
|
2152 |
if it's not an empty string; otherwise it uses organizationName(). |
|
2153 |
On all other platforms, QSettings uses organizationName() as the |
|
2154 |
organization. |
|
2155 |
||
2156 |
\sa organizationName applicationName applicationVersion |
|
2157 |
*/ |
|
2158 |
void QCoreApplication::setOrganizationDomain(const QString &orgDomain) |
|
2159 |
{ |
|
2160 |
coreappdata()->orgDomain = orgDomain; |
|
2161 |
} |
|
2162 |
||
2163 |
QString QCoreApplication::organizationDomain() |
|
2164 |
{ |
|
2165 |
return coreappdata()->orgDomain; |
|
2166 |
} |
|
2167 |
||
2168 |
/*! |
|
2169 |
\property QCoreApplication::applicationName |
|
2170 |
\brief the name of this application |
|
2171 |
||
2172 |
The value is used by the QSettings class when it is constructed |
|
2173 |
using the empty constructor. This saves having to repeat this |
|
2174 |
information each time a QSettings object is created. |
|
2175 |
||
2176 |
\sa organizationName organizationDomain applicationVersion |
|
2177 |
*/ |
|
2178 |
void QCoreApplication::setApplicationName(const QString &application) |
|
2179 |
{ |
|
2180 |
coreappdata()->application = application; |
|
2181 |
} |
|
2182 |
||
2183 |
QString QCoreApplication::applicationName() |
|
2184 |
{ |
|
2185 |
return coreappdata()->application; |
|
2186 |
} |
|
2187 |
||
2188 |
/*! |
|
2189 |
\property QCoreApplication::applicationVersion |
|
2190 |
\since 4.4 |
|
2191 |
\brief the version of this application |
|
2192 |
||
2193 |
\sa applicationName organizationName organizationDomain |
|
2194 |
*/ |
|
2195 |
void QCoreApplication::setApplicationVersion(const QString &version) |
|
2196 |
{ |
|
2197 |
coreappdata()->applicationVersion = version; |
|
2198 |
} |
|
2199 |
||
2200 |
QString QCoreApplication::applicationVersion() |
|
2201 |
{ |
|
2202 |
return coreappdata()->applicationVersion; |
|
2203 |
} |
|
2204 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
2205 |
#ifndef QT_NO_LIBRARY |
0 | 2206 |
|
2207 |
Q_GLOBAL_STATIC_WITH_ARGS(QMutex, libraryPathMutex, (QMutex::Recursive)) |
|
2208 |
||
2209 |
/*! |
|
2210 |
Returns a list of paths that the application will search when |
|
2211 |
dynamically loading libraries. |
|
2212 |
||
2213 |
Qt provides default library paths, but they can also be set using |
|
2214 |
a \l{Using qt.conf}{qt.conf} file. Paths specified in this file |
|
2215 |
will override default values. |
|
2216 |
||
2217 |
This list will include the installation directory for plugins if |
|
2218 |
it exists (the default installation directory for plugins is \c |
|
2219 |
INSTALL/plugins, where \c INSTALL is the directory where Qt was |
|
2220 |
installed). The directory of the application executable (NOT the |
|
2221 |
working directory) is always added, as well as the colon separated |
|
2222 |
entries of the QT_PLUGIN_PATH environment variable. |
|
2223 |
||
2224 |
If you want to iterate over the list, you can use the \l foreach |
|
2225 |
pseudo-keyword: |
|
2226 |
||
2227 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 2 |
|
2228 |
||
2229 |
\sa setLibraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary, |
|
2230 |
{How to Create Qt Plugins} |
|
2231 |
*/ |
|
2232 |
QStringList QCoreApplication::libraryPaths() |
|
2233 |
{ |
|
2234 |
QMutexLocker locker(libraryPathMutex()); |
|
2235 |
if (!coreappdata()->app_libpaths) { |
|
2236 |
QStringList *app_libpaths = coreappdata()->app_libpaths = new QStringList; |
|
2237 |
QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath); |
|
2238 |
#if defined(Q_OS_SYMBIAN) |
|
2239 |
// Add existing path on all drives for relative PluginsPath in Symbian |
|
2240 |
if (installPathPlugins.at(1) != QChar(QLatin1Char(':'))) { |
|
2241 |
QString tempPath = installPathPlugins; |
|
2242 |
if (tempPath.at(tempPath.length() - 1) != QDir::separator()) { |
|
2243 |
tempPath += QDir::separator(); |
|
2244 |
} |
|
2245 |
RFs& fs = qt_s60GetRFs(); |
|
2246 |
TPtrC tempPathPtr(reinterpret_cast<const TText*> (tempPath.constData())); |
|
2247 |
TFindFile finder(fs); |
|
2248 |
TInt err = finder.FindByDir(tempPathPtr, tempPathPtr); |
|
2249 |
while (err == KErrNone) { |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2250 |
QString foundDir(reinterpret_cast<const QChar *>(finder.File().Ptr()), |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2251 |
finder.File().Length()); |
0 | 2252 |
foundDir = QDir(foundDir).canonicalPath(); |
2253 |
if (!app_libpaths->contains(foundDir)) |
|
2254 |
app_libpaths->append(foundDir); |
|
2255 |
err = finder.Find(); |
|
2256 |
} |
|
2257 |
} |
|
2258 |
#else |
|
2259 |
if (QFile::exists(installPathPlugins)) { |
|
2260 |
// Make sure we convert from backslashes to slashes. |
|
2261 |
installPathPlugins = QDir(installPathPlugins).canonicalPath(); |
|
2262 |
if (!app_libpaths->contains(installPathPlugins)) |
|
2263 |
app_libpaths->append(installPathPlugins); |
|
2264 |
} |
|
2265 |
#endif |
|
2266 |
||
2267 |
// If QCoreApplication is not yet instantiated, |
|
2268 |
// make sure we add the application path when we construct the QCoreApplication |
|
2269 |
if (self) self->d_func()->appendApplicationPathToLibraryPaths(); |
|
2270 |
||
2271 |
const QByteArray libPathEnv = qgetenv("QT_PLUGIN_PATH"); |
|
2272 |
if (!libPathEnv.isEmpty()) { |
|
2273 |
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) |
|
2274 |
QLatin1Char pathSep(';'); |
|
2275 |
#else |
|
2276 |
QLatin1Char pathSep(':'); |
|
2277 |
#endif |
|
2278 |
QStringList paths = QString::fromLatin1(libPathEnv).split(pathSep, QString::SkipEmptyParts); |
|
2279 |
for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) { |
|
2280 |
QString canonicalPath = QDir(*it).canonicalPath(); |
|
2281 |
if (!canonicalPath.isEmpty() |
|
2282 |
&& !app_libpaths->contains(canonicalPath)) { |
|
2283 |
app_libpaths->append(canonicalPath); |
|
2284 |
} |
|
2285 |
} |
|
2286 |
} |
|
2287 |
} |
|
2288 |
return *(coreappdata()->app_libpaths); |
|
2289 |
} |
|
2290 |
||
2291 |
||
2292 |
||
2293 |
/*! |
|
2294 |
||
2295 |
Sets the list of directories to search when loading libraries to |
|
2296 |
\a paths. All existing paths will be deleted and the path list |
|
2297 |
will consist of the paths given in \a paths. |
|
2298 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2299 |
In Symbian this function is only useful for setting paths for |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2300 |
finding Qt extension plugin stubs, since the OS can only |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2301 |
load libraries from the \c{/sys/bin} directory. |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2302 |
|
0 | 2303 |
\sa libraryPaths(), addLibraryPath(), removeLibraryPath(), QLibrary |
2304 |
*/ |
|
2305 |
void QCoreApplication::setLibraryPaths(const QStringList &paths) |
|
2306 |
{ |
|
2307 |
QMutexLocker locker(libraryPathMutex()); |
|
2308 |
if (!coreappdata()->app_libpaths) |
|
2309 |
coreappdata()->app_libpaths = new QStringList; |
|
2310 |
*(coreappdata()->app_libpaths) = paths; |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
2311 |
locker.unlock(); |
0 | 2312 |
QFactoryLoader::refreshAll(); |
2313 |
} |
|
2314 |
||
2315 |
/*! |
|
2316 |
Prepends \a path to the beginning of the library path list, ensuring that |
|
2317 |
it is searched for libraries first. If \a path is empty or already in the |
|
2318 |
path list, the path list is not changed. |
|
2319 |
||
2320 |
The default path list consists of a single entry, the installation |
|
2321 |
directory for plugins. The default installation directory for plugins |
|
2322 |
is \c INSTALL/plugins, where \c INSTALL is the directory where Qt was |
|
2323 |
installed. |
|
2324 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2325 |
In Symbian this function is only useful for adding paths for |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2326 |
finding Qt extension plugin stubs, since the OS can only |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2327 |
load libraries from the \c{/sys/bin} directory. |
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2328 |
|
0 | 2329 |
\sa removeLibraryPath(), libraryPaths(), setLibraryPaths() |
2330 |
*/ |
|
2331 |
void QCoreApplication::addLibraryPath(const QString &path) |
|
2332 |
{ |
|
2333 |
if (path.isEmpty()) |
|
2334 |
return; |
|
2335 |
||
2336 |
QMutexLocker locker(libraryPathMutex()); |
|
2337 |
||
2338 |
// make sure that library paths is initialized |
|
2339 |
libraryPaths(); |
|
2340 |
||
2341 |
QString canonicalPath = QDir(path).canonicalPath(); |
|
2342 |
if (!canonicalPath.isEmpty() |
|
2343 |
&& !coreappdata()->app_libpaths->contains(canonicalPath)) { |
|
2344 |
coreappdata()->app_libpaths->prepend(canonicalPath); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
2345 |
locker.unlock(); |
0 | 2346 |
QFactoryLoader::refreshAll(); |
2347 |
} |
|
2348 |
} |
|
2349 |
||
2350 |
/*! |
|
2351 |
Removes \a path from the library path list. If \a path is empty or not |
|
2352 |
in the path list, the list is not changed. |
|
2353 |
||
2354 |
\sa addLibraryPath(), libraryPaths(), setLibraryPaths() |
|
2355 |
*/ |
|
2356 |
void QCoreApplication::removeLibraryPath(const QString &path) |
|
2357 |
{ |
|
2358 |
if (path.isEmpty()) |
|
2359 |
return; |
|
2360 |
||
2361 |
QMutexLocker locker(libraryPathMutex()); |
|
2362 |
||
2363 |
// make sure that library paths is initialized |
|
2364 |
libraryPaths(); |
|
2365 |
||
2366 |
QString canonicalPath = QDir(path).canonicalPath(); |
|
2367 |
coreappdata()->app_libpaths->removeAll(canonicalPath); |
|
2368 |
QFactoryLoader::refreshAll(); |
|
2369 |
} |
|
2370 |
||
2371 |
#endif //QT_NO_LIBRARY |
|
2372 |
||
2373 |
/*! |
|
2374 |
\typedef QCoreApplication::EventFilter |
|
2375 |
||
2376 |
A function with the following signature that can be used as an |
|
2377 |
event filter: |
|
2378 |
||
2379 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 3 |
|
2380 |
||
2381 |
\sa setEventFilter() |
|
2382 |
*/ |
|
2383 |
||
2384 |
/*! |
|
2385 |
\fn EventFilter QCoreApplication::setEventFilter(EventFilter filter) |
|
2386 |
||
2387 |
Replaces the event filter function for the QCoreApplication with |
|
2388 |
\a filter and returns the pointer to the replaced event filter |
|
2389 |
function. Only the current event filter function is called. If you |
|
2390 |
want to use both filter functions, save the replaced EventFilter |
|
2391 |
in a place where yours can call it. |
|
2392 |
||
2393 |
The event filter function set here is called for all messages |
|
2394 |
received by all threads meant for all Qt objects. It is \e not |
|
2395 |
called for messages that are not meant for Qt objects. |
|
2396 |
||
2397 |
The event filter function should return true if the message should |
|
2398 |
be filtered, (i.e. stopped). It should return false to allow |
|
2399 |
processing the message to continue. |
|
2400 |
||
2401 |
By default, no event filter function is set (i.e., this function |
|
2402 |
returns a null EventFilter the first time it is called). |
|
2403 |
||
2404 |
\note The filter function set here receives native messages, |
|
2405 |
i.e. MSG or XEvent structs, that are going to Qt objects. It is |
|
2406 |
called by QCoreApplication::filterEvent(). If the filter function |
|
2407 |
returns false to indicate the message should be processed further, |
|
2408 |
the native message can then be translated into a QEvent and |
|
2409 |
handled by the standard Qt \l{QEvent} {event} filering, e.g. |
|
2410 |
QObject::installEventFilter(). |
|
2411 |
||
2412 |
\note The filter function set here is different form the filter |
|
2413 |
function set via QAbstractEventDispatcher::setEventFilter(), which |
|
2414 |
gets all messages received by its thread, even messages meant for |
|
2415 |
objects that are not handled by Qt. |
|
2416 |
||
2417 |
\sa QObject::installEventFilter(), QAbstractEventDispatcher::setEventFilter() |
|
2418 |
*/ |
|
2419 |
QCoreApplication::EventFilter |
|
2420 |
QCoreApplication::setEventFilter(QCoreApplication::EventFilter filter) |
|
2421 |
{ |
|
2422 |
Q_D(QCoreApplication); |
|
2423 |
EventFilter old = d->eventFilter; |
|
2424 |
d->eventFilter = filter; |
|
2425 |
return old; |
|
2426 |
} |
|
2427 |
||
2428 |
/*! |
|
2429 |
Sends \a message through the event filter that was set by |
|
2430 |
setEventFilter(). If no event filter has been set, this function |
|
2431 |
returns false; otherwise, this function returns the result of the |
|
2432 |
event filter function in the \a result parameter. |
|
2433 |
||
2434 |
\sa setEventFilter() |
|
2435 |
*/ |
|
2436 |
bool QCoreApplication::filterEvent(void *message, long *result) |
|
2437 |
{ |
|
2438 |
Q_D(QCoreApplication); |
|
2439 |
if (result) |
|
2440 |
*result = 0; |
|
2441 |
if (d->eventFilter) |
|
2442 |
return d->eventFilter(message, result); |
|
2443 |
#ifdef Q_OS_WIN |
|
2444 |
return winEventFilter(reinterpret_cast<MSG *>(message), result); |
|
2445 |
#else |
|
2446 |
return false; |
|
2447 |
#endif |
|
2448 |
} |
|
2449 |
||
2450 |
/*! |
|
2451 |
This function returns true if there are pending events; otherwise |
|
2452 |
returns false. Pending events can be either from the window |
|
2453 |
system or posted events using postEvent(). |
|
2454 |
||
2455 |
\sa QAbstractEventDispatcher::hasPendingEvents() |
|
2456 |
*/ |
|
2457 |
bool QCoreApplication::hasPendingEvents() |
|
2458 |
{ |
|
2459 |
QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(); |
|
2460 |
if (eventDispatcher) |
|
2461 |
return eventDispatcher->hasPendingEvents(); |
|
2462 |
return false; |
|
2463 |
} |
|
2464 |
||
2465 |
#ifdef QT3_SUPPORT |
|
2466 |
/*! \fn void QCoreApplication::lock() |
|
2467 |
||
2468 |
In Qt 3, this function locked the Qt library mutex, allowing |
|
2469 |
non-GUI threads to perform basic printing operations using |
|
2470 |
QPainter. |
|
2471 |
||
2472 |
In Qt 4, this is no longer supported, since painting is only |
|
2473 |
supported from within a paint event handler. This function does |
|
2474 |
nothing. |
|
2475 |
||
2476 |
\sa QWidget::paintEvent() |
|
2477 |
*/ |
|
2478 |
||
2479 |
/*! \fn void QCoreApplication::unlock(bool wakeUpGui) |
|
2480 |
||
2481 |
In Qt 3, this function unlocked the Qt library mutex. The mutex |
|
2482 |
allowed non-GUI threads to perform basic printing operations |
|
2483 |
using QPainter. |
|
2484 |
||
2485 |
In Qt 4, this is no longer supported, since painting is only |
|
2486 |
supported from within a paint event handler. This function does |
|
2487 |
nothing. |
|
2488 |
*/ |
|
2489 |
||
2490 |
/*! \fn bool QCoreApplication::locked() |
|
2491 |
||
2492 |
This function does nothing. It is there to keep old code working. |
|
2493 |
It always returns false. |
|
2494 |
||
2495 |
See lock() for details. |
|
2496 |
*/ |
|
2497 |
||
2498 |
/*! \fn bool QCoreApplication::tryLock() |
|
2499 |
||
2500 |
This function does nothing. It is there to keep old code working. |
|
2501 |
It always returns false. |
|
2502 |
||
2503 |
See lock() for details. |
|
2504 |
*/ |
|
2505 |
||
2506 |
/*! \fn void QCoreApplication::processOneEvent() |
|
2507 |
\obsolete |
|
2508 |
||
2509 |
Waits for an event to occur, processes it, then returns. |
|
2510 |
||
2511 |
This function is useful for adapting Qt to situations where the |
|
2512 |
event processing must be grafted onto existing program loops. |
|
2513 |
||
2514 |
Using this function in new applications may be an indication of design |
|
2515 |
problems. |
|
2516 |
||
2517 |
\sa processEvents(), exec(), QTimer |
|
2518 |
*/ |
|
2519 |
||
2520 |
/*! \obsolete |
|
2521 |
||
2522 |
This function enters the main event loop (recursively). Do not call |
|
2523 |
it unless you really know what you are doing. |
|
2524 |
*/ |
|
2525 |
int QCoreApplication::enter_loop() |
|
2526 |
{ |
|
2527 |
if (!QCoreApplicationPrivate::checkInstance("enter_loop")) |
|
2528 |
return -1; |
|
2529 |
if (QThreadData::current() != self->d_func()->threadData) { |
|
2530 |
qWarning("QCoreApplication::enter_loop: Must be called from the main thread"); |
|
2531 |
return -1; |
|
2532 |
} |
|
2533 |
QEventLoop eventLoop; |
|
2534 |
int returnCode = eventLoop.exec(); |
|
2535 |
return returnCode; |
|
2536 |
} |
|
2537 |
||
2538 |
/*! \obsolete |
|
2539 |
||
2540 |
This function exits from a recursive call to the main event loop. |
|
2541 |
Do not call it unless you are an expert. |
|
2542 |
*/ |
|
2543 |
void QCoreApplication::exit_loop() |
|
2544 |
{ |
|
2545 |
if (!QCoreApplicationPrivate::checkInstance("exit_loop")) |
|
2546 |
return; |
|
2547 |
QThreadData *data = QThreadData::current(); |
|
2548 |
if (data != self->d_func()->threadData) { |
|
2549 |
qWarning("QCoreApplication::exit_loop: Must be called from the main thread"); |
|
2550 |
return; |
|
2551 |
} |
|
2552 |
if (!data->eventLoops.isEmpty()) |
|
2553 |
data->eventLoops.top()->exit(); |
|
2554 |
} |
|
2555 |
||
2556 |
/*! \obsolete |
|
2557 |
||
2558 |
Returns the current loop level. |
|
2559 |
*/ |
|
2560 |
int QCoreApplication::loopLevel() |
|
2561 |
{ |
|
2562 |
if (!QCoreApplicationPrivate::checkInstance("loopLevel")) |
|
2563 |
return -1; |
|
2564 |
return self->d_func()->threadData->eventLoops.size(); |
|
2565 |
} |
|
2566 |
#endif |
|
2567 |
||
2568 |
/* |
|
2569 |
\fn void QCoreApplication::watchUnixSignal(int signal, bool watch) |
|
2570 |
\internal |
|
2571 |
*/ |
|
2572 |
||
2573 |
/*! |
|
2574 |
\fn void QCoreApplication::unixSignal(int number) |
|
2575 |
\internal |
|
2576 |
||
2577 |
This signal is emitted whenever a Unix signal is received by the |
|
2578 |
application. The Unix signal received is specified by its \a number. |
|
2579 |
*/ |
|
2580 |
||
2581 |
/*! |
|
2582 |
\fn void qAddPostRoutine(QtCleanUpFunction ptr) |
|
2583 |
\relates QCoreApplication |
|
2584 |
||
2585 |
Adds a global routine that will be called from the QApplication |
|
2586 |
destructor. This function is normally used to add cleanup routines |
|
2587 |
for program-wide functionality. |
|
2588 |
||
2589 |
The function specified by \a ptr should take no arguments and should |
|
2590 |
return nothing. For example: |
|
2591 |
||
2592 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 4 |
|
2593 |
||
2594 |
Note that for an application- or module-wide cleanup, |
|
2595 |
qAddPostRoutine() is often not suitable. For example, if the |
|
2596 |
program is split into dynamically loaded modules, the relevant |
|
2597 |
module may be unloaded long before the QApplication destructor is |
|
2598 |
called. |
|
2599 |
||
2600 |
For modules and libraries, using a reference-counted |
|
2601 |
initialization manager or Qt's parent-child deletion mechanism may |
|
2602 |
be better. Here is an example of a private class that uses the |
|
2603 |
parent-child mechanism to call a cleanup function at the right |
|
2604 |
time: |
|
2605 |
||
2606 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 5 |
|
2607 |
||
2608 |
By selecting the right parent object, this can often be made to |
|
2609 |
clean up the module's data at the right moment. |
|
2610 |
*/ |
|
2611 |
||
2612 |
/*! |
|
2613 |
\macro Q_DECLARE_TR_FUNCTIONS(context) |
|
2614 |
\relates QCoreApplication |
|
2615 |
||
2616 |
The Q_DECLARE_TR_FUNCTIONS() macro declares and implements two |
|
2617 |
translation functions, \c tr() and \c trUtf8(), with these |
|
2618 |
signatures: |
|
2619 |
||
2620 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 6 |
|
2621 |
||
2622 |
This macro is useful if you want to use QObject::tr() or |
|
2623 |
QObject::trUtf8() in classes that don't inherit from QObject. |
|
2624 |
||
2625 |
Q_DECLARE_TR_FUNCTIONS() must appear at the very top of the |
|
2626 |
class definition (before the first \c{public:} or \c{protected:}). |
|
2627 |
For example: |
|
2628 |
||
2629 |
\snippet doc/src/snippets/code/src_corelib_kernel_qcoreapplication.cpp 7 |
|
2630 |
||
2631 |
The \a context parameter is normally the class name, but it can |
|
2632 |
be any string. |
|
2633 |
||
2634 |
\sa Q_OBJECT, QObject::tr(), QObject::trUtf8() |
|
2635 |
*/ |
|
2636 |
||
2637 |
QT_END_NAMESPACE |