2

So what I want is just incrementing the progressbar with a timer. But somehow it increments the progressbar more than it should.

mainwindow.h :

Class MainWindow {
//...
private slots:
//...
    void update();
private:
    Ui::MainWindow *ui;
    QTimer *timer;
    unsigned int counter;
};

mainwindow.cpp :

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    counter = 0;
    connect(timer, SIGNAL(timeout()), this, SLOT( update() ) );
}

void MainWindow::on_actionStart_triggered()
{
    if( ui->txtTime->text().isEmpty() || (ui->txtTime->text().toInt() == 0) )
    {
        QMessageBox::warning(this, "Error", "Could not start the timer.", QMessageBox::Ok);
        return;
    }

    ui->cmdStart->setEnabled(false);
    timer->start(ui->txtTime->text().toInt() * 60000  / 60);
}


void MainWindow::update()
{
    counter++;
    ui->progressBar->setValue( counter ); //Should be incremented by one
    if( ui->progressBar->value() == 60 )
    {
        timer->stop();
        Phonon::MediaObject *music = Phonon::createPlayer(Phonon::MusicCategory,
                                                          Phonon::MediaSource( ":/Music/" + ui->chkMusic->currentText() ));
        music->play();  //Playing music
        delete timer;
    }
}

I've noticed with the debugger that the progressbar had a value of 6 while the counter had only the value 4. Also it increments first 1, then 2, then 2 again and then 1, and so on. What am I doing wrong?!

Edit: I think it is the progressbar. I changed the action to this :

void MainWindow::on_actionStart_triggered()
{
    if( ui->txtTime->text().isEmpty() || (ui->txtTime->text().toInt() == 0) )
    {
        QMessageBox::warning(this, "Error", "Could not start the timer.", QMessageBox::Ok);
        return;
    }

   // ui->cmdStart->setEnabled(false);
   // ui->progressBar->setMaximum( ui->txtTime->text().toInt() * 60 );
   // timer->start( 1000 );
    counter++;
    ui->progressBar->setValue( counter );
}

No timer will be started since I commented it out. Always when I click on the action button it increments the progressbar by 1, then 2, then 2 again and then 1. Same behavior. So it's not the timer!

Davlog
  • 2,162
  • 8
  • 36
  • 60
  • 1
    dont call your slot `update()`, this is a widely used function for widgets! – UmNyobe Jul 01 '13 at 17:28
  • Alright, now it's called increment() – Davlog Jul 01 '13 at 17:35
  • It's difficult to debug timers since they run much faster than you can read the values. You might want to write output to a log file instead. Then you can review what happened when it was running at full speed. If you include times then delays will be visible as well. – Jay Jul 01 '13 at 17:46
  • Are you creating a new music player every time the progress bar ticks? – Jay Jul 01 '13 at 17:48
  • I don't think so, it should only be created once since the timer will stop right after that. – Davlog Jul 01 '13 at 17:50
  • So I know now that it was the progressbar. I deleted the old one and just put in a new one. It seems to work. I'll try to find out what was wrong. – Davlog Jul 01 '13 at 18:00

1 Answers1

1

I think you are mismatching the QProgressBar value (a integer between minimum() and maximum()) and the displayed progress percentage, which is roughly (value-min)/(max-min)

floor(1/60*100) = 1%

floor(2/60*100) = 3%

floor(3/60*100) = 5%

floor(4/60*100) = 6%

So incrementing value() by 1 increases the percentage by the sequence : 1%, 2%, 2%, 1%...

If you want to display 60% when the counter reaches 60, you need setMaximum(100)

Am I right?

galinette
  • 8,896
  • 2
  • 36
  • 87