QT5 QThread

基于http://blog.csdn.net/liang19890820/article/details/52186626修改

适用于像我这样的小白,目的就是实现点击开始按钮,进度条值从0++到100



添加线程类:

不知道是什么原因,我添加QThread的C++ class总是不成功,我拷贝了以前同事建立的项目下面的thread_receive.h与thread_receive.cpp文件,然后手动添加在项目中。


1

在mainwindow.h头文件中,定义了一个thread_receive类;在mainwindow.cpp源文件中实例化:

thread_reflash = new thread_receive(this);
 

startThread_slot()函数:

void MainWindow::startTheard_slot()
{
    thread_refresh->start();
}

connect了pStartButton与startThread_slot()

connect(ui->pStartButton, SIGNAL(clicked()), this, SLOT(startThread_slot()));

先编译一下,看有没有报错,然后点击一下按钮,看有没有qDebug信息输出


2

接下来要编写thread_receive里面的run()函数了

run() 函数实现的功能是间隔50ms将value值+1,并emit出去

这里定义了一个指向MainWindows的指针pt,因为我的value值,resultReady()函数都在MainWindows的构造函数中定义的

void thread_receive:: run()
{
 
    MainWindow *pt = (MainWindow*)parent();  
    qDebug() << "Worker Run Thread : " << QThread::currentThreadId();
    
    while (pt->value < 100)
    {
        msleep(50);
        pt->value++;
        qDebug() << pt->value;
        emit pt->resultReady(pt->value);
    }
    qDebug() << "end thread!" << endl;
}

3

在MainWindows中,connect了resultReady信号到show_slot()槽函数,show_slot()槽函数实现进度条m_pProgressBar值的变化

connect(this, SIGNAL(resultReady(int)), this, SLOT(show_slot()));

void MainWindow::show_slot()
{
 
    ui->m_pProgressBar->setValue(value);
    qDebug() << value;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值