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. How to get data from thread into ui.
QtWS25 Last Chance

How to get data from thread into ui.

Scheduled Pinned Locked Moved Unsolved General and Desktop
threadsqthread
7 Posts 4 Posters 1.3k Views
  • 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.
  • A Offline
    A Offline
    aftershocker1
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • A aftershocker1

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • A Offline
        A Offline
        aftershocker1
        wrote on last edited by
        #3

        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?

        jsulmJ 1 Reply Last reply
        0
        • A aftershocker1

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • A Offline
            A Offline
            aftershocker1
            wrote on last edited by
            #5

            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 sps2data

            in 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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • C Offline
                C Offline
                CarloMecan
                wrote on last edited by CarloMecan
                #7
                This post is deleted!
                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