Qt update ui on stream
hi I have a problem updating ui with a stream. the code is working correctly, but the problem is when I want to move my window, as you know, at this point the thread of the thread will stop updating. and my thread is posting values ββto the stopped thread which is throwing an error. I don't know how to fix this.
here is my 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 .cpp stream:
#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's my 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)) , this , 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 are likely to run into this problem, which we fixed recently for the 5.5 release:
Stop streaming on window change or move
You can back up your changes if you like.
More importantly, it is relatively odd to use threads on your own when the library is for an asynchronous API. I wrote a simple example that demonstrates the correct asynchronous use of the read library. You can find it here:
source to share