简体   繁体   English

通过线程更新ui

[英]qt-updating ui by thread

hi I have a problem with updating ui by a thread. 嗨我有一个线程更新ui的问题。 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. 代码工作正常,但问题是当我想移动我的窗口,因为你知道在那一刻ui线程将停止更新。 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

my thread .cpp: 我的主题.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 my mainwindow.cpp in short : 在这里我的mainwindow.cpp简而言之:

   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 hitting this issue that we fixed a while ago for the 5.5 release: 您可能会遇到我们在5.5版本之前修复过的问题:

Stopping streaming when window is resizing or moving 当窗口调整大小或移动时停止流式传输

You can backport the change if you wish. 如果您愿意,可以向后移动更改。

More importantly, it is relatively strange to use threading on your own when the library is designed to have an async API. 更重要的是,当库被设计为具有异步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: 你在这里找到它:

Command Line Reader Async Example 命令行读取器异步示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM