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. Understanding how to start a Qthread

Understanding how to start a Qthread

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 4.3k 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.
  • weedyW Offline
    weedyW Offline
    weedy
    wrote on last edited by
    #1

    Hi,
    Just started using QT from last week. I am trying to start a thread and verify if its working by incrementing a variable. But, the thread is not starting, Not able to identify what is the flaw.
    Here is the .h file of mythread class

    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    #include <QObject>
    #include<QThread>
    #include"mainwindow.h"
    
    class mythread:public QObject
    {
        Q_OBJECT
    public:
        explicit mythread(QObject *parent = 0);
        ~mythread();
    private:
    int count;
    public slots:
        void counter();
    signals:
        void finished(int i);
    
    };
    
    #endif // MYTHREAD_H
    

    Here is the mythread.cpp

    #include "mythread.h"
    #include"mainwindow.h"
    
    
    
    mythread::mythread(QObject *parent) : QObject(parent)
    {
        count=0x00;
    }
    
    mythread::~mythread()
    {
    
    }
    
    void mythread::counter()
    {
        count++;
        emit finished(count);
    
    }
    

    Here is .h file of mainwindow

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include<mythread.h>
    #include<QThread>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    public slots:
        void updatecount(int dat);
    
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    And here is mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QThread>
    #include"mythread.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QThread *thread = new QThread;
        mythread *worker = new mythread();
        worker->moveToThread(thread);
        connect(thread,SIGNAL(started()),worker,SLOT(counter()));
        connect(worker,SIGNAL(finished(int)),this,SLOT(updatecount(int)));
        thread->start();
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::updatecount(int dat)
    {
        ui->lcdNumber->display(dat);
    }
    
    

    The logic is, every time the count variable is incremented, a signal containing the value of the count variable is emitted. This signal is caught by the slot updatecount and displayed on the lcd. Since, its a thread the count variable should continuously increment and display the counts. But this is not happening. I don't get what I am doing wrong, any support regarding this would be helpful.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      @weedy said:

      Since, its a thread the count variable should continuously increment and display the counts

      That's a weird assumption. A thread is not automatically a loop. What happens is:

      • Your thread starts
      • counter() is called in it
      • the thread count is increased (to 1) and finished(1) is emitted
      • updatecount(1) is called and 1 goes to the display
        That's it.
        At this point you've got your usual main thread and a second thread with an event loop that does nothing.
        There's no loop that calls counter() anywhere in your program.

      Btw. your thread and worker objects are never destroyed so they leak memory. Also, you don't have any condition for the worker thread to exit event loop so it will spin empty in the background and print a warning at the app exit.

      1 Reply Last reply
      1
      • weedyW Offline
        weedyW Offline
        weedy
        wrote on last edited by
        #3

        That's exactly what happened. After reading your answer and a little bit about threads, it cleared the
        ambiguity. I had assumed that these functions are called repeatedly once the thread starts. I did not get the last part about worker thread exiting the event loop. Could you please elaborate a little on that?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          Qt works with so-called event loops.
          There's usually one in your main() started with a.exec();. this method runs a loop in which it processes and dispatches incoming events, signals etc. By default it exits when last window is closed (see docs for details).
          QThread::start() does basically the same as a.exec(), just in another thread. It starts a loop that processes events and signals of objects in that thread. The difference is that this event loop doesn't quit when last window is closed. You need to manually exit that loop with either exit or quit. Otherwise it will run until your app terminates, but that's not a clean exit. You should quit the loop yourself which will in turn terminate the thread.

          1 Reply Last reply
          0
          • weedyW Offline
            weedyW Offline
            weedy
            wrote on last edited by
            #5

            That clears it. Thanks!

            1 Reply Last reply
            0
            • weedyW Offline
              weedyW Offline
              weedy
              wrote on last edited by
              #6

              How do I mark this post solved?.

              the_T 1 Reply Last reply
              0
              • weedyW weedy

                How do I mark this post solved?.

                the_T Offline
                the_T Offline
                the_
                wrote on last edited by
                #7

                @weedy
                @Wieland wrote this one some time ago :)
                http://forum.qt.io/topic/62700/hitchhiker-s-visual-guide-to-the-qt-forum

                -- No support in PM --

                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