Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a game in Qt regarding GUI windows

I've been wanting to program a simple game with a simple GUI using Qt (Its will be a VERY simple game, nothing fancy). What I've been wondering is, how can I create multiple windows and display them when needed? For an example, a battle screen and an inventory screen. The user should only see one of them, but should be able to access the other one when needed. I was using stacked widget but I'm not sure if that's the proper way. Also, is it better to design the windows in the designer or to code them?

like image 261
David Avatar asked Oct 10 '22 15:10

David


1 Answers

A StackWidget certainly would accomplish what you want to do. The reason why it is not always used for this kind of thing, is that it all the screens are pre-created at the beginning and always exist. This means it takes a little longer to initialize, and you are using more resources than you need at any one time

But as you are saying, if this is a simple game, then I don't see a big problem with it. Just as easily, you could also, create an empty layout and swap the inventory and game panels as needed.

Add my vote to everyone else suggesting to use the designer. It is so much easier to manipulate layouts, actions, and such using the designer then through code.

You can see the Designer manual here

So this is what I would suggest:

Create your "battleScreen.ui" - which is the designer file for your battle screen and everything in it, and then create your "inventory.ui". Both of these could be QWidgets, or QFrames, or whatever makes sense.

Then create your "Game.ui" which will be your QMainWindow.

In your Game main window, you can then add your QStackWidget, and place your inventory, and battle screens in the stack widget.

If you don't know how to do that... 1) drag a QWidget into your form (into the stack widget) 2) select the new QWidget and right-click. 3) Select "Promote to..." 4) Fill out the information to promote the QWidget to your inventory class Promoted Class Name: The name of your inventory class Header File: The header file of your inventory class 5) Click add 6) Click Promote.

Hope that helps.

like image 148
Liz Avatar answered Oct 14 '22 05:10

Liz