[英]Qt Progressbar Incrementing more than it should
So what I want is just incrementing the progressbar with a timer. 所以我想要的只是增加带有计时器的进度栏。 But somehow it increments the progressbar more than it should.
但是,它以某种方式增加了进度条的数量。
mainwindow.h : mainwindow.h:
Class MainWindow {
//...
private slots:
//...
void update();
private:
Ui::MainWindow *ui;
QTimer *timer;
unsigned int counter;
};
mainwindow.cpp : 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. 我已经在调试器中注意到,进度条的值为6,而计数器的值为4。此外,它先递增1,然后递增2,再递增2,再递增1,依此类推。 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.
总是当我单击操作按钮时,进度栏将依次递增1、2、2、2和1。相同的行为。 So it's not the timer!
因此,这不是计时器!
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) 我认为您不匹配QProgressBar值(minimum()和maximum()之间的整数)和显示的进度百分比,该进度百分比大致为(value-min)/(max-min)
floor(1/60*100) = 1% 地板(1/60 * 100)= 1%
floor(2/60*100) = 3% 地板(2/60 * 100)= 3%
floor(3/60*100) = 5% 地板(3/60 * 100)= 5%
floor(4/60*100) = 6% 地板(4/60 * 100)= 6%
So incrementing value() by 1 increases the percentage by the sequence : 1%, 2%, 2%, 1%... 因此,将value()递增1将按以下顺序增加百分比:1%,2%,2%,1%...
If you want to display 60% when the counter reaches 60, you need setMaximum(100) 如果要在计数器达到60时显示60%,则需要setMaximum(100)
Am I right? 我对吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.