Results 1 to 8 of 8

Thread: problem try to trigger a refresh of my Ui (the main thread) from an external QThread

  1. #1
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default problem try to trigger a refresh of my Ui (the main thread) from an external QThread

    Hello,

    i have my main thread (GUI thread) and i have launched a new thread and established a connection between this new thread (send a signal) and the GUI thread (receive message in slot).

    I am trying in the slot of this connection to refresh my UI. For so i do ... qApp->processEvents().

    I can't get that to refresh my UI... only if i do qApp->processEvents() from my GUI thread, i can get the refresh.

    what do i do wrong or what should i do?

    Thanks for your help

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem try to trigger a refresh of my Ui (the main thread) from an external QThr

    Please show how did You create connection.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem try to trigger a refresh of my Ui (the main thread) from an external QThr

    I'm sure your connections are not correct. Can you post the code please?

    And why do you need to call qApp->processEvents()?
    Certainly don't do that from another thread.

  4. #4
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: problem try to trigger a refresh of my Ui (the main thread) from an external QThr

    thank you for your fast answer here is the code in question,
    i use qApp->processEvents ( QEventLoop::ExcludeUserInputEvents ) because usually that process my queue



    from my main GUI thread here is my slot

    Qt Code:
    1. void MainWindow::updateGUI_slot()
    2. {
    3.  
    4. qApp->processEvents ( QEventLoop::ExcludeUserInputEvents );
    5. qApp->flush();
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 





    here is my timerThread, (the one that has been created from the main thread),
    so _parent is my MainWindow pointer

    Qt Code:
    1. timerThread::timerThread (QObject *_parent)
    2. {
    3. parent = _parent;
    4. }
    5.  
    6. timerThread::~timerThread ()
    7. {
    8. delete timer;
    9. }
    10.  
    11.  
    12.  
    13. void timerThread::run ()
    14. {
    15. timer = new QTimer(this);
    16. connect(timer, SIGNAL(timeout()), parent, SLOT(updateGUI_slot()), Qt::DirectConnection );
    17. timer->start(1000);
    18.  
    19. exec();
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem try to trigger a refresh of my Ui (the main thread) from an external QThr

    Problem is with Qt:irectConnection. You can't "play" with GUI thread from another thread. Qt:irectConnection is like ordinary calling of method. Change this connection to Qt::QueuedConnection (it is default for interthreads signals) and all will be working.

  6. #6
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: problem try to trigger a refresh of my Ui (the main thread) from an external QThr

    thanks again,

    well i tried nearly all QueuedConnection and AutoConnection, i have the same behaviour and still not getting the refresh of my UI except if i do it from my main thread so i guess i still have
    updateGUI_slot() in my timerThread don't see why!



    Qt Code:
    1. void timerThread::run ()
    2. {
    3. timer = new QTimer(this);
    4. connect(timer, SIGNAL(timeout()), parent, SLOT(updateGUI_slot()), Qt::QueuedConnection );
    5. timer->start(1000);
    6.  
    7. exec();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem try to trigger a refresh of my Ui (the main thread) from an external QThr

    If You really need call processEvents() to refresh GUI it is something wrong with logic of application. Show us authentic code.
    P.S.
    Create connection in auto mode and add this line to timerThread constructor :
    Qt Code:
    1. moveToThread( this);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lesiok; 6th November 2010 at 07:59.

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: problem try to trigger a refresh of my Ui (the main thread) from an external QThr

    Quote Originally Posted by Lesiok View Post
    add this line to timerThread constructor :
    Qt Code:
    1. moveToThread( this);
    To copy to clipboard, switch view to plain text mode 
    That's not correct.
    Instead move the code of the timerThread to a QObject subclass. Then move that object to a thread.

Similar Threads

  1. Replies: 7
    Last Post: 22nd October 2010, 12:17
  2. QThread sends signal to main thread immediatly
    By BIllNo123 in forum Newbie
    Replies: 7
    Last Post: 27th August 2010, 10:32
  3. QThread Signal Not Received By Main Thread Slot
    By EF2008 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2010, 08:06
  4. Replies: 1
    Last Post: 25th August 2009, 13:26
  5. Determine if QThread::currentThread is main thread?
    By chaoticbob in forum Qt Programming
    Replies: 2
    Last Post: 17th December 2008, 06:26

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.