非单一拍摄QTimer只倒计时一次

时间:2015-08-24 01:22:18

标签: c++ qt qtimer

编辑3:当我第一次提出这个问题时,我认为这是因为QTimer只发射了一次;然而事实证明这是因为我根据其remainingTime()成员观察它。事实证明,真正的问题是remainingTime()只计算一次0(并且timeout()信号 确实会触发多次次)。

我有一个单线程Qt应用程序,它有一个需要重复调​​用的计时器。我还有一个进度条显示主计时器剩余的时间,进度条每隔40ms由另一个计时器更新。

目前,主计时器设置为15秒(15 x 1000 = 15000 ms),但计时器仅触发其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 

(ad infinitum)

0 个答案:

没有答案