QTimer to execute method every second

前端 未结 3 991
醉酒成梦
醉酒成梦 2020-12-22 02:05

I\'m learning Qt and I was reading about Threads, Events and QObjects from Qt wiki, and followed the wiki recommendations on how to handle some work in a while condition but

3条回答
  •  生来不讨喜
    2020-12-22 02:47

    Where is timer declared and defined?

    If it's local to Foo::connect() it'll be destroyed before it ever has a chance to fire. Presumably it just needs to be a member object of the Foo class.

    Also keep in mind that QObject provides it's own simple interface to a timer - just override the protected virtual timerEvent() function and call QObject's startTimer() to start getting those timer events. In this case instead of having a slot to receive the timer events, they will just end up at the overridden timerEvent() function:

    protected:
        void timerEvent(QTimerEvent *event) {
            processMessages();
        }
    
    public:
        void connect( /* ... */ ) {
    
                // ... 
    
                startTimer(1000);
        }
    

提交回复
热议问题