【发布时间】:2015-08-24 01:22:17
【问题描述】:
编辑 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
(无限)
【问题讨论】:
-
您已经注释掉了 timer_capture->timeout() 的插槽;如果没有插槽连接到
QTimer,也许QTimer实际上什么都不做?如果它在您连接插槽时开始工作,那么看起来可能就是这样。 -
当插槽简单地连接到
&MainWindow::update_progress()(未注释)时它不起作用,但是当它连接到&QTimer::start()时它确实起作用(如预期的那样)。我尝试将其连接到update_progress(),因为这将使其与timer_updateProgress基本相同。 -
我明白了。你能通过只有一个计时器来简化吗,
timer_updateProgress?只需让它在每次超时时将进度条增加一个刻度。您是在调试器中运行还是只是观察 UI 的变化?在调试器中运行会有很大帮助,因此您可以看到计算结果。 -
我都做了,因为计时器似乎不会在调试器中的断点处暂停:P 我有两个计时器的原因是因为主计时器的长度可以随时改变,所以我认为将它分开会更有意义(还有其他逻辑处理所有这些)。我将编辑我的问题以澄清一下。
-
问题的标题具有误导性:计时器不会触发一次,它应该触发。
remainingTime已损坏,但这不会影响计时器的触发方式。有趣的是,我一直使用单独的QElapsedTimer来查看自计时器启动以来真正经过了多少时间——我的代码在 Qt5 之前的时间里有很长的寿命,所以我从来没有遇到过。但我感谢您让我注意到remainingTime()成员,一旦它起作用就会很有用:)