5

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.

4
  • Your "question" is much too wide. Please be more specific. Mar 18, 2012 at 13:27
  • You're right, it could be more specific. I was unsure how to ask it - I'll provide a code sample.
    – Mitch
    Mar 19, 2012 at 8:02
  • This is not a great fit for this community. You should research game engine design and game programming first (there are lots of good books for this), then class design related advice may be better answered by http://gamedev.stackexchange.com. Mar 19, 2012 at 8:35
  • 1
    I think you're misunderstanding my question - I know how to write a game, that is not the issue. My issue is/was integrating Qt into a game to enable the game objects to be notified of input events, GUI events and everything else they require to function. The question was not so much about class design as it was about the methods or processes to achieve integration between a game and Qt. Thanks, I will use the gamedev stackexchange site for future game-related questions.
    – Mitch
    Mar 19, 2012 at 8:55

1 Answer 1

8

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.

2
  • Thank you @tmpearce. I apologise - I was not implying that Qt has no event loop, despite how it may have come across. What I meant to say was that I was unsure how to execute game logic within the application.
    – Mitch
    Mar 19, 2012 at 8:04
  • 1
    @mitch No problem. I think you're on the right track. The advice I gave about QTimer connecting to a slot is, I believe, what you're missing. Make Game derive from QObject, include the Q_OBJECT macro in the class definition, and define a custom slot (there are plenty of good docs about Qt if you don't know how to do this already). Connect the QTimer, started with start(0), to your slot, and now you're continuously updating. You can connect GUI widgets to your objects the same way, or dispatch through the master Game class - either way.
    – tmpearce
    Mar 19, 2012 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.