qt更新ui通过线程 [英] qt-updating ui by thread
问题描述
hi我有一个线程更新ui的问题。
代码工作正常,但问题是当我想移动我的窗口,因为你知道在那一刻ui线程将停止更新。和我的线程发送值到停止的线程,导致错误。
我不知道如何解决这个问题。
hi I have a problem with updating ui by a thread. code works correctly but the problem is when I want to move my window as you know in that moment ui thread will stop updating. and my thread sends values to stopped thread that causes 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
我的主题.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);
}
}
}
这里我的mainwindow.cpp
and here 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));
}
推荐答案
我们在前面修正了5.5版本:
You are likely hitting this issue that we fixed a while ago for the 5.5 release:
如果您愿意,可以撤消更改。
You can backport the change if you wish.
更重要的是,当库设计为具有异步API时,使用自己的线程是相对奇怪的。我写了一个简单的例子,然后演示了正确的异步使用库的读取目的。您可以在这里找到它:
More importantly, it is relatively strange to use threading on your own when the library is designed to have an async API. I wrote a simple example back then that demonstrates the proper async use of the library for reading purposes. Here can you find it:
这篇关于qt更新ui通过线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!