非单发 QTimer 只倒计时一次

Non-singleshot QTimer only counts down once

编辑 3: 当我第一次问这个问题时,我以为是因为 QTimer 只触发了一次;然而事实证明是因为我是根据它的 remainingTime() 成员观察它的。事实证明 真正的 问题是 remainingTime() 只倒数到 0 一次(而 timeout() 信号 确实发射了多次)。


我有一个单线程的Qt应用程序,它有一个需要重复调​​用的定时器。我还有一个进度条来显示主计时器还剩多少时间,进度条每 40 毫秒由另一个计时器更新一次。

目前,主计时器设置为 15 秒(15 x 1000 = 15000 毫秒),但计时器仅触发其 QTimer::timeout() 事件一次。在那之后,remainingTime()总是returns0,即使isActive()returnstrue。 (还有isSingleShot() returns 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 确实 有效(因为我可以看到进度条更新),并且它的初始化方式基本相同。 ..


编辑: 澄清一下,我确定我的所有其他逻辑都正常运行,因为我可以在调试器中看到:

而且我还可以看到倒计时工作一次然后停止。

编辑 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 

(无穷大)

经过更多的挖掘,我想我找到了答案。问题是,即使 QTimer 没有设置为 singleShotremainingTime 也会像它那样工作。 timeout() 信号仍按计划发出,但在第一个信号之后,remainingTime 停留在 0,而其他一切都在发生。

我能够通过 QObject::connecttimeout() 信号发送到一个新的 slot 来测试它,该信号在调用时被定位到调试流。这表明 timeout() 被重复调用,而 remainingTime 保持 0

由于这种行为似乎不是故意的,我将尝试使用 Qt 提交错误报告。 (郑重声明,我使用的是 Qt 5.5.0。)


编辑: 看来这是一个已知错误 (46940),将在 Qt 5.5.1 中修复。