How to get data from thread into ui.
-
Hello,
I have created a thread as shown below.
//create a monitoring thread
QThread monitorThread;//create dataObjewct to move into thread SPS2data sps2data; //setup function that connects signals and slots - when started connects to the DOWork slot sps2data.DoSetup(monitorThread); //takes sps2data object and moves into the monitor thread sps2data.moveToThread(&monitorThread); monitorThread.start();
The thread reads data from usb and I can print this via QDebug to the console. What is the best way to get this data into my application? I looked yesterday at QtConcurrent but was unable to make it work. An idiot guy would be great as i'm am relatively inexperienced and feel a little overwhelmed even reading about this.
Thanks,
G
-
@aftershocker1 said in How to get data from thread into ui.:
What is the best way to get this data into my application?
Using signals/slots. Emit signals from thread and pass the data as signal parameters.
-
I tried this yesterday but the data didn't seem to be getting passed. Is using signals and slots the best(easiest) way to do this?
Any good examples you can point me in the direction of. Watched a couple of videos on it last night and it didn't seem to work although he spoke about QConcurrent. Is the process different from QThread?
-
@aftershocker1 said in How to get data from thread into ui.:
Is using signals and slots the best(easiest) way to do this?
Yes, if it does not work then please show what you tried (show the code).
-
Ok - still not able to get the data passed across.
Main
int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);//create a monitoring thread QThread monitorThread; //create dataObjewct to move into thread SPS2data sps2data; //connect( &sps2data2, SPS2data::on_temp_change, this, &ManuNonEng::get_temp_slot); //setup function that connects signals and slots - when started connects to the DOWork slot sps2data.DoSetup(monitorThread); //takes sps2data object and moves into the monitor thread sps2data.moveToThread(&monitorThread); monitorThread.start(); QApplication a(argc, argv); QtTestTool w; w.show(); return a.exec();
SPS2Data.cpp (the thread collecting the data)
if ((buf[3] == 0x75) && (buf[4] == 0x3f) && (buf[5] == 0x32))
{
/readingType = "// Temperture data //";
tempCount++;/
cpu_temp = (int8_t)buf[8];
acc_temp = (int8_t)buf[10];
adc_temp = (int8_t)buf[9];//print temps out to console qDebug() << " cpu temp(Thread) = " << cpu_temp; qDebug() << " acc temp(Thread) = " << acc_temp; qDebug() << " adc temp(Thread) = " << adc_temp; //emit signal to other thread emit SPS2data::on_temp_change(cpu_temp, acc_temp, adc_temp); //emit working correctly(values have correct values) }
from ManuNonEng.cpp
ManuNonEng::ManuNonEng(QDialog* parent)
: QDialog(parent)
{
ui.setupUi(this);//connect button connect(ui.btn_next, &QPushButton::clicked, this, &ManuNonEng::onclick_btn_next2); //connect to sps2data thread connect(&sps2data2, &SPS2data::on_temp_change, this, &ManuNonEng::get_temp_slot);
}
void ManuNonEng::get_temp_slot(int8_t Qcpu_temp, int8_t Qacc_temp, int8_t Qadc_temp)
{
sps2data2.cpu_temp = Qcpu_temp;
//cpu_temp = Qcpu_temp;
sps2data2.acc_temp = Qacc_temp;
sps2data2.adc_temp = Qadc_temp;
qDebug() << "////// cpu temp in application = " << sps2data2.cpu_temp;
}in manunoeng.h
public slots:
void get_temp_slot(int8_t cpu_temp, int8_t acc_temp, int8_t adc_temp);//three temps from sps2datain sps2data.h
signals:
//void on_temp_change(int8_t cpu_temp, int8_t acc_temp, int8_t adc_temp);
void on_temp_change(int8_t cpu_temp, int8_t acc_temp, int8_t adc_temp);Hope that is understandable and really hope someone can advise. The program is running without error but the data is not being passes from sps2data.cpp to manunoneng.cpp
Thanks
-
Hi,
Please use coding tags for your code, that will make it readable.
You are using several different instance of sps2data that are unrelated to each other.
On a side note, you should always instanciate QXXXApplication before any other QObject based classes.
-
This post is deleted!