Search code examples
qtqt5

How increase and decrease progress bar with checkbox in Qt?


enter image description here

I want when i check each box, progress bar increase %20 and when i unchecked box, progress bar decreased %20.


Solution

  • Connect to QCheckBox::stateChanged(int state) signal, and change the progressbar value according to signal state.

    Edit

    Following the comments, I will update the answer further more. Like I said you need to connect to QCheckBox::stateChanged(int state) signal from each QCheckBox you have.

    To connect to signal you use:

    connect(ui->checkBox, &QCheckBox::stateChanged, this, &MainWindow::Update_ProgressBar);
    connect(ui->checkBox_2, &QCheckBox::stateChanged, this, &MainWindow::Update_ProgressBar);
    ...
    

    And the slot will be:

    void MainWindow::Update_ProgressBar(int value)
    {
        int currentValue = ui->progressBar->value();
        if(value == 0)ui->progressBar->setValue(currentValue - 20);
        else ui->progressBar->setValue(currentValue + 20);
    }