What is the best way to show multiple pictures on a widget?
-
Hello, I've just started learning QT recently for a school project and I am unsure about something.
This is what I have right now, but it is written in an unmaintainable and ugly way.

For each widget in the grid layout, i want to show three things, tile texture : static and a character texture and healthbar (character is image, healthbar currently im using a qwidget with a linear gradient stylesheet, but that's not a good solution, both change place on every turn).
I am not asking how to handle the changes, but how to actually draw them on the QWidget. Is the best solution creating three QLabels, setting a pixmap to each, and setting QTile as their parent? I have tried using QPainter but it seems that for that I have to change the paintEvent function? I am not looking for the actual code but just directions.
Thanks
-
If I were writing this "for real": each
QWidgetcomes with quite some overhead. You already have 100, then you are talking about 3 pixmaps per widget making it 300, and it won't scale well if you increase the number of pictures you want.I would probably use something from: (a) forget widgets and go
QGrapicsScene/View, (b) do it as aQAbstractTableModel+QTableViewwith your ownQStyledItemDelegateto draw the picture or (c) maybe some kind of single "table" widget with each pic painted at whatever position on it.Of course I quite understand if you don't want to do one of those or the purpose of the project is to use multiple widgets like you have at the moment. It also depends on what you are intending to do with this screen/pictures and your end user. You may wait to see if others agree or disagree with me.
-
Hello, I've just started learning QT recently for a school project and I am unsure about something.
This is what I have right now, but it is written in an unmaintainable and ugly way.

For each widget in the grid layout, i want to show three things, tile texture : static and a character texture and healthbar (character is image, healthbar currently im using a qwidget with a linear gradient stylesheet, but that's not a good solution, both change place on every turn).
I am not asking how to handle the changes, but how to actually draw them on the QWidget. Is the best solution creating three QLabels, setting a pixmap to each, and setting QTile as their parent? I have tried using QPainter but it seems that for that I have to change the paintEvent function? I am not looking for the actual code but just directions.
Thanks
@Mbkerdellou said in What is the best way to show multiple pictures on a widget?:
Is the best solution creating three QLabels, setting a pixmap to each, and setting QTile as their parent? I have tried using QPainter but it seems that for that I have to change the paintEvent function?
You could do it this way, but it would be quite problematic to achieve:
- As @JonB says, QWidget objects have overhead, for example in memory and in the time it takes to distribute events (which is roughly linear in the amount of widgets you have in a window)
QPixmapis a windowing system resource which Qt can't implicitly share for you, so each label would need a copy of the pixmap for the base texture etc - in general bad for memory (and on some operating system there is a limit to how many pixmaps can be allocated). Probably not a problem for the scale of your game, but still something to keep in mind.- It is not sufficient to just have the title widget be the owner of the three labels - it needs to layout them (set their size and position). There is no built-in layout manager (a subclass of
QLayout) that will quite fit the bill here -QStackedLayoutis a starting point but it will not show the background label when showing a character label for example, which kind of defeats the purpose.
The way I would implement this in Qt Widgets would be by creating a custom widget for representing the title by creating a class that inherits from
QWidgetand drawing it manually. This is indeed done by overriding thepaintEventmethod. In it I'd setup aQPainterand draw from the bottom layer up - first the background on the entirecontentRect(), then the player/enemy/ladder/whatever texture, and then a life bar on top by filling a rectangle with a gradient brush. The textures can be kept as staticQImageobjects shared across all instances of the title widget.Though - Qt Widgets isn't ideal for this scenario, QML is much more appropriate. But that depends on the parameters given to you for your project of course.