Qt之多线程简单学习

Qt中创建线程的方法:
只需要子类化QThread并重新实现它的run()函数就可以了。run()是个纯虚函数,是线程执行的入口,在run()里出现的代码将会在另外线程中被执行。run()函数是通过start()函数来实现调用的。

下面是我学习时的例子:

工程文件:
这里写图片描述

界面效果:
这里写图片描述

运行效果:
这里写图片描述

下面贴出代码,大家参考一下:
thread.h

#ifndef THREAD_H
#define THREAD_H

#include <QString>
#include <QThread>

class Thread : public QThread
{
    Q_OBJECT
public:
    Thread();
    void setMessage(QString message);
    void stop();

protected:
    void run();
    void printmessage();
private:
    QString messageStr;
    volatile bool stopped;

};
//stopped被声明为易失性变量(volatile variable,断电或中断时数据丢失而不可再恢复的变量类型),
//这是因为不同的线程都需要访问它,并且我们也希望确保它能在任何需要的时候都保持最新读取的数值。如果省略关键字volatile,
//则编译器就会对这个变量的访问进行优化,可能导致不正确的结果。

#endif // THREAD_H

thread.cpp

#include "thread.h"
#include <QDebug>

Thread::Thread()
{
    stopped = false;

}


void Thread::run()
{
    while(!stopped)
    {
        printmessage();
    }

    stopped =false;
}

void Thread::stop()
{
    stopped = true;
}

void Thread::setMessage(QString message)
{
    messageStr = message;
}

void Thread::printmessage()
{
    qDebug() << messageStr;
    sleep(1);
}

threadDialog.h

#ifndef THREADDIALOG_H
#define THREADDIALOG_H

#include <QDialog>
#include "thread.h"
#include <QCloseEvent>
namespace Ui {
class ThreadDialog;
}

class ThreadDialog : public QDialog
{
    Q_OBJECT

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

protected:
    void closeEvent(QCloseEvent *event);
private slots:
    void on_ApushButton_clicked();
    void on_BpushButton_clicked();
    void on_QuitpushButton_clicked();

private:
    Ui::ThreadDialog *ui;
    Thread threadA;
    Thread threadB;

};

#endif // THREADDIALOG_H

threadDialog.cpp

#include "threaddialog.h"
#include "ui_threaddialog.h"

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

    threadA.setMessage("A");
    threadB.setMessage("B");

}

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

void ThreadDialog::on_ApushButton_clicked()
{
    if(threadA.isRunning())
    {
        this->ui->ApushButton->setText("stop A");
        threadA.stop();
        this->ui->ApushButton->setText("start A");
    }
    else
    {
        this->ui->ApushButton->setText("start A");
        threadA.start();
        this->ui->ApushButton->setText("stop A");
    }
}

void ThreadDialog::on_BpushButton_clicked()
{
    if(threadB.isRunning())
    {
        this->ui->BpushButton->setText("stop B");
        threadB.stop();
        this->ui->BpushButton->setText("start B");
    }
    else
    {
        this->ui->BpushButton->setText("start B");
        threadB.start();
        this->ui->BpushButton->setText("stop B");
    }
}

void ThreadDialog::on_QuitpushButton_clicked()
{
    this->close();
  //  exit(0);
}

void ThreadDialog::closeEvent(QCloseEvent *event)
{
    threadA.stop();
    threadB.stop();
    threadA.wait();
    threadB.wait();
    event->accept();
}

main.cpp

#include "threaddialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ThreadDialog w;
    w.show();
    w.exec();
    return a.exec();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值