Results 1 to 18 of 18

Thread: Non-Gui thread blocking gui

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Non-Gui thread blocking gui

    It may behave incorrectly because you are emitting the signal to the thread object. And the thread object lives in the same thread as the calling object (only objects created within run() or from within methods called from the thread's event loop live in the worker thread), so the slot might be called by the wrong (main) thread. It could be possible that all you need to do is to wait until the thread is started before emitting the signal (but you have to explicitely use queued connections for that to work!) - that would explain why subsequent signals work correctly.

    Qt Code:
    1. connections.at( i )->start( QThread::NormalPriority );
    2. while(!connections.at(i)->isRunning()) QThread::sleep(100); //crude, but should work
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Non-Gui thread blocking gui

    Would the custom event run in the worker thread? I'd prefer to do it that way if it is so, I'm busy restructuring the program now to make it cleaner and fix bugs so I don't want to hack some half-assed fix in if at all possible

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Non-Gui thread blocking gui

    Quote Originally Posted by Valheru View Post
    Would the custom event run in the worker thread?
    Yes, but with the same condition - make sure the thread is already running its event queue (as this probably moves the thread object to the thread itself) before posting the first event.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Non-Gui thread blocking gui

    In Qt 4, a slot in a QThread subclass ends up being executed in the thread where the QThread object itself is living in (QObject lives in the thread where it was created).

    A signal and slot connection is direct between objects living in the same thread. So if a QThread object is instantiated in the main GUI thread, the QThread object itself also lives in the main GUI thread. And so the connection is direct and the slot in a QThread subclass ends up being executed in the main GUI thread aswell.

    You can avoid all this by simply creating a QObject in QThread::run() and connecting signals to that object instead of the QThread object. This ensures that the signal slot connection is across thread.
    Attached Files Attached Files
    J-P Nurmi

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Non-Gui thread blocking gui

    The behaviour is exactly the same with posting events. The event gets processed in the event loop running in the thread where the receiver object lives in. Since the QThread object lives in the main thread, it's customEvent() gets executed in the main thread as well.
    Attached Files Attached Files
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Non-Gui thread blocking gui

    Quote Originally Posted by Valheru View Post
    Hmm, it doesn't seem to like being passed an int as argument for the creator of QEvent. Would it be safe to statically cast it to QEvent::Type?
    Yes.

    Quote Originally Posted by jpn View Post
    The behaviour is exactly the same with posting events. The event gets processed in the event loop running in the thread where the receiver object lives in. Since the QThread object lives in the main thread, it's customEvent() gets executed in the main thread as well.
    Hmm... I thought exec() moves the object into the thread. If it doesn't, it should be enough to move it inside run():

    Qt Code:
    1. void MyThread::run(){
    2. moveToThread(this);
    3. exec();
    4. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Non-Gui thread blocking gui

    Quote Originally Posted by wysota View Post
    Hmm... I thought exec() moves the object into the thread. If it doesn't, it should be enough to move it inside run():

    Qt Code:
    1. void MyThread::run(){
    2. moveToThread(this);
    3. exec();
    4. }
    To copy to clipboard, switch view to plain text mode 
    Good idea, but seems to be not possible:
    QObject::moveToThread: Current thread (003E7CA0) is not the object's thread (0012FEC4).
    Cannot move to target thread (0012FEC4)
    I tested with both
    Qt Code:
    1. moveToThread(this);
    2. // and
    3. moveToThread(QThread::currentThread());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. #8
    Join Date
    Feb 2010
    Posts
    3

    Default Re: Non-Gui thread blocking gui

    Quote Originally Posted by jpn View Post
    Good idea, but seems to be not possible:


    I tested with both
    Qt Code:
    1. moveToThread(this);
    2. // and
    3. moveToThread(QThread::currentThread());
    To copy to clipboard, switch view to plain text mode 
    I had the same problem, but when you call "moveToThread" in the class' constructor (which is executed in the thread the object is created in), it seems to work.

  9. #9
    Join Date
    Aug 2006
    Posts
    163
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 5 Times in 4 Posts

    Default Re: Non-Gui thread blocking gui

    Hmm, it doesn't seem to like being passed an int as argument for the creator of QEvent. Would it be safe to statically cast it to QEvent::Type?

    Qt Code:
    1. connection.h:35: error: invalid conversion from ‘int’ to ‘QEvent::Type’
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  2. Replies: 11
    Last Post: 7th July 2006, 13:09
  3. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44
  4. Problem building Qt4.1.0 with thread support on windows XP
    By pavithra in forum Installation and Deployment
    Replies: 1
    Last Post: 1st April 2006, 11:35
  5. Replies: 2
    Last Post: 6th January 2006, 21:15

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
  •  
Qt is a trademark of The Qt Company.