0

我正在尝试使用 QTimer 并且我开始良好,但是当我尝试通过使用 isActive 检查其状态来停止它时,它总是返回 false,但计时器插槽内的代码正在以正确的时间间隔执行。

这是代码

void CurrentController::currentAction(void)
{
    count++;
    QTimer *timer = new QTimer(this);
    if(count == 1)
    {
        qDebug()<< "Count" << count;

        QObject::connect(timer, SIGNAL(timeout()), this, SLOT(callMethod()));
        timer->setInterval(10000);
        timer->start();
    }

用于停止计时器

    if(count >= 2)
    {
        qDebug()<< "Count2" <<timer->isActive();
        timer->stop();
        count = 0;
    }
}

帮助感谢感谢...

4

1 回答 1

1

This is a local variable, not d->timer:

QTimer *timer = new QTimer(this); 

Yet, here you're checking if some d->timer is active:

qDebug()<< "Count2" << d->timer->isActive();

And a line later you refer to timer, not d->timer again:

timer->stop();

Maybe you need to decide which timer you wish to use, and stick with it :)

于 2014-03-31T17:24:56.287 回答