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. Passing Data to a slot in another thread
Qt 6.11 is out! See what's new in the release blog

Passing Data to a slot in another thread

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.2k 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.
  • B Offline
    B Offline
    badzie
    wrote on last edited by
    #1

    Dear all,

    I am a beginner in Qt. In my GUI, I'm getting the values of 3 temperatures in spinboxes and I want these values to be transmitted to another thread on button click. So here is my code
    for

    MainWindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include"ProcessThread.h"
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();
    

    signals:
    void SendSignal(unsigned int temp_1,unsigned int temp_2,unsigned int temp_3);

    private slots:

    void Get_From_SpinBox(void);

    private:
    Ui::MainWindow *ui;
    ProcessThread p;
    };

    #endif // MAINWINDOW_H
    @
    MainWindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QDebug>
    #include<QDir>
    #include<QFile>
    #include<QString>
    #include "ProcessThread.h"
    #include<QLabel>
    #include<QThread>
    #include<QObject>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QThread *prthread=new QThread();
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(Get_From_SpinBox()));
    connect(this,SIGNAL(MainWindow::SendSignal(int,int,int)),prthread,SLOT(Store_Temperatures(int ,int ,int )),Qt::QueuedConnection);
    prthread->start();
    }

    MainWindow::~MainWindow()
    {

    p.exit();
    p.wait();

    }

    void MainWindow::Get_From_SpinBox()
    {
    temp_1=ui->spinBox->value();
    temp_2=ui->spinBox_2->value();
    temp_3=ui->spinBox_3->value();
    emit SendSignal(temp_1,temp_2,temp_3);

    }

    @
    The following is my ProcessThread.h
    @
    #ifndef PROCESSTHREAD_H
    #define PROCESSTHREAD_H

    #include<QtGui>
    #include<QThread>
    #include<QString>
    #include<QObject>
    class ProcessThread : public QThread
    {
    Q_OBJECT
    public:
    explicit ProcessThread(QObject *parent = 0);
    void run();

    signals:

    public slots:
    void WriteFileToUSB();
    void GetValues();
    void Store_Temperatures( int temp1, int temp2, int temp3);
    private:

    };

    #endif // MYTHREAD_H
    @
    and finally, my ProcessThread.cpp
    @

    #include "ProcessThread.h"
    #include<QtCore>
    #include "ui_mainwindow.h"
    #include<QDebug>
    #include"mainwindow.h"
    #include<QDebug>

    void ProcessThread::run()
    {
    qDebug() << "hello from worker thread " << thread()->currentThreadId();
    exec();
    }

    ProcessThread::ProcessThread(QObject *parent ):QThread(parent){
    moveToThread(this);
    }

    void ProcessThread::Store_Temperatures( int temp1, int temp2, int temp3)
    {
    qDebug()<<temp1;
    qDebug()<<temp2;
    qDebug()<<temp3;
    }

    @

    However, my slot in my thead Store_Temperatures is not getting executed. Is this is a proper way of passing value to a thread?

    I tried using the suggestions given in

    http://qt-project.org/forums/viewthread/23887

    Yet to resolve. Kindly help

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Hi,

      First of all, the way you are using threads is NOT the way recommended by Qt.

      Have a look at the notes at the end of this page:
      http://qt-project.org/doc/qt-4.8/qthread.html#details

      As for your code. I see that you have a ProcessThread p as member of MainWindow. However in the MainWindow constructor you never start this thread. Instead you are creating a QThread *pthread = new QThread. ProcessThread p is never started or connected to.

      There should be error warnings as you run this program because QThread does not have a Store_Temperatures slot.
      *More importantly pthread is never deleted, so you're leaking memory.

      Hope this helps.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        badzie
        wrote on last edited by
        #3

        Thanks for the reply.

        I made the correction in my code making use of ProcessThread p which is a member of mainwindow.
        @
        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(Get_From_SpinBox()));
        connect(this,SIGNAL(MainWindow::SendSignal(int,int,int)),&p,SLOT(Store_Temperatures(int,int,int)),Qt::QueuedConnection);
        p.start();

        }
        @

        But Store_Temperatures slot is present in ProcessThread.h and I'm not getting any errors or warnings. Only that my Store_Temperatures slot is not running.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          badzie
          wrote on last edited by
          #4

          I solved the problem. The problem was with my connect, where I wrote as MainWindow::SendSignal, instead I made as SendSignal(int,int,int) and it worked. Thanks!

          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