QTimer to execute method every second

2019-08-12 08:19发布

问题:

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 its not working for my specific case. Here's a simple example of what I'm currently trying to achieve.

class FooEvents : public FooWrapper {

    public virtual serverTime(..) { std::cout << "Server time event\n"; }
    public virtual connected(..) { std::cout << "Connected event\n"; }
}

class Foo : public QObject {

private:

    FooAPI *client;

public:


    Foo(FooEvents *ev, QObject *parent = 0) : client(new FooApi(ev)) { .. }

private slots:
    void processMessages() {

        if (state is IDLE)              

            reqFooAPiServerTime();

        select(client->fd()+1, ...);

        if (socket is ready for read)

            client.onReceive();

    }
public:
    void connect(...) {

        if (connection) {

            QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(processMessages()));
            timer.start(1000);  // I don't get the output from FooEvents

        }

    }

}

This is a very simple but I think it illustrates my case. Why is this not working and what other alternatives to I have to handle this case? Thanks.s

Edit: The processMessages is being called every second but I don't get any output from the events

回答1:

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


回答2:

This won't work, because processMessages() is not a SLOT.

 So Declare processMessages() as a private slot and then try.


回答3:

You don't declare the timer neither the slot. In the header you must declare:

class ... {

  QTimer timer;
  ...
private slots:
  void processMessages();
  ...
};

Then remember to make the SIGNAL-SLOT connection and configure the timer:

connect(&timer, SIGNAL(timeout()), this, SLOT(processMessages()));
timer.setInterval(1000);
timer.start();

Also timer.start(1000); would be valid...

ANOTHER POSSIBILITY

Other possibility would be to use the timer associated with each Q_OBJECT and overload the timerEvent:

class ... {
  Q_OBJECT
  ...
protected:
  void timerEvent(QTimerEvent *event);
  ...
};

Then you must implement the timer event as this:

void MyClass::timerEvent(QTimerEvent *event) {
  processMessages();
}

And you can configure the timer with a simple call to startTimer(1000);



标签: c++ qt qt4