QT5创建线程

在QT5开发中,为实现多线程操作,通常需要自定义C++类继承QThread,并覆盖run方法。本文将详细阐述如何创建并使用这些线程类,包括头文件(如cfaceclassthread.h)、源代码文件(cfaceclassthread.cpp)以及在MainWindow.cpp中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发应用是经常会用到多线程,使用QT5实现多线程的方式时,首先新建C++类继承QThread,然后重写run方法。

1.cfaceclassthread.h

#ifndef CFACECLASSTHREAD_H
#define CFACECLASSTHREAD_H
#include <QThread>
#include <QEventLoop>
#include <QTimer>

#include <iostream>
using namespace std;

class CFaceClassThread : public QThread
{
public:
    CFaceClassThread();

    //结束线程标志位
    bool bStopThread;

    void run();
};

#endif // CFACECLASSTHREAD_H


2.cfaceclassthread.cpp

#include "cfaceclassthread.h"

CFaceClassThread::CFaceClassThread()
{
    bStopThread = false;
}

//线程执行函数
void CFaceClassThread::run()
{
    //cout<<"thread run!"<<endl;
    bStopThread = false;
    while(!bStopThread)
    {
        //to do what you want

        //延时等待一会
        QEventLoop eventloop;
        QTimer::singleShot(100, &eventloop, SLOT(quit()));
        eventloop.exec();
    }
}
<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值