当前位置:网站首页>Day14QProgressBar2021-10-17

Day14QProgressBar2021-10-17

2022-06-22 02:44:00 Morning and evening rain

QProgrssBar—— Implementation of progress bar

The progress bar component combined with the timer will realize the simple application of the progress bar , This article will be published in Day3 Achieve content consistency , namely , single click start Button , The progress bar is incremented by one per second , single click end, Progress bar stop , Overflow from 10% Start . The difference is , Another way to realize it , Another way to write signals and slots , Provide more ideas for you to write large-scale projects .
Let's introduce Program implementation steps :

1.h Declare functions and variables in

publicvoid Init();
    void doProcessStart();
    void doProcessEnd();
    void doProcessTimeOut();
    QTimer * myTimer;
    int num;

2. Realization 、 Call initialization function Init();

//.cpp To realize 
void MainWindow::Init()
{
    
    num = 0;
    connect(ui->btn_start,&QPushButton::clicked,this,&MainWindow::doProcessStart);
    connect(ui->btn_end,&QPushButton::clicked,this,&MainWindow::doProcessEnd);
    myTimer = new QTimer(this);
    connect(myTimer,&QTimer::timeout,this,&MainWindow::doProcessTimeOut);
}
// Invocation in constructor 
 MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    
    ui->setupUi(this);
    Init();
}

3. Realization doProcess function

void MainWindow::doProcessStart()
{
    
    myTimer->start(1000);
}
void MainWindow::doProcessEnd()
{
    
    myTimer->stop();
}
void MainWindow::doProcessTimeOut()
{
    
    num++;
    if(num==101)
    {
    
        num = 10;
    }
    ui->progressBar->setValue(num);
}

4. Code and effect ( Small scenario application )

single click start Button , Turn on timer ; single click end Button , off timer ; After the timer is turned on , The progress bar will appear in 1S Add for interval 1, When the progress bar is 100% when , from 10% Begin to continue .
 Insert picture description here

原网站

版权声明
本文为[Morning and evening rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211658130262.html