Passing Data to a slot in another thread
-
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
forMainWindow.h
@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include"ProcessThread.h"
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
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
-
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#detailsAs 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.
-
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.