Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Modifying Images
QtWS25 Last Chance

Modifying Images

Scheduled Pinned Locked Moved Unsolved Game Development
qimageqpixmapqgraphicspixmapqgraphicssceneqgraphicsview
9 Posts 3 Posters 5.3k Views
  • 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.
  • A Offline
    A Offline
    Axator
    wrote on 7 Feb 2017, 22:22 last edited by
    #1

    Hello everyone,
    I've read many articles about differences between QImage and QPixmap. I can easly use QGraphicsPixmapItem to display an image from disk. However, if I want to modify pixels of this image in runtime, I'm forced to convert that pixmap to QImage, modify it, convert back to pixmap and display. This is how it looks so far:

    if(type == InterfaceType::HPBAR || type == InterfaceType::MPBAR)
        {
            QImage image;
            if(type == InterfaceType::HPBAR)
                image.load("interface/00053.png");
            else
            {
                image.load("interface/00055.png");
            }
    
            int length = image.width();
            length = (int)(((double)length * (double)value) / 100.0);
    
            for(int i = 0; i< image.width(); i++)
            {
                for(int j = 0; j< image.height(); j++)
                {
                    QColor color(image.pixel(i,j));
                    if(i>length)
                    {
                        color.setAlphaF(0.5);
                    }
                    else
                    {
                        color.setAlphaF(1);
                    }
                    image.setPixel(i,j,color.rgba());
                }
            }
            QPixmap pix = QPixmap::fromImage(image);
            this->setPixmap(pix);
        }
    
    
    

    This piece of code changes Image (which is Health Bar or Mana Bar) to show percentage value of those attributes. For example, if there's 50% health points left, 50% of this image is half-transparent. The object itself inherits from QGraphicsPixmapItem. I know it's not the best way to do things like this, because converting Image to Pixmap is expensive operation.
    Is there something different, that allows me display images and modify them anytime I want?

    Best regards.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Feb 2017, 22:30 last edited by
      #2

      Hi and welcome to devnet,

      What about putting an overlay on top of your bar rather than modify it every time ?

      Just draw your image and put another QGraphicsItem on top of it that you paint to the right value.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Axator
        wrote on 7 Feb 2017, 22:40 last edited by
        #3

        Thank you for your reply.

        I guess I dont understand. What kind of overlay? Like drawing half-transparent image to cover bar?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 Feb 2017, 23:02 last edited by
          #4

          Yes, that was the idea. But I'm realising I may have misunderstood your needs.

          In fact, you want that the complete bar changes, not some kind of gauge overlay, right ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Axator
            wrote on 8 Feb 2017, 10:47 last edited by
            #5

            This is how it looks so far:

            alt text

            I'd like to do it this way, I think it's more apparent than gauge, isn't it?

            K 1 Reply Last reply 8 Feb 2017, 12:09
            1
            • A Axator
              8 Feb 2017, 10:47

              This is how it looks so far:

              alt text

              I'd like to do it this way, I think it's more apparent than gauge, isn't it?

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 8 Feb 2017, 12:09 last edited by kshegunov 2 Aug 2017, 12:10
              #6

              I think @SGaist has the right idea. You can use one (semi-transparent) image as the base and then just paint over it (or rather have an opaque item on top). Also you could load the images directly, instead of loading them as pixmaps and then converting to QImage.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Axator
                wrote on 8 Feb 2017, 13:21 last edited by
                #7

                Okay, so leave the Pixmap as it is and use QPainter to make another half-transparent pixmap and cover the main one? Seems to be a good idea :D

                K 1 Reply Last reply 8 Feb 2017, 13:31
                0
                • A Axator
                  8 Feb 2017, 13:21

                  Okay, so leave the Pixmap as it is and use QPainter to make another half-transparent pixmap and cover the main one? Seems to be a good idea :D

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 8 Feb 2017, 13:31 last edited by
                  #8

                  @Axator said in Modifying Images:

                  Okay, so leave the Pixmap as it is and use QPainter to make another half-transparent pixmap and cover the main one?

                  Actually I'd go about it the other way around - put the transparent one (and blend it, possibly reducing the number of compositions you need to do). And then just pain the opaque one on top. One thing you should consider, however, is that all this (i.e. the composition modes) are done through the CPU, so you might not get the computational power you need if you intend to have dynamic content behind the semi-transparent items.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Axator
                    wrote on 8 Feb 2017, 14:09 last edited by
                    #9

                    What do you think about this?

                    int length = this->boundingRect().width();
                            length = (int)(((double)length * (double)value) / 100.0);
                    
                            
                            QPixmap pix(this->boundingRect().width() - length, this->boundingRect().height());
                            pix.fill(Qt::transparent);
                            QPainter p;
                            p.begin(&pix);
                            p.fillRect(pix.rect(), QColor(0, 0, 0, 120));
                            p.end();
                            gauge->setPixmap(pix);
                            gauge->setPos(pos().x()+length, pos().y());
                    

                    It works fine and the effect is the same as on example pixtures above.

                    1 Reply Last reply
                    0

                    1/9

                    7 Feb 2017, 22:22

                    • Login

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