编辑3:当我第一次问这个问题时,我认为这是因为QTimer
只触发了一次;然而事实证明,这是因为我是根据它的remainingTime()
成员来观察它的。事实证明,真正的问题是只remainingTime()
倒计时0
一次(并且timeout()
信号确实是多次触发)。
我有一个单线程 Qt 应用程序,它有一个需要重复调用的计时器。我还有一个进度条来显示主计时器还剩多少时间,并且进度条每 40 毫秒由另一个计时器更新一次。
目前,主定时器设置为 15 秒(15 x 1000 = 15000 毫秒),但定时器仅触发其QTimer::timeout()
事件一次。之后,remainingTime()
总是返回0
,即使isActive()
返回true
。(也isSingleShot()
返回false
。)
下面是相关代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
interval_capture(15.00),
timer_capture(new QTimer(this)),
timer_updateProgress(new QTimer(this)),
isRecording(false)
{
ui->setupUi(this);
QObject::connect(timer_updateProgress, &QTimer::timeout,
this, &MainWindow::update_progress);
// I had to add the following line to force the timer to be called repeatedly
//QObject::connect(timer_capture, &QTimer::timeout,
// timer_capture, static_cast<void (QTimer::*)()>(&QTimer::start));
timer_capture->setInterval(static_cast<int>(round(interval_capture * 1000)));
timer_capture->setSingleShot(false);
timer_updateProgress->setInterval(40);
timer_updateProgress->setSingleShot(false);
}
// -snip-
void MainWindow::update_progress()
{
// The math does work
double time_passed =
static_cast<double>(interval_capture) -
static_cast<double>(timer_capture->remainingTime())/1000.0;
double fraction_passed =
time_passed / static_cast<double>(interval_capture);
int percentage = static_cast<int>(round(100 * fraction_passed));
ui->progressBar_timer->setValue(percentage);
// I only get an output of "tick: 0" after the first timeout
if (timer_capture->isSingleShot() || !timer_capture->isActive()) {
ui->progressBar_timer->setFormat("it ded");
} else {
ui->progressBar_timer->setFormat("tick: " + QString::number(timer_capture->remainingTime()));
}
}
// -snip-
void MainWindow::on_button_start_clicked()
{
isRecording = !isRecording;
switch (isRecording) {
case true :
ui->button_start->setIcon(QIcon(":/icons/stop.png"));
timer_capture->start();
timer_updateProgress->start();
break;
case false :
ui->button_start->setIcon(QIcon(":/icons/record.png"));
timer_capture->stop();
timer_updateProgress->stop();
break;
}
}
奇怪的是,我知道这timer_updateProgress
确实有效(因为我可以看到进度条正在更新),并且它的初始化方式基本相同......
编辑:澄清一下,我确信我的所有其他逻辑都正常运行,因为我可以在调试器中看到:
timer_capture.isSingleShot()
是false
timer_capture.remainingTime()
是0
timer_capture.isActive()
是true
- 所有计算结果都是正确的(例如
time_passed
)
而且我还可以看到倒计时工作一次然后停止。
编辑2:我在末尾添加了以下代码以update_progress()
进一步说明正在发生的事情:
qDebug() << "Active: " << timer_capture->isActive();
qDebug() << "Single shot: " << timer_capture->isSingleShot();
qDebug() << "Remaining ticks:" << timer_capture->remainingTime() <<
" / " << timer_capture->interval() << "\n";
我得到的输出是:
Active: true
Single shot: false
Remaining ticks: 1496 / 15000
Active: true
Single shot: false
Remaining ticks: 996 / 15000
Active: true
Single shot: false
Remaining ticks: 494 / 15000
Active: true
Single shot: false
Remaining ticks: 3 / 15000
Active: true
Single shot: false
Remaining ticks: 0 / 15000
Active: true
Single shot: false
Remaining ticks: 0 / 15000
(无限)