0
点赞
收藏
分享

微信扫一扫

全量知识系统 程序详细设计之“ AI操作系统” (百度搜索的Q&A)

小月亮06 04-03 06:00 阅读 1

QT(6.5) cmake构建C++编程,多线程

1. 先写一个包含要被其他线程调用的函数且继承了QObject的类

#ifndef MY_FUNCTIONS_H
#define MY_FUNCTIONS_H
#include <QObject>
#include <QQmlApplicationEngine>

class My_Functions : public QObject
{
    Q_OBJECT //这个宏要写上
public:
    explicit My_Functions(QObject *parent = nullptr);
    ~My_Functions();

    Q_INVOKABLE bool detectImages(QString  filesPaths);
    void operateFunction(QString mode);

signals:
    void resultReady(QString result); //线程完成工作时发送的信号
private:


};
#endif // MY_FUNCTIONS_H
#include "My_Functions.h"
#include <QDebug>

My_Functions::My_Functions(QObject *parent) : QObject(parent){}
My_Functions:: ~My_Functions(){}

bool My_Functions :: detectImages(QString  filesPaths){
    qDebug()<< "检测图片的函数\n";
    return true;
}

void My_Functions :: operateFunction(QString mode){
    qDebug()<<"收到执行信号:" << mode << "\n";
    qDebug()<<"当前线程号:"<< QThread::currentThreadId() << "\n";  //线程号为第二线程号
    //发送执行信号给Function_Thread执行handleResults方法,参数也传给handleResults
    emit resultReady(mode);  //emit表示发送信号
}

2. 再写一个调用函数类的继承了QObject的类

#ifndef FUNCTION_THREAD_H
#define FUNCTION_THREAD_H

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QQmlApplicationEngine>
#include <my_functions.h>

class Function_Thread : public QObject
{
    Q_OBJECT
public:
    explicit Function_Thread(QObject *parent = nullptr);
    Q_INVOKABLE void startOperateFunction(QString mode);
    ~Function_Thread();
    void handleResults(QString result); //处理线程执行的结果

signals:
    void operate(QString mode); //发送信号触发线程
private:
    QThread* workerThread = NULL;
    My_Functions* my_functions = NULL;
};

#endif // FUNCTION_THREAD_H
#include "function_thread.h"
#include <QQmlContext>
#include <QQmlProperty>
#include <QQmlComponent>

Function_Thread :: Function_Thread(QObject *parent): QObject{parent}{}
Function_Thread :: ~Function_Thread(){}

void Function_Thread :: startOperateFunction(QString mode){
    if(workerThread != NULL && workerThread->isRunning()){
        workerThread->quit();  // 请求线程停止
        workerThread->wait();  // 等待线程彻底结束
        workerThread = NULL;
        my_functions = NULL;
        qDebug()<<"已关闭第二线程\n";
    }
    if(workerThread == NULL || !workerThread->isRunning()){
        workerThread = new QThread;
        my_functions = new My_Functions;
        qDebug()<< "新线程已创建\n";
    }
    my_functions->moveToThread(workerThread);  //调用moveToThread将该任务交给workThread

    //连接类1和类2的方法,当类1的方法发送信号则执行类2的方法,对应参数在发送信号时填入参数列表
    //this为当前实体类,&Function_Thread :: operate为当前类的operate函数地址
    //my_functions为需要调用的函数所在的实体类, &My_Functions :: operateFunction为其函数地址
    connect(this, &Function_Thread :: operate, my_functions, &My_Functions :: operateFunction);  
    connect(workerThread, &QThread::finished, my_functions, &QObject::deleteLater);  //该线程结束时销毁
    connect(my_functions, &My_Functions :: resultReady, this, &Function_Thread ::handleResults);  //线程结束后发送信号,对结果进行处理

    workerThread->start();                //启动线程
    qDebug()<<"将发送执行信号\n";
    qDebug()<<"当前线程号:" << QThread::currentThreadId() <<'\n';  //线程号为主线程号
    //发送执行信号给My_Functions执行operateFunction方法,参数也传给operateFunction
    emit operate(mode);   //emit表示发送信号
    qDebug()<<"执行信号发送完毕!" <<'\n';
}

void Function_Thread :: handleResults(const QString result)                        //处理线程执行的结果
{
    qDebug()<<"收到结果信号\n";
    qDebug()<<"当前线程号:"<< QThread :: currentThreadId() <<'\n';  //线程号为主线程号
    qDebug()<<"结果为:"<< result << "\n";
}

3. 在合适的位置初始化Function_Thread,并调用Function_Thread的方法startOperateFunction去执行其中需要调用的My_Functions的函数

举报

相关推荐

0 条评论