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

标签: c++ qt qtimer


【解决方案1】:

经过更多的挖掘,我想我已经找到了答案。问题在于,即使 QTimer 未设置为 singleShotremainingTime 的行为也会如此。 timeout() 信号仍按计划发出,但在第一个信号之后,remainingTime 停留在0,而其他一切都发生了。

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

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


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多