QT学习之旅 - QThread多线程

文章目录


其实QT中的thread(线程)是很容易的

首先是主线程

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    //threadTest = new ThreadTest;//线程实例
	//threadTest->start();//开启线程
    for(int i=0;i<100;i++){
    
    
        qDebug() << "mainwindow i:" << i;
        _sleep(500);
    }

}

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

其次是一个程序

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

Test::Test()
{
    
    
    for(int i=0;i<100;i++){
    
    
        qDebug() << "i=" << i;
        _sleep(1000);
    }
}

Test::~Test(){
    
    

}

通过一个QThread来放入程序

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

ThreadTest::ThreadTest()
{
    
    

}

void ThreadTest::run(){
    
    
    test = new Test;
}
void ThreadTest::stop(){
    
    

}

ThreadTest::~ThreadTest(){
    
    

}

一个简单的线程就实现了

进阶一点: 手动开启关闭线程

添加一个按键,通过信号和槽来控制线程使能关闭

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QPushButton>

bool isTrue;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    isTrue = true;
    threadTest = new ThreadTest;
    connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::on_button_test);
    for(int i=0;i<100;i++){
    
    
        qDebug() << "mainwindow i:" << i;
        _sleep(100);
    }

}

void MainWindow::on_button_test(){
    
    
    if(isTrue){
    
    
        threadTest->start();
        isTrue = false;
        ui->pushButton->setText("关闭线程");
    }else{
    
    
        threadTest->stop();
        isTrue = true;
        ui->pushButton->setText("开启线程");
    }
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
//    threadTest->de;
    delete threadTest;
}
#include "threadtest.h"
#include <QDebug>

ThreadTest::ThreadTest()
{
    
    

}

void ThreadTest::run(){
    
    
    test = new Test;
}
void ThreadTest::stop(){
    
    
    this->quit();
    this->wait();
}

ThreadTest::~ThreadTest(){
    
    

}

Test不变。现象mainwindow i:99(执行完)后窗口出现,之后开启线程。开启后立马关闭,会执行到i=99(线程执行完)后才关闭

俩个线程

第一个线程后,我们再来一个线程,也是在主函数中实例。查看情况

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

ThreadTestTwo::ThreadTestTwo()
{
    
    

}
void ThreadTestTwo::run(){
    
    
    for(int i=0;i<100;i++){
    
    
        qDebug() << "threadTwo i=" << i;
        _sleep(200);
    }
}
void ThreadTestTwo::stop(){
    
    

}
ThreadTestTwo::~ThreadTestTwo(){
    
    

}

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QPushButton>

bool isTrue;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);
    isTrue = true;
    threadTest = new ThreadTest;
    threadTestTwo = new ThreadTestTwo;
    connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::on_button_test);

}

void MainWindow::on_button_test(){
    
    
    if(isTrue){
    
    
        threadTest->start();
        threadTestTwo->start();
        isTrue = false;
        ui->pushButton->setText("关闭线程");
    }else{
    
    
        threadTest->stop();
        threadTestTwo->stop();
        isTrue = true;
        ui->pushButton->setText("开启线程");
    }
}

MainWindow::~MainWindow()
{
    
    
    delete ui;
    delete threadTest;
}

猜你喜欢

转载自blog.csdn.net/yasinawolaopo/article/details/131708416