Results 1 to 3 of 3

Thread: invokeMethod doesn't change the thread

  1. #1
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default invokeMethod doesn't change the thread

    Hi,
    I try to call invokeMethod or send a signal in a typical worker (=QObject) and QThread scenario. But the invokeMethod doesn't change from the worker's thread into the main thread. And the signal has the same problem. My code is as follows:
    Qt Code:
    1. Worker::Worker(Ui::MainWindow *ui, QObject *parent) :
    2. QObject(parent)
    3. {
    4. m_ui = ui;
    5. m_ui->statusBar->showMessage("Worker started.");
    6. connect(this, SIGNAL(sigWriteSomethingInTheGui()), this, SLOT(slotWriteSomethingInTheGui()), Qt::QueuedConnection);
    7. }
    8.  
    9. void Worker::slotDoSomeWork()
    10. {
    11. m_ui->statusBar->showMessage("slotDoSomeWork started.");
    12.  
    13. bool ret = QMetaObject::invokeMethod(this, "slotWriteSomethingInTheGui", Qt::QueuedConnection); //We try to change from worker thread into the main thread.
    14.  
    15. emit sigWriteSomethingInTheGui(); //Here we try to do the same...
    16. }
    17.  
    18. void Worker::slotWriteSomethingInTheGui()
    19. {
    20. m_ui->statusBar->showMessage("slotWriteSomethingInTheGui started."); //If this slot is called we should be in the main thread after calling
    21. //invokeMethod(this, "slotWriteSomethingInTheGui", Qt::QueuedConnection);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. m_pWorker = std::unique_ptr<Worker>( new Worker(ui));
    8.  
    9. m_pWorker->moveToThread(&m_WorkerThread);
    10.  
    11. connect(&m_WorkerThread, SIGNAL(started()), m_pWorker.get(), SLOT(slotDoSomeWork()));
    12. connect(m_pWorker.get(), SIGNAL(sigWriteSomethingInTheGui()), this, SLOT(slotWriteIt()));
    13.  
    14. m_WorkerThread.start();
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20.  
    21. m_WorkerThread.quit();
    22. m_WorkerThread.wait(2000);
    23. }
    24.  
    25. void MainWindow::slotWriteIt()
    26. {
    27. ui->statusBar->showMessage("Yes!"); //Yes. We are in the main thread.
    28. }
    To copy to clipboard, switch view to plain text mode 

    Can someone explain this behaviour?

    Thanks,

    The Lamb

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: invokeMethod doesn't change the thread

    Your Worker object was moved into the worker thread, its slot.
    Slots, when invoked through Qt:QueuedConnection will be executed in the context of the object's owner thread.
    In your case the worker thread.

    Connect the signal to some object belonging to the main thread, or invoke a slot of such an object. That's why it works for MainWindow::slotWriteIt()

    Btw, your slotDoSomeWork(), which is supposed to run in the worker thread, access UI directly. This is bad. Only the main thread is allowed to work with UI objects.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: invokeMethod doesn't change the thread

    Thanks,
    now I understand the moveToThread method more in details. The whole object (Worker object) lives in this thread. Even if the connect
    Qt Code:
    1. connect(this, SIGNAL(sigWriteSomethingInTheGui()), this, SLOT(slotWriteSomethingInTheGui()), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    was applied in the main thread. After calling moveToThread there is no chance for the worker class to switch into the main thread by itself.
    Greetings,

    The Lamb

Similar Threads

  1. Replies: 3
    Last Post: 21st June 2014, 14:49
  2. Worker thread doesn't seem to start event loop?
    By Data42 in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2013, 17:33
  3. Change GUI from server thread
    By zuperman in forum Newbie
    Replies: 2
    Last Post: 23rd January 2011, 19:09
  4. QToolButton - doesn't change icons when pressed
    By walsha3000 in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2010, 10:03
  5. Using QMetaObject::invokeMethod() doesn't work
    By bruccutler in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2007, 22:40

Tags for this Thread

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.