[Interest] Event handling for QObjects created before QApplication

Alan Ezust alan.ezust at gmail.com
Sun Oct 5 17:50:11 CEST 2014


Here are my suggested changes to the threads.qdoc. What do you think?

diff --git a/doc/src/frameworks-technologies/threads.qdoc
b/doc/src/frameworks-technologies/threads.qdoc
index 5e92425..b5136fe 100644
--- a/doc/src/frameworks-technologies/threads.qdoc
+++ b/doc/src/frameworks-technologies/threads.qdoc
@@ -632,13 +632,23 @@
     event loop. The thread in which a QObject lives is available using
     QObject::thread().

-    Note that for QObjects that are created before QApplication,
-    QObject::thread() returns zero. This means that the main thread
-    will only handle posted events for these objects; other event
-    processing is not done at all for objects with no thread. Use the
-    QObject::moveToThread() function to change the thread affinity for
-    an object and its children (the object cannot be moved if it has a
-    parent).
+    Note that for QObjects that are created before the QApplication,
+    QObject::thread() returns zero. This means that the main thread will
+    not handle events for these objects. Event processing is not done at
+    all for objects with no thread.
+
+    The above paragraph is for academic interest only. In general,
+    creating QObjects before the QApplication is not supported
+    and can lead to weird crashes on exit, depending on the platform.
+    This means static instances of QObject are also not supported. A
+    properly structured single or multi-threaded application should make
+    the QApplication be the last QObject to be destroyed before exiting.
+
+    The QObject::moveToThread() function can be used to change the
+    thread affinity for a QObject and its children (the object cannot be
+    moved if it has a parent). Unfortunately, QObject::moveToThread()
+    does not work properly for complex QObjects with sub-QObjects, so it
+    is better to create these in the actual thread that uses them.

     Calling \c delete on a QObject from a thread other than the one
     that \e owns the object (or accessing the object in other ways) is



On Sun, Sep 21, 2014 at 5:46 PM, Thiago Macieira <thiago.macieira at intel.com>
wrote:

> On Monday 22 September 2014 04:24:21 Dmitrii Volosnykh wrote:
> > Threads and QObjects
> > <http://qt-project.org/doc/qt-5/threads-qobject.html> tutorial
> >
> > says:
> > > Note that for QObjects that are created before QApplication,
> > > QObject::thread() returns zero. This means that the main thread will
> only
> > > handle posted events for these objects; other event processing is not
> done
> > > at all for objects with no thread.
> >
> > Could someone elaborate on this, please? The most confusing part is "main
> > thread will only handle posted events for these objects".
>
> Qt is not supported before the creation of QCoreApplication. Your use-case
> is
> not supported, so no answer is necessary.
>
> Just don't do it.
>
> Note: the doc is wrong.
> --
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20141005/ddaf1e0e/attachment.html>


More information about the Interest mailing list