Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Passing Data to a slot in another thread

$
0
0
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

Viewing all articles
Browse latest Browse all 13965