Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread is blocking UI-Thread
Forum Updated to NodeBB v4.3 + New Features

QThread is blocking UI-Thread

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sw0ce
    wrote on last edited by
    #1

    Hi,

    I think a have a Little missunderstanding in using QThread. I have a WorkerThread that is doing some intense work and is sending a refresh-signal to the MainThread from time to time.

    @

    ////////////////////// MAINTHREAD //////////////////////
    void MainThread::StartWorkerThread()
    {
    QThread t;
    bool retVal = connect(&t, SIGNAL(SigRefreshUI()), this, SLOT(Refresh()), Qt::DirectConnection);
    bool retVal2 = connect(&t, SIGNAL(SigSendAsyncTCP(string)), this, SLOT(SendAsnyTCP(string)), Qt::DirectConnection);

    t.start();
    

    }

    void MainThread::GettingTCPAnswer(string answer)
    {
    emit SigSendAsyncTCP(answer);
    }

    ////////////////////// WORKERTHREAD //////////////////////
    void WorkerThread::run()
    {
    bool retVal3 = connect((MainThread*)m_mainThread, SIGNAL(SigTCPAnswer(string)), this, SLOT(TCPAnswer(string)), Qt::DirectConnection);

     doSomeWork....
    
     emit RefreshUI();
    
     doSomeWork....
    
     emit RefreshUI();
    
     doSomeWork....  
    
     emit SigSendAsyncTCP("Somedata");
    

    }

    void WorkerThread::TCPAnswer(string answer)
    {
    doSomeWork....

     emit RefreshUI();
    
     doSomeWork....
    
     emit RefreshUI();
    

    }
    @

    Everthing is fine so far. In the run method the UI is refreshed instantly after the signal "RefreshUI" is emited. After sending the TCPData through the MainThread and getting the answer the slot "TCPAnswer" is called. This time the WorkerThread is blocking the MainThread. The UI is refreshed after leaving the scope of "TCPAnswer" and not everytime "RefrehUI" is emited.

    Could someone explain me what I´m doing wrong?

    Thanks and regards

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Hi,
      This:@Thread t@ doesn't really start a new thread! It only creates an object of the type thread. You need to move that object to an other thread by moveToThread();
      It would be advisable to keep the thread pointer into your member variables to not loos control off it.
      In your debugger you should be able to see the added thread if you are successful in it.

      Greetz, Jeroen

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved