C++ Qt Progressbar的增量大于其应增加的值
所以我想要的是用一个计时器增加进度条。但不知何故,它增加了progressbar,超出了它应该增加的量 mainwindow.h:C++ Qt Progressbar的增量大于其应增加的值,c++,qt,qprogressbar,qtimer,C++,Qt,Qprogressbar,Qtimer,所以我想要的是用一个计时器增加进度条。但不知何故,它增加了progressbar,超出了它应该增加的量 mainwindow.h: Class MainWindow { //... private slots: //... void update(); private: Ui::MainWindow *ui; QTimer *timer; unsigned int counter; }; mainwindow.cpp: MainWindow::MainWindo
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;
}
}
我注意到调试器中progressbar的值为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。同样的行为。所以这不是计时器 我认为QProgressBar值(介于minimum()和maximum()之间的整数)与显示的进度百分比不匹配,大致为(value min)/(max min) 地板(1/60*100)=1% 地板(2/60*100)=3% 地板(3/60*100)=5% 地板(4/60*100)=6% 因此,将value()增加1会按顺序增加百分比:1%,2%,2%,1% 如果要在计数器达到60%时显示60%,则需要设置最大值(100)
我说得对吗?不要调用插槽
update()
,这是一个广泛用于小部件的函数!好的,现在它被称为increment(),很难调试计时器,因为它们的运行速度比您读取值的速度快得多。您可能希望将输出写入日志文件。然后你可以回顾它全速运行时发生的事情。如果包含时间,则延迟也将可见。是否每次进度条滴答声时都要创建一个新的音乐播放器?我不这么认为,它应该只创建一次,因为计时器将在此之后立即停止。