所以我想要的只是用计时器增加进度条。但不知何故,它增加了进度条超过它应该。
主窗口.h:
Class MainWindow {
//...
private slots:
//...
void update();
private:
Ui::MainWindow *ui;
QTimer *timer;
unsigned int counter;
};
主窗口.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;
}
}
我在调试器中注意到进度条的值为 6,而计数器的值为 4。它也先递增 1,然后递增 2,然后再递增 2,然后递增 1,依此类推。我究竟做错了什么?!
编辑:我认为这是进度条。我将操作更改为:
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 );
}
自从我将其注释掉后,将不会启动任何计时器。总是当我单击操作按钮时,它会将进度条递增 1,然后是 2,然后是 2,然后是 1。同样的行为。所以不是计时器!