Results 1 to 4 of 4

Thread: Handling graphical objects (QWidget objects) in different threads ?

  1. #1
    Join Date
    Apr 2009
    Posts
    58
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Handling graphical objects (QWidget objects) in different threads ?

    Hi

    Is is possible to make different threads each handling different gui objects like window containing textedits, buttons etc. ?
    Qt documentation for Qthread says: "Note, however, that it is not possible to use any widget classes in the thread." but in that case, can QThread::moveToThread() be of any help ?

    just like in some chatting client, I want the main thread to handle main window - contact list etc, and the chatting window is handled by different thread(s) i.e. whenever somebody sends me a text message, the window popping up should pop up in a different thread.


    Thanks

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Handling graphical objects (QWidget objects) in different threads ?

    You can do the processing (computation, data base lookups, Internet socket access, whatever) in other threads; but actually updating the widgets themselves really does need to be done in the GUI thread. Signals and slots are great for this — do your processing in a QThread and have it signal a slot in the QWidget, which lives in the GUI thread.

    Don’t confuse separate windows with separate threads. Each window gets its own messages, but they all still live in the GUI thread. To the best of my knowledge, this is not a limitation of Qt: it is a limitation of virtually all windowing systems currently in use. (You are probably misunderstanding “some chatting client” if you think each window lives in a different thread.) The only way around this would be to spawn a completely new QProcess for each independent top-level window, and it’s unlikely that you’d want to do that, or that it would accomplish anything useful.

  3. #3
    Join Date
    Apr 2009
    Posts
    58
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Handling graphical objects (QWidget objects) in different threads ?

    Thanks for the reply :-)

    One more doubt on the same "thread" thing:
    what is the use of void QObject::moveToThread(QThread *targetThread) ?

    What exactly does moving a QObject to a different thread means ?

  4. #4
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Handling graphical objects (QWidget objects) in different threads ?

    Quote Originally Posted by anupamgee View Post
    What exactly does moving a QObject to a different thread means ?
    The thread in which a QObject “lives” is the thread in which events directed to that object are dispatched. For example, when a signal from one thread is connected to a slot of an object in a different thread in the default way (allowing the connection type to default to Qt::AutoConnection), the signal will be delivered through the event queue of the thread to which the object containing the slot belongs, so that the slot will be executed in the receiving object’s thread (not the signaling thread).

    Since an object initially lives in the thread in which its constructor ran, you would commonly call QObject::moveToThread if, after constructing an object in the GUI thread, you wanted signals, timer events, etc. sent to it to execute in some other thread.

    As far as I can tell, this is the only practical effect of thread affinity — it determines which thread’s event queue will deliver events to the object.

    QThread itself can be confusing; like any other QObject, a QThread object initially lives in the thread that constructed it — not the thread represented by the QThread object, which is the new thread in which QThread::run executes after QThread::start is called. You can, however, move a QThread to itself, so long as you do that from the thread in which it was constructed.

Similar Threads

  1. QSharedMemory and C++ objects
    By MattPhillips in forum Qt Programming
    Replies: 7
    Last Post: 29th November 2010, 08:42
  2. Replies: 11
    Last Post: 25th February 2009, 18:35
  3. Qt/C++: Treeview of Objects
    By lynx in forum Qt Programming
    Replies: 0
    Last Post: 2nd October 2008, 15:43
  4. Replies: 7
    Last Post: 18th July 2006, 22:33
  5. How to Use Objects
    By raphaelf in forum Newbie
    Replies: 1
    Last Post: 2nd June 2006, 10:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.