欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

进度条以及多线程的使用

程序员文章站 2022-06-22 21:06:32
...

1.设置了进度条
2.使用了多线程

#include "processwidget.h"
#include "ui_processwidget.h"
#include <QTimer>
#include <QDebug>
ProcessWidget::ProcessWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ProcessWidget)
{
    ui->setupUi(this);
    nStep  = 0;
    ptimer = new QTimer;
    connect(ptimer,SIGNAL(timeout()),this,SLOT(slot_updateProgress()));
   // ptimer->start(100);
   // myth = new MyThread;


    connect(&myth,SIGNAL(send(int)),this,SLOT(slot_getdata(int)));
    connect(this,&ProcessWidget::destroyed,this,&ProcessWidget::slot_close);
     myth.start();
}


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


void ProcessWidget::slot_updateProgress()
{
    if(nStep==100)
    {
        nStep = 0;
    }
    nStep ++;
    ui->progressBar->setValue(nStep);
    update();
}


void ProcessWidget::slot_getdata(int i)
{
    qDebug()<< __func__<<i<<endl;
    ui->lineEdit->setText(QString::number(i));
    ui->progressBar->setValue(i);
    ui->progressBar->update();
    ui->lineEdit->update();
}


void ProcessWidget::slot_close()
{


     myth.terminate();
     myth.wait();
     delete myth;
}


#include "mythread.h"






void MyThread::run()
{
    int j=0;
    int i = 0;


    for(i = 0;i<10;i++)
    {


        j++;
        emit this->send(j);
      QThread::msleep(1);


    }
}


#ifndef PROCESSWIDGET_H
#define PROCESSWIDGET_H
#include "mythread.h"
#include <QWidget>


namespace Ui {
class ProcessWidget;
}
class QTimer;


class ProcessWidget : public QWidget
{
    Q_OBJECT


public:
    ProcessWidget(QWidget *parent = 0);
    ~ProcessWidget();
   // void run();


private slots:
    void slot_updateProgress();
    void slot_getdata(int i);
    void slot_close();
private:
    Ui::ProcessWidget *ui;
    QTimer  * ptimer;
    int nStep;
    MyThread  myth;


};


#endif // PROCESSWIDGET_H


#ifndef MYTHREAD_H
#define MYTHREAD_H


#include <QThread>


class MyThread : public QThread
{
    Q_OBJECT
public:
     MyThread(QObject *parent = nullptr);
protected:
    void run();//overrid
signals:
void send(int j);
public slots:
};


#endif // MYTHREAD_H