QT-线程的启动、暂停和继续

一、摘要

刚开始学习QT的线程知识,有不正确的请指正。
在这里插入图片描述

二、创建线程的类,继承QThread

新建一个类,命名为MyThread,在MyThread.h中按照下列代码修改并添加缺少内容。

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QMutex>
#include <QDebug>

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = 0);

    void threadPause();
    void threadResume();

protected:
    //QThread的虚函数
    //线程处理函数
    //不能直接调用,通过start()间接调用
    void run();

signals:
    void isDone();

public slots:

private:
    bool m_buttonState; //if pause m_buttonState=false;else m_buttonState=true;
    int m_i;
    QMutex m_mutex;//互斥量
};

#endif // MYTHREAD_H
三、添加线程类的实体函数
#include "mythread.h"

MyThread::MyThread(QObject *parent) : QThread(parent)
{

}

void MyThread::run()
{
    m_buttonState=true;

    while(1)
    {

        m_mutex.lock();
        m_i++;
        qDebug()<<QString("the value of m_i is %1 ").arg(m_i);
        m_mutex.unlock();
        this->sleep(1);
    }
}

void MyThread::threadPause()//暂停线程
{
    qDebug()<<QString("pause :%1").arg(m_buttonState);
        this->m_mutex.lock();
        this->m_buttonState=false;
        qDebug()<<QString("pause");
}

void MyThread::threadResume()//继续线程
{
      qDebug()<<QString("resume :%1").arg(m_buttonState);
        this->m_mutex.unlock();
        this->m_buttonState=true;
        qDebug()<<QString("resume");

}
四、启动线程、暂停线程、继续线程

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer> //定时器头文件
#include "mythread.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    void dealTimeout();//定时器槽函数

    void dealDone();

    void stopThread();//停止线程槽函数

    void slotGrabFullScreen();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::Widget *ui;

    QTimer *myTimer;
    MyThread *thread;//线程对象
};

#endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include "ui_widget.h"
#include <QThread>
#include <QDebug>
#include <QDateTime>//系统时间的头文件
#include <Windows.h>
#include <QScreen>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    myTimer = new QTimer(this);

    //只要定时器启动,自动触发timerout()
    connect(myTimer, &QTimer::timeout, this, &Widget::dealTimeout);

    //分配空间,指定父对象
    thread = new MyThread(this);

    connect(thread, &MyThread::isDone, this, &Widget::dealDone);

    //当按窗口右上角X时,窗口触发destroyed()
    connect(this, &Widget::destroyed, this, &Widget::stopThread);
}

void Widget::stopThread()//退出应用执行关闭线程
{
   thread->threadPause();//暂停线程

    qDebug() << "开始关闭线程";
    thread->quit();//停止线程
    thread->wait();//等待线程处理完手头工作
}

void Widget::dealTimeout()//LCD控件显示
{
    static int i = 0;

    i++;

    ui->lcdNumber->display(i);
}

void Widget::dealDone()
{
    qDebug() << "it is over";

    myTimer->stop();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()//开始线程
{
    //如果定时器没有工作
    if(myTimer->isActive() == false)
    {
        myTimer->start(100);
        qDebug() << "start";
    }

    thread->start();//启动线程
}

void Widget::on_pushButton_2_clicked()//暂停线程
{
    thread->threadPause();
}

void Widget::on_pushButton_3_clicked()//线程继续
{
    thread->threadResume();
}
五、界面

在这里插入图片描述

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用Qt的多线程机制来实现任务的暂停、恢复和终止功能。以下是一个基本的示例代码: ```cpp #include <QThread> #include <QMutex> #include <QDebug> // 自定义的工作线程类 class WorkerThread : public QThread { public: // 构造函数 WorkerThread(QObject *parent = nullptr) : QThread(parent), m_paused(false), m_terminated(false) {} // 提供外部控制线程暂停的方法 void pause() { QMutexLocker locker(&m_mutex); m_paused = true; } // 提供外部控制线程恢复的方法 void resume() { QMutexLocker locker(&m_mutex); m_paused = false; m_condition.wakeAll(); } // 提供外部控制线程终止的方法 void terminate() { QMutexLocker locker(&m_mutex); m_terminated = true; m_condition.wakeAll(); } protected: // 线程执行函数 void run() override { int count = 0; while (true) { // 检查是否被暂停或终止 QMutexLocker locker(&m_mutex); if (m_paused) { m_condition.wait(&m_mutex); // 等待恢复信号 } if (m_terminated) { break; } // 执行任务 qDebug() << "Count:" << count++; msleep(1000); // 休眠1秒 } } private: QMutex m_mutex; QWaitCondition m_condition; bool m_paused; bool m_terminated; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 创建并启动工作线程 WorkerThread workerThread; workerThread.start(); // 模拟控制任务的暂停、恢复和终止 QTimer timer; timer.setInterval(3000); // 3秒后暂停任务 QObject::connect(&timer, &QTimer::timeout, [&workerThread]() { qDebug() << "Pausing task..."; workerThread.pause(); }); timer.start(); QTimer timer2; timer2.setInterval(6000); // 6秒后恢复任务 QObject::connect(&timer2, &QTimer::timeout, [&workerThread]() { qDebug() << "Resuming task..."; workerThread.resume(); }); timer2.start(); QTimer timer3; timer3.setInterval(9000); // 9秒后终止任务 QObject::connect(&timer3, &QTimer::timeout, [&workerThread]() { qDebug() << "Terminating task..."; workerThread.terminate(); }); timer3.start(); return a.exec(); } ``` 在上面的示例中,我们创建了一个名为`WorkerThread`的自定义线程类,它继承自`QThread`。该类提供了三个方法来控制线程暂停、恢复和终止。 在`run()`函数中,我们使用一个无限循环来执行任务。在每次循环开始时,我们检查是否被暂停或终止。如果被暂停,则调用`m_condition.wait(&m_mutex)`等待恢复信号。如果被终止,则跳出循环,结束线程。 在主函数中,我们创建并启动了一个`WorkerThread`实例,并使用`QTimer`模拟控制任务的暂停、恢复和终止。通过设置不同的定时器间隔,可以调整任务的执行时间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值