添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 9 years ago .

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...

Voting to close as this is a simple typo, and the answer won't help anyone else. Such errors happen often, since compilers can't read our minds. – Kuba hasn't forgotten Monica Mar 31, 2014 at 17:25 sorry , I am checking correct timer ..it was by mistake I forgot to change d->timer->isActive() t0 timer->isActive() – JNI_OnLoad Mar 31, 2014 at 17:49 @user1618 No, you're not. You're never remembering that newly created timer anywhere. As soon as that function/method returns, the timer is forgotten by you, but it still runs, and still invokes callMethod, only that you can't access it anymore (except as sender() within the body of callMethod()). – Kuba hasn't forgotten Monica Mar 31, 2014 at 18:07

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

sorry , I am checking correct timer ..it was by mistake I forgot to change d->timer->isActive() t0 timer->isActive() – JNI_OnLoad Mar 31, 2014 at 17:49 @user1618 No, you meant the other way around. As shown, your code checks a new timer each time it runs. You never store the actively running timer anywhere, you just create new timers over and over again. – Kuba hasn't forgotten Monica Mar 31, 2014 at 18:06 Ok , it means I need to initialize it only once outside this method I guess so that the same instance will be available to stop it...Am I understood it correctly..? – JNI_OnLoad Mar 31, 2014 at 18:08 @user1618 Yes. If you only need one timer, use one timer, don't create a new one all the time. Just make a QTimer or a QBasicTimer a member of your class (or the pimpl class). – Kuba hasn't forgotten Monica Mar 31, 2014 at 18:09 @user1618 Basically, you have to understand the meaning of the code you write. When you write new Foo, that has a rather clear meaning: it gives you a fresh, new instance of Foo. You can't pretend that you're using the same Foo all the time when you, yourself, insist on getting a fresh one! – Kuba hasn't forgotten Monica Mar 31, 2014 at 18:12