Qt多线程实现(一)

Qt中有两种实现多线程的方式,其中一种是写一个继承于Object的类,将这个类移入QThread 对象中,代码如下:
1.写一个继承于object 的线程类

//头文件
class executorThread : public QObject
{
	Q_OBJECT

public:
	executorThread(QObject *parent=0);
	~executorThread();
//源文件
executorThread::executorThread(QObject *parent)
	: QObject(parent)
{

}
executorThread::~executorThread()
{
}

2.主线程中将开辟的线程类移入QThread 对象中

    m_objThread = new QThread();
    m_ThreadTwo = new executorThread();//需要执行的线程类
    m_ThreadTwo->moveToThread(m_objThread);//将线程类移入QThread对象中
    connect(m_objThread, &QThread::finished, m_objThread, &QObject::deleteLater);
    connect(this, &AutoTestUi::sig_start, m_ThreadTwo, &executorThread::test);//子线程中需要执行的操作都放入test函数中
    connect(m_ThreadTwo, &executorThread::sig_showExample, this, &AutoTestUi::slot_showExample);
    connect(m_ThreadTwo, &executorThread::sig_showResult, this, &AutoTestUi::slot_showResult);
    connect(m_ThreadTwo, &executorThread::sig_identifyResult, this, &AutoTestUi::slot_identifyResult);
    
    m_objThread->start();//线程开始
    emit sig_start(m_filePath,m_box);//将数据传递给子线程

3.子线程暂停和恢复

void executorThread::pauseThread(bool status) 
{	//设置线程锁的标志位
    if (status)
    {
        QMutex m_lock.lock();//暂停
    }
    else 
    {
        QMutex m_lock.unlock();//恢复
    }
}
void executorThread::test(const QStringList& fileName, const QVector<QStandardItem*> m_box)
{
	foreach(auto step, stepList)
	{   
	QMutexLocker locker(&m_lock);//这里通过线程锁的标志位来判断线程是暂停还是运行
	resOk = true;
	exeDis.clear();
	eveOk = exep->exeCmd(step.second, resOk, exeDis);
	emit sig_showResult(stepList,step, eveOk,exeDis);
	// 对执行结果 处理, 打印,   保存
	// todo
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值