3

嗨,我在通过线程更新 ui 时遇到问题。代码工作正常,但问题是当我想移动我的窗口时,你知道那一刻 ui 线程将停止更新。我的线程将值发送到导致错误的停止线程。我不知道如何解决这个问题。

这是我的线程代码标题:

#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:

   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));
        }
4

1 回答 1

2

您可能会遇到我们不久前为 5.5 版本修复的这个问题:

窗口调整大小或移动时停止流式传输

如果您愿意,您可以反向移植更改。

更重要的是,当库被设计为具有异步 API 时,自己使用线程是比较奇怪的。那时我写了一个简单的例子,演示了正确异步使用库以用于阅读目的。你可以在这里找到它:

命令行阅读器异步示例

于 2014-12-14T01:48:36.520 回答