Pass parameters to run() method
-
Hello everyone,
My question is very simple.
I have a Dialog that has one button, one lineEdit and a text label.So what I want to do is to type a number in my lineEdit and when I push the button it starts a thread, and this number which I typed into lineEdit is passed into the thread, do some calculations and returns another number to be showed in the text label.
something like this:
Iniciar is the name of my button.
@void Dialog::on_Iniciar_clicked()
{
mThread->start("here is the number I typed in the lineEdit, for example ui->lineEdit->text().toInt()");
}@and then it would be passed into the thread:
@void MyThread::run("here it receives the number, for example: int number")
{QString a;
a.setNum(b);"here i already have a signal which emits this number to update the GUI, there is no problem here, I just want the b to gets here"
}
@Did I make it clear?
Can someone help me please?
I'm reading documentations for hours and reading other topics but can't make it work.
Thank you in advance
-
Hi,
The fastest way to make it work is to:
Write your int value into a member variable in mThread
Call mThread->start()
Inside run(), read this variable.
Make sure that the read and write operations are "protected by a mutex":http://doc.qt.io/qt-5/threads-synchronizing.html.
Note 1: There are many ways to use threads in Qt. See the "Multithreading Technologies in Qt":http://doc.qt.io/qt-5/threads-technologies.html article.
Note 2: For simple cases like what you described, it is fine to subclass QThread. However, if you want two-way communication with your thread using signals and slots, use the Worker Object technique instead of subclassing QThread. -
Hello JKSH,
Can you wirte an example?
How would I write to read this variable?
If it helps, I'll give you my code:
Dialog.h
@#ifndef DIALOG_H
#define DIALOG_H#include "mythread.h"
#include <QDialog>namespace Ui {
class Dialog;
}class Dialog : public QDialog
{
Q_OBJECTpublic:
explicit Dialog(QWidget *parent = 0);
~Dialog();
MyThread *mThread;private:
Ui::Dialog *ui;public slots:
void onNumberChanged(int , int , int , int, int, int, int);private slots:
void on_Iniciar_clicked();
void on_Iniciar_2_clicked();
};#endif // DIALOG_H@
Dialog.cpp
@#include "dialog.h"
#include "ui_dialog.h"
#include <QString>Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->Iniciar_2->hide();mThread = new MyThread(this); connect(mThread,SIGNAL(NumberChanged(int, int, int, int, int, int,int)),this,SLOT(onNumberChanged(int,int, int, int, int, int,int)));
}
Dialog::~Dialog()
{
delete ui;
}void Dialog::onNumberChanged(int Show_KM, int Show_VMA, int Show_Notch, int Show_VEL, int Show_KM2, int Show_Notch2, int Show_Diferenca)
{
QString a;
a.setNum(Show_KM);
ui->label->setText(a);
"This is the function that will update the GUI window with the parameters"
}void Dialog::on_Iniciar_clicked()
{
"this is where I want to collect the information from a lineEdit which is in the dialog.ui, but want to pass it to the mThread"
mThread->start();
}void Dialog::on_Iniciar_2_clicked()
{
mThread->terminate();
}@mythread.h
@#ifndef MYTHREAD_H
#define MYTHREAD_H
#include "cblsocket.h"#include <QThread>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void run();private:
//CBLSocket cbl;signals:
void NumberChanged(int, int, int, int, int, int, int);public slots:
};
#endif // MYTHREAD_H
@mythread.cpp
@#include "mythread.h"
#include <QtCore>
#include "QTime"
#include "ui_dialog.h"
#include "dialog.h"MyThread::MyThread(QObject *parent) :
QThread(parent)
{
}void MyThread::run()
{"exactly where I want to receive the variable that I collected from the lineEdit which is in the dialog.ui"
"I see two ways of doing this, either I access the information form the Dialog class here, or I pass this value when I start the thread"int a = 0; while(true){ QMutex mutex; mutex.lock(); mutex.unlock(); //qDebug(a); if(a < 1000){ a = a + 1; } else{ a = 0; } emit NumberChanged(a, 2, 3, 4, 5, 6, 7); this->msleep(50); }
}@
Could you give me an example JKSH?
I appreciate you help!
-
Hi MBFernan,
See the "Mandelbrot Example":http://doc.qt.io/qt-5/qtcore-threads-mandelbrot-example.html . The caller doesn't call start() directly. Instead, it calls a custom function (render() in the example). render() accepts the parameters and calls start().
-
Maybe something like this:
mythread.h
@
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include "cblsocket.h"#include <QThread> class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QObject *parent = 0); void run();
// called from dialog before 'start'
void set_data(int in_1,int,int,int,int,int in_6) {d_data_1 = in_1;…d_data_6 = in_6;}private: //CBLSocket cbl;
int d_data_1;
int d_data_2;
int d_data_3;
int d_data_4;
int d_data_5;
int d_data_6;signals: void NumberChanged(int, int, int, int, int, int, int); public slots: }; #endif // MYTHREAD_H
@
mythread.cpp
@
#include "mythread.h"
#include <QtCore>
#include "QTime"
#include "ui_dialog.h"
#include "dialog.h"MyThread::MyThread(QObject *parent) : QThread(parent) { } void MyThread::run() {
// use d_data_1 to d_data_6
int a = 0; while(true){ QMutex mutex; mutex.lock(); mutex.unlock(); //qDebug(a); if(a < 1000){ a = a + 1; } else{ a = 0; } emit NumberChanged(a, 2, 3, 4, 5, 6, 7); this->msleep(50); } }
@
I think there is a bigger problem here then simply getting the data to the thread. If any of the values in the dialog are changed you probably want the output to be updated based on the new input. This won't happen in this case as the thread is created, input is supplied, and it runs endlessly.
-
[quote author="MBFernan" date="1422788707"]
mythread.cpp@
QMutex mutex;
mutex.lock();
mutex.unlock();
@
[/quote]By the way, what's that for?