c++ - qt-updating ui by thread -


hi have problem updating ui thread. code works correctly problem when want move window know in moment ui thread stop updating. , thread sends values stopped thread causes error. don't know how fix this.

here thread code header:

#ifndef readerthread_h #define readerthread_h #include <qtserialport/qserialport> #include <qtserialport/qserialportinfo> #include <qthread>  class readerthread : public qthread {     q_object public:     explicit readerthread(qobject *parent = 0);     void run();     bool stop = false;     qbytearray port_input;     qbytearray payload;     quint8 starter_symbol = 0;     quint8 message_length = 0;     quint8 message_id = 0;     readerthread *thread; signals:     void updated(qbytearray , quint8);  private:     qserialport *serial;  };  #endif // readerthread_h 

my thread .cpp:

#include "readerthread.h" #include <qtcore>  readerthread::readerthread(qobject *parent) :     qthread(parent) {      serial = new qserialport(this);      foreach (const qserialportinfo &serialportinfo, qserialportinfo::availableports())     serial->setportname(serialportinfo.portname());      serial->setbaudrate(qserialport::baud115200);      serial->setdatabits(qserialport::data8);      serial->setparity(qserialport::noparity);      serial->setflowcontrol(qserialport::noflowcontrol);      serial->setstopbits(qserialport::onestop);  //    serial->setreadbuffersize(8192);      serial->open(qiodevice::readonly);      serial->errorstring();  }  void readerthread::run() {     while(serial->isopen())     {         port_input.append(serial->readall());         if(port_input.count() >= 150)         {            starter_symbol = port_input.indexof(254);            if((port_input.at(starter_symbol + 3) == 01) && (port_input.at(starter_symbol + 4) == 01))            {               message_length = port_input.at(starter_symbol + 1);               message_id = port_input.at(starter_symbol + 5);               payload = port_input.mid(starter_symbol + 6 , message_length);               port_input.remove(starter_symbol , message_length + 8);               emit updated(payload , message_id);            }            port_input.remove(0 , starter_symbol);         }     } } 

and here mainwindow.cpp in short :

   struct mavlink_attitude_t         {             /// <summary> timestamp (milliseconds since system boot) </summary>               quint32 time_boot_ms;                 /// <summary> roll angle (rad, -pi..+pi) </summary>               float roll;                 /// <summary> pitch angle (rad, -pi..+pi) </summary>               float pitch;                 /// <summary> yaw angle (rad, -pi..+pi) </summary>               float yaw;                 /// <summary> roll angular speed (rad/s) </summary>               float rollspeed;                 /// <summary> pitch angular speed (rad/s) </summary>               float pitchspeed;                 /// <summary> yaw angular speed (rad/s) </summary>               float yawspeed;          };      mainwindow::mainwindow(qwidget *parent) :         qmainwindow(parent),         ui(new ui::mainwindow)     {         ui->setupui(this);         thread = new readerthread(this);         connect(thread , signal(updated(qbytearray,quint8)) , , slot(onupdate(qbytearray,quint8)));         thread->start();      }          void mainwindow::onupdate(qbytearray payload , quint8 id)         {                mavlink_attitude_t data;                memcpy(&data,payload.data(),sizeof(mavlink_attitude_t));                ui->timebootms->settext(qstring::number(data.time_boot_ms));                ui->roll->settext(qstring::number(data.roll));                ui->pitch->settext(qstring::number(data.pitch));                ui->yaw->settext(qstring::number(data.yaw));                ui->rollspeed->settext(qstring::number(data.rollspeed));                ui->pitchspeed->settext(qstring::number(data.pitchspeed));                ui->yawspeed->settext(qstring::number(data.yawspeed));         } 

you hitting issue fixed while ago 5.5 release:

stopping streaming when window resizing or moving

you can backport change if wish.

more importantly, relatively strange use threading on own when library designed have async api. wrote simple example demonstrates proper async use of library reading purposes. here can find it:

command line reader async example


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -