Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What is the best way to show multiple pictures on a widget?

What is the best way to show multiple pictures on a widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 37 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Online
    M Online
    Mbkerdellou
    wrote last edited by
    #1

    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.

    01314d7c-3ea6-4b7b-9f64-a5c2a872c579-image.png

    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

    I 1 Reply Last reply
    0
    • JonBJ Online
      JonBJ Online
      JonB
      wrote last edited by JonB
      #2

      If I were writing this "for real": each QWidget comes 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 a QAbstractTableModel + QTableView with your own QStyledItemDelegate to 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.

      1 Reply Last reply
      0
      • M Mbkerdellou

        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.

        01314d7c-3ea6-4b7b-9f64-a5c2a872c579-image.png

        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

        I Online
        I Online
        IgKh
        wrote last edited by
        #3

        @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)
        • QPixmap is 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 - QStackedLayout is 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 QWidget and drawing it manually. This is indeed done by overriding the paintEvent method. In it I'd setup a QPainter and draw from the bottom layer up - first the background on the entire contentRect(), 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 static QImage objects 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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved