I am trying to use QTimer and I is starting well, but When I am trying to stop it by checking its state using isActive, it returning false always but the code inside the slot for timer is executing in correct interval.

here is code

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();
    }

for stopping timer

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

Help appreciated thanks...

有帮助吗?

解决方案

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 :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top