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!