c++ - qt-通过线程更新用户界面

标签 c++ multithreading qt qtserialport qtwidgets

您好,我在通过线程更新 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));
        }

最佳答案

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

Stopping streaming when window is resizing or moving

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

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

Command Line Reader Async Example

关于c++ - qt-通过线程更新用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26906413/

相关文章:

java - 设计问题 - 应该为 GUI 使用单独的线程吗?

c++ - QListWidget 无法动态添加项目

c++ - 向导中的必填字段

c++ - 从字母数字 QString 中提取数字

java - 原生 hadoop 库程序集

c++ - 我可以使用动态构建的比较器创建 map 吗?

c++ - 如何使我的 OpenGL 视频具有与 QQuickItem 相同的大小和位置?

c++ - QtCreator : How to make builderrors from cmake->ninja show in issues panel in "Makefile project"

java - Java-8 的 DoubleStream.sum() 方法在并行运行时是否稳定?

c - 如何使用 PTRACE 获得多线程的一致 View ?