Question

I'd like to know how to update and render game objects when using Qt. E.g. with a typical game you'd have an event loop, but Qt just has exec(). What is the correct way to update and render game objects using Qt? How do I do things like get the time between each frame to update my game objects? Do I use a separate thread for all of this?

My question is quite similar to this thread, except I'm not trying to avoid standard Qt processes.

Edit: I apologise for the incomplete question. I believe tmpearce has already answered my question, but here's a basic example of what I'm trying to do in case anyone else has the same issue:

int main(int argv, char **args)
{
    QApplication app(argv, args);

    ApplicationWindow window;
    window.show();

    // How and where do I update my Game object?

    return app.exec();
}

class Game
{
public:
    void timeUpdate()
    {
        // Update game entities, etc. Should be called every frame.
    }

    void render()
    {
        // Renders game entities - where should I render to?
        // Should be called 30 times a second, etc.
    }
};

Cheers.

Was it helpful?

Solution

Qt has an event loop; it is started by QApplication::exec(). I have no idea what your requirements are as far as updating and rendering; but since you seem to want an event loop like in a "typical game", I'll assume you can take it from there.

In your application, create a QTimer. Connect it's timeout signal to a slot you want to call on each event loop. Call QTimer::start(0): this will trigger the timeout signal each time through the event loop.

As far as timing goes, check out QElapsedTimer - I suspect it is what you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top