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. ending thread at end of app
Forum Updated to NodeBB v4.3 + New Features

ending thread at end of app

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 281 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I've seen some other postings on this, but none are quite focused on my issue.

    Recently, when I close an app by pressing the "X" in the upper right corner of my main widget (this is Window 10), I started getting an error message:

    QThread: Destroyed while thread is still running
    

    If I'm in the debugger, it really throws a shoe, asking me (twice) if I want to debug the process, etc.

    I'm not entirely sure what's going on here. Here's some of my main code:

        QApplication a(argc, argv);
        ModelDevice modelDevices(nullptr);
       QThread thread(&a);
        
        worker = new Worker(&modelDevices);
        worker->moveToThread(&thread);
    
        QObject::connect(&widget, &Widget::quitButtonPushed, &thread, &QThread::exit);
        QObject::connect(&thread, &QThread::finished, &a, &QApplication::quit);
        QObject::connect(&thread, &QThread::finished, worker, &Worker::deleteLater);
    

    I had the idea of having the widget destructor signal the thread to quit:

    // this doesn't compile
    QObject::connect(&widget, &Widget::sendTerminate, &thread, QThread::quit);
    

    But I get a warning about a call to non-static member function without an object argument.

    So:

    1. would this be the right way to do this if I can get that line fixed?
    2. what's wrong with that attempted connection? It seems to me that thread is the object, so I don't understand the error message.

    Thanks...

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      If your QThread is only in main.cpp, the normal way is

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          ...
          int code = a.exec();
          thread.quit();//do something to quit the thread, this depends on how your Worker is working. If it is blocking, you need to find a way to tell it to stop
          thread.wait();//wait the thread to finish
          return code;
      }
      
      
      1 Reply Last reply
      5
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        Beautiful...thanks, Bonnie. (The worker doesn't block; it's really just a bunch of signals and slots connected to the widget.)

        1 Reply Last reply
        1

        • Login

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