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. How to run a function in another thread
QtWS25 Last Chance

How to run a function in another thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
qthread
2 Posts 2 Posters 305 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.
  • D Offline
    D Offline
    Disco
    wrote on last edited by
    #1

    I tried to run a function in the event loop of another thread by clicking a QPushButton but failed.

    The sources are as follows:

    // main.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "workthread.h"
    
    #include <QApplication>
    #include <QObject>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        WorkThread t;
        QObject::connect(w.ui->pushButton, &QPushButton::clicked, &t, &WorkThread::printThreadId, Qt::QueuedConnection);
        t.start();
    
        return a.exec();
    }
    

    // mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    // mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QDebug>
    
    #include "workthread.h"
    
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        qDebug() << "MainWindow thread:" << QThread::currentThreadId();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    // workthread.h
    #ifndef WORKTHREAD_H
    #define WORKTHREAD_H
    
    #include <QThread>
    
    class MainWindow;
    
    class WorkThread : public QThread
    {
        Q_OBJECT
    
    public:
        WorkThread();
    
    public slots:
        void printThreadId();
    
    protected:
        void run();
    };
    
    #endif // WORKTHREAD_H
    

    // workthread.cpp
    #include "workthread.h"
    
    #include <QDebug>
    
    WorkThread::WorkThread()
    {}
    
    void WorkThread::printThreadId()
    {
        qDebug() << "WorkThread slot thread:" << QThread::currentThreadId();
    }
    
    void WorkThread::run()
    {
        qDebug() << "WorkThread thread:" << QThread::currentThreadId();
    
        exec();
    }
    

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>400</width>
        <height>300</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <widget class="QPushButton" name="pushButton">
        <property name="geometry">
         <rect>
          <x>150</x>
          <y>110</y>
          <width>99</width>
          <height>27</height>
         </rect>
        </property>
        <property name="text">
         <string>PushButton</string>
        </property>
       </widget>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    

    The output of the program is like:

    MainWindow thread: 0x7ff21ff9d780
    WorkThread thread: 0x7ff1e7fff700
    WorkThread slot thread: 0x7ff21ff9d780
    WorkThread slot thread: 0x7ff21ff9d780

    from which we can conclude that the method invoked by the clicked signal of pushButton is run in the main thread.

    So how can I run this function in my workthread after pressing the button?

    J.HilkJ 1 Reply Last reply
    0
    • D Disco

      I tried to run a function in the event loop of another thread by clicking a QPushButton but failed.

      The sources are as follows:

      // main.cpp
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "workthread.h"
      
      #include <QApplication>
      #include <QObject>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          WorkThread t;
          QObject::connect(w.ui->pushButton, &QPushButton::clicked, &t, &WorkThread::printThreadId, Qt::QueuedConnection);
          t.start();
      
          return a.exec();
      }
      

      // mainwindow.h
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
          Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      

      // mainwindow.cpp
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      #include <QDebug>
      
      #include "workthread.h"
      
      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          qDebug() << "MainWindow thread:" << QThread::currentThreadId();
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      

      // workthread.h
      #ifndef WORKTHREAD_H
      #define WORKTHREAD_H
      
      #include <QThread>
      
      class MainWindow;
      
      class WorkThread : public QThread
      {
          Q_OBJECT
      
      public:
          WorkThread();
      
      public slots:
          void printThreadId();
      
      protected:
          void run();
      };
      
      #endif // WORKTHREAD_H
      

      // workthread.cpp
      #include "workthread.h"
      
      #include <QDebug>
      
      WorkThread::WorkThread()
      {}
      
      void WorkThread::printThreadId()
      {
          qDebug() << "WorkThread slot thread:" << QThread::currentThreadId();
      }
      
      void WorkThread::run()
      {
          qDebug() << "WorkThread thread:" << QThread::currentThreadId();
      
          exec();
      }
      

      mainwindow.ui

      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
       <class>MainWindow</class>
       <widget class="QMainWindow" name="MainWindow">
        <property name="geometry">
         <rect>
          <x>0</x>
          <y>0</y>
          <width>400</width>
          <height>300</height>
         </rect>
        </property>
        <property name="windowTitle">
         <string>MainWindow</string>
        </property>
        <widget class="QWidget" name="centralWidget">
         <widget class="QPushButton" name="pushButton">
          <property name="geometry">
           <rect>
            <x>150</x>
            <y>110</y>
            <width>99</width>
            <height>27</height>
           </rect>
          </property>
          <property name="text">
           <string>PushButton</string>
          </property>
         </widget>
        </widget>
       </widget>
       <layoutdefault spacing="6" margin="11"/>
       <resources/>
       <connections/>
      </ui>
      

      The output of the program is like:

      MainWindow thread: 0x7ff21ff9d780
      WorkThread thread: 0x7ff1e7fff700
      WorkThread slot thread: 0x7ff21ff9d780
      WorkThread slot thread: 0x7ff21ff9d780

      from which we can conclude that the method invoked by the clicked signal of pushButton is run in the main thread.

      So how can I run this function in my workthread after pressing the button?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Disco thats what you get for choosing the subclassing thread approach

      Anyway I leave this repo link here:

      https://github.com/DeiVadder/QtThreadExample

      I would almost always suggest the worker approach, but if it's a fire and forget function you want to run in parallel, QThread::create it probably the way to go.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2

      • Login

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