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. QPixmap variable wont update when drawing
Qt 6.11 is out! See what's new in the release blog

QPixmap variable wont update when drawing

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 985 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.
  • R Offline
    R Offline
    RetroZelda
    wrote on last edited by
    #1

    So I have a QPixMap that is used to draw multiple instances of the same image with the goal of being able to change the QPixMap to then change all instances that are already being drawn.

    my setup is something like this:

        // in my header file nested in a QGraphicsScene child class
        struct _InstanceData
        {
            uint m_x;
            uint m_y;
        };
    
        struct _PaintData
        {
            QPixmap m_ImageToDraw;
            const QImage* m_SourceImage;
            std::vector<_InstanceData> m_Instances;
        };
    
        // in my header file as a member
        _PaintData m_PaintData;
    
        // in my source file to create a new QPixMap from a QImage
        void QGraphicsSceneChild::RefreshImage(const QImage* image)
        {
             if(image == m_PaintData.m_SourceImage)
            {
                m_PaintData.m_ImageToDraw = QPixmap::fromImage(*image); // a breakpoint will hit here
                // _PaintData.m_ImageToDraw.fill(Qt::red); // doesnt work either
            }
            update();
        }
    
    
    // in my source file to draw each image.  
    void QGraphicsSceneChild::drawBackground(QPainter *painter, const QRectF &view_rect)
    {
        QGraphicsScene::drawBackground(painter, view_rect);
    
        QRect tile_rect(0, 0, 128, 128);
        for(const _InstanceData& instance : m_PaintData.m_Instances)
        {
                tile_rect.setX(instance.m_x);
                tile_rect.setY(instance.m_y);
                painter->drawPixmap(tile_rect, m_PaintData.m_ImageToDraw);
        }
    }
    

    Everything in regards to painting each instance does work as I expect when the QPixMap is first created. However, when I call RefreshImage(), passing in a pointer to the original QImage that gets modified in the parent window, it will not update.

    I have also tried deep copying the QImage before creating the QPixMap; creating a new QPixMap and calling swap(); and even creating a new QImage and filling it before creating the QPixMap. I feel like theres something im missing and so any help would be appreciated.

    thanks

    JonBJ Christian EhrlicherC 2 Replies Last reply
    0
    • R RetroZelda

      So I have a QPixMap that is used to draw multiple instances of the same image with the goal of being able to change the QPixMap to then change all instances that are already being drawn.

      my setup is something like this:

          // in my header file nested in a QGraphicsScene child class
          struct _InstanceData
          {
              uint m_x;
              uint m_y;
          };
      
          struct _PaintData
          {
              QPixmap m_ImageToDraw;
              const QImage* m_SourceImage;
              std::vector<_InstanceData> m_Instances;
          };
      
          // in my header file as a member
          _PaintData m_PaintData;
      
          // in my source file to create a new QPixMap from a QImage
          void QGraphicsSceneChild::RefreshImage(const QImage* image)
          {
               if(image == m_PaintData.m_SourceImage)
              {
                  m_PaintData.m_ImageToDraw = QPixmap::fromImage(*image); // a breakpoint will hit here
                  // _PaintData.m_ImageToDraw.fill(Qt::red); // doesnt work either
              }
              update();
          }
      
      
      // in my source file to draw each image.  
      void QGraphicsSceneChild::drawBackground(QPainter *painter, const QRectF &view_rect)
      {
          QGraphicsScene::drawBackground(painter, view_rect);
      
          QRect tile_rect(0, 0, 128, 128);
          for(const _InstanceData& instance : m_PaintData.m_Instances)
          {
                  tile_rect.setX(instance.m_x);
                  tile_rect.setY(instance.m_y);
                  painter->drawPixmap(tile_rect, m_PaintData.m_ImageToDraw);
          }
      }
      

      Everything in regards to painting each instance does work as I expect when the QPixMap is first created. However, when I call RefreshImage(), passing in a pointer to the original QImage that gets modified in the parent window, it will not update.

      I have also tried deep copying the QImage before creating the QPixMap; creating a new QPixMap and calling swap(); and even creating a new QImage and filling it before creating the QPixMap. I feel like theres something im missing and so any help would be appreciated.

      thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @RetroZelda
      This is only a theory! Are you sure your update() is really causing the whole scene to be repainted? I can't recall if it works that way, or only causes updated areas to be repainted. Do you need to call void QGraphicsScene::invalidate(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = AllLayers)?

      Put a qDebug() << view_rect into your drawBackground(), both to determine when it is being called and what rectangular area gets redrawn.

      R 1 Reply Last reply
      0
      • JonBJ JonB

        @RetroZelda
        This is only a theory! Are you sure your update() is really causing the whole scene to be repainted? I can't recall if it works that way, or only causes updated areas to be repainted. Do you need to call void QGraphicsScene::invalidate(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = AllLayers)?

        Put a qDebug() << view_rect into your drawBackground(), both to determine when it is being called and what rectangular area gets redrawn.

        R Offline
        R Offline
        RetroZelda
        wrote on last edited by
        #3

        @JonB

        I just attempted calling invalidate() and it gives the same results

        i can also confirm that the proper areas do get redrawn in drawBackground(). I put something like this above where im drawing the QPixMaps to force a flicker on regions that get drawn.

        static uint hack = 0;
        if(++hack % 4 != 0)
            return
        
        1 Reply Last reply
        0
        • R RetroZelda

          So I have a QPixMap that is used to draw multiple instances of the same image with the goal of being able to change the QPixMap to then change all instances that are already being drawn.

          my setup is something like this:

              // in my header file nested in a QGraphicsScene child class
              struct _InstanceData
              {
                  uint m_x;
                  uint m_y;
              };
          
              struct _PaintData
              {
                  QPixmap m_ImageToDraw;
                  const QImage* m_SourceImage;
                  std::vector<_InstanceData> m_Instances;
              };
          
              // in my header file as a member
              _PaintData m_PaintData;
          
              // in my source file to create a new QPixMap from a QImage
              void QGraphicsSceneChild::RefreshImage(const QImage* image)
              {
                   if(image == m_PaintData.m_SourceImage)
                  {
                      m_PaintData.m_ImageToDraw = QPixmap::fromImage(*image); // a breakpoint will hit here
                      // _PaintData.m_ImageToDraw.fill(Qt::red); // doesnt work either
                  }
                  update();
              }
          
          
          // in my source file to draw each image.  
          void QGraphicsSceneChild::drawBackground(QPainter *painter, const QRectF &view_rect)
          {
              QGraphicsScene::drawBackground(painter, view_rect);
          
              QRect tile_rect(0, 0, 128, 128);
              for(const _InstanceData& instance : m_PaintData.m_Instances)
              {
                      tile_rect.setX(instance.m_x);
                      tile_rect.setY(instance.m_y);
                      painter->drawPixmap(tile_rect, m_PaintData.m_ImageToDraw);
              }
          }
          

          Everything in regards to painting each instance does work as I expect when the QPixMap is first created. However, when I call RefreshImage(), passing in a pointer to the original QImage that gets modified in the parent window, it will not update.

          I have also tried deep copying the QImage before creating the QPixMap; creating a new QPixMap and calling swap(); and even creating a new QImage and filling it before creating the QPixMap. I feel like theres something im missing and so any help would be appreciated.

          thanks

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @RetroZelda said in QPixmap variable wont update when drawing:

          if(image == m_PaintData.m_SourceImage)

          Are you sure this is correct? Why do you work with pointers at all and why do you do this comparison?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          R 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @RetroZelda said in QPixmap variable wont update when drawing:

            if(image == m_PaintData.m_SourceImage)

            Are you sure this is correct? Why do you work with pointers at all and why do you do this comparison?

            R Offline
            R Offline
            RetroZelda
            wrote on last edited by
            #5

            @Christian-Ehrlicher that line is correct. I use the pointer as a lookup as this is part of a larger chunk of code. My example was just a minimized example that reproduces the issue. The check passes as expected for what im doing, so I dont think its necessarily related to this problem(but i also am still very new to QT so I could be wrong).

            essentially I have QImages that I have modifiable inside the program and when they get modified it seems like the QPixMap needs to be recreated in order to display the changes. So im currently using the address of the QImage in order to find the QPixMap that I need to update. This of course can be changed if theres a better standard for a QT codebase, I am just not aware of anything like that.

            however, with or without the QImage pointer, the issue is still present that the QPixMap doesnt want to update when I attempt to set it to either the QImage that gets passed in, or through a brand new QImage on the stack or heap.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Please provide a minimal, compilable example - your code above looks fishy, esp. the pointer comparision but otherwise ok so there must be a problem somewhere else.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              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