【QT 多线程】QRunnable封装SDK耗时接口

一、应用场景

UI控制机械臂运动,发送指令,等待运行完毕,不能卡界面

该指令比较耗时,使用QRunnable放入线程池中运行

二、封装SDK指令

DeviceSDKRunnable.h

#ifndef DEVICESDKRUNNABLE_H
#define DEVICESDKRUNNABLE_H

#include <QObject>
#include <QRunnable>
#include <QVariantList>
#include "czcore_global.h"

enum E_DeviceCommand
{
    MOVE_TO_HOME,            /**< 指令1 */
    CMD_SCAN_DOT,            /**< 指令2 */
    CMD_SCAN_LINE,           /**< 指令3 */
    CMD_SCAN_PLANE,          /**< 指令4 */
};

struct T_DeviceRetValue
{
    int errCode;
    QString errMsg;
    QVariantList params;         /**< 参数 */
    QVariant ret;                /**< 结果 */
    E_DeviceCommand command;     /**< 指令 */
};

class CZCORESHARED_EXPORT DeviceSDKRunnable : public QObject, public QRunnable
{
    Q_OBJECT
public:
    explicit DeviceSDKRunnable(QObject *parent = 0);
    ~DeviceSDKRunnable();
    void run();
    /**
     * @brief setCalledCommandAndParams
     * @param cmd    指令
     * @param param  指令参数
     * @return
     */
    bool setCalledCommandAndParams(const E_DeviceCommand &cmd, const QVariantList &param = QVariantList());

signals:
    void sigCallSDKResult(T_DeviceRetValue tRet);

private:
    E_DeviceCommand m_CalledCommand;
    QVariantList m_CalledParams;    /**< 指令的参数 */
};
#endif // SDKRUNNABLE_H

DeviceSDKRunnable.cpp

#include "DeviceSDKRunnable.h"
#include <QThread>
#include <QDebug>
#include <QTime>


DeviceSDKRunnable::DeviceSDKRunnable(QObject *parent)
    : QObject(parent)
{
    qRegisterMetaType<T_DeviceRetValue>("T_DeviceRetValue");
}

DeviceSDKRunnable::~DeviceSDKRunnable()
{
    qDebug() << __FUNCTION__;
}

void DeviceSDKRunnable::run()
{
    int errCode = 0;
    QVariant runResult;
    CLOG_DEBUG("start DeviceSDKRunnable run" );
    QTime time;
    time.start();
    QThread::msleep(10);
    switch (m_CalledCommand)
    {
        case E_DeviceCommand::CMD_SCAN_DOT:
            {
                //调用自己的接口
                runResult = QVariant::fromValue(tScanDotResult);
            }
            break;
        case E_DeviceCommand::CMD_SCAN_LINE:
            {
                //调用自己的接口
                runResult = QVariant::fromValue(tScanLineResult);
            }
            break;
        default:
            break;
    }
    CLOG_DEBUG("The run time is: {} s", time.elapsed() / 1000.0);
    CLOG_DEBUG("end DeviceSDKRunnable run");
    T_DeviceRetValue tRet;
    tRet.command = m_CalledCommand;
    tRet.errCode = errCode;
    tRet.params = m_CalledParams;
    tRet.ret = runResult;
    emit sigCallSDKResult(tRet);
}

bool DeviceSDKRunnable::setCalledCommandAndParams(const E_DeviceCommand &cmd, const QVariantList &param)
{
    m_CalledCommand = cmd;
    m_CalledParams = param;
    return true;
}

使用示例

QVariantList tParam;
tParam << QVariant::fromValue(model);
tParam << QVariant::fromValue(hardwareSpec);

DeviceSDKRunnable *tDeviceSDKRunnable = new DeviceSDKRunnable();
connect(tDeviceSDKRunnable, SIGNAL(sigCallSDKResult(T_DeviceRetValue)), this,
                        SLOT(slot_HandleDeviceCalledRet(T_DeviceRetValue)), Qt::UniqueConnection);
tDeviceSDKRunnable->setCalledCommandAndParams(E_DeviceCommand::CMD_SCAN_DOT, tParam);
                QThreadPool::globalInstance()->start(tDeviceSDKRunnable);

猜你喜欢

转载自blog.csdn.net/qq_40602000/article/details/122292715