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: what am I missing here?

QThread: what am I missing here?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 743 Views
  • 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 mzimmers
    #1

    Hi all -

    I thought I had a handle on using QThreads, but this minimal example doesn't work as I expect:

    #include "widget.h"
    #include "worker.h"
    
    #include <QApplication>
    #include <QDebug>
    #include <QThread>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget *widget;
        Worker *worker;
        QThread *thread(nullptr);
        int rc;
    
        widget = new Widget();
        worker = new Worker();
        thread = new QThread(nullptr);
    
        QObject::connect(worker, &Worker::askForMessageBox, widget, &Widget::displayMessageBox);
    
        widget->show();
        worker->moveToThread(thread);
        thread->start();
    
        rc = a.exec();
        qDebug() << rc;
        return rc;
    }
    
    #include <QDebug>
    #include "worker.h"
    
    Worker::Worker(QObject *parent) : QObject(parent)
    {
    }
    void Worker::start()
    {
        emit askForMessageBox();
    }
    

    When I run this in the debugger, my start() never gets called. What am I missing?

    Thanks...

    EDIT:

    I was missing this:

        QObject::connect(thread, &QThread::started, worker, &Worker::start);
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Side-note: you are leaking every single object in this example

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        It's probably not clear from the example, but (one instance each of) thread, widget and worker all need to exist for the life of the process.

        JonBJ 1 Reply Last reply
        0
        • mzimmersM mzimmers

          It's probably not clear from the example, but (one instance each of) thread, widget and worker all need to exist for the life of the process.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @mzimmers
          Up to you, but you're "supposed" to delete them just before exit. Makes using valgrind a lot easier when you want to detect other leaks in the future :)

          Separate point: for this particular code you could put them all on stack instead of heap, then it wouldn't be an issue.

          1 Reply Last reply
          4
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Don't let the OS cleanup after you, it's rude, it works hard already and is never appreciated. For once you could pick up your own memory instead of always locking yourself in your room listening to 8bit music at the maximum volume possible!

            Give a parent to the objects in the main thread (widget and thread) and QObject::connect(thread, &QThread::finished, worker, &Worker::deleteLater);

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            5
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #6

              "But Ma...it's not even a school night! Geez..."

              But, you're right of course about picking up memory. You don't want to step on those bits barefoot. The 0s are OK, but the 1s are kind of sharp.

              What does one ordinarily use as a parent for top level objects like my widget and thread? I guess I could use the worker, since if I use your connect example, I know it will be destroyed, but I'm guessing there's a better alternative?

              jsulmJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

                "But Ma...it's not even a school night! Geez..."

                But, you're right of course about picking up memory. You don't want to step on those bits barefoot. The 0s are OK, but the 1s are kind of sharp.

                What does one ordinarily use as a parent for top level objects like my widget and thread? I guess I could use the worker, since if I use your connect example, I know it will be destroyed, but I'm guessing there's a better alternative?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mzimmers Why do you use heap allocation in main()? Simply allocate on the stack then you do not have to care about leaks.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  As mentioned the top level usually is stack-allocated, alternatively you can use the QApplication object as parent

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  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