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 copy question

QPixmap copy question

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 240 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.
  • H Offline
    H Offline
    Hristo Konstantinov
    wrote on last edited by Hristo Konstantinov
    #1

    Let's say I have something like a QPixmap, which loads a large png file, which serves as a sprite sheet. Then I want to separate it to different QPixmaps and paint them in custom QGraphicsItem. Should I allocate the QPixmaps which contain the copy of the original QPixmap sprite sheet on the heap and delete them after painting, or I can do it on the stack? Example:

    class CustomQGraphicsItem : public QGraphicsItem
    {
         CustomQGraphicsItem()
         { 
                 original = new QPixmap;
                 original = original.load('image.png');
         }
              QPixmap *original;
        
            paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
            {
             QRect rect1(0, 0, 10, 20);
             QRect rect2(0, 20, 10, 20);
    
             QPixmap cropped1 = original.copy(rect1);
             QPixmap cropped2 = original.copy(rect2);
    
             painter.drawPixmap(0, 0, cropped1);
             painter.drawPixmap(0, 20, cropped2);
             }
    
         ~CustomQGraphicsItem()
         {
               delete original;
          }
    };
    

    Done this way, do I have to delete anything concerning the deep copy, or it's sufficient for cropped1 and cropped2 to go out of the scope of the paint function, for all the memory to be freed, and just delete original, when the object is destroyed?
    P.S. I'm writing this on a PC without IDE, so there might be some errors. I haven't written the boundingRect.

    JonBJ 1 Reply Last reply
    0
    • H Hristo Konstantinov

      Let's say I have something like a QPixmap, which loads a large png file, which serves as a sprite sheet. Then I want to separate it to different QPixmaps and paint them in custom QGraphicsItem. Should I allocate the QPixmaps which contain the copy of the original QPixmap sprite sheet on the heap and delete them after painting, or I can do it on the stack? Example:

      class CustomQGraphicsItem : public QGraphicsItem
      {
           CustomQGraphicsItem()
           { 
                   original = new QPixmap;
                   original = original.load('image.png');
           }
                QPixmap *original;
          
              paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
              {
               QRect rect1(0, 0, 10, 20);
               QRect rect2(0, 20, 10, 20);
      
               QPixmap cropped1 = original.copy(rect1);
               QPixmap cropped2 = original.copy(rect2);
      
               painter.drawPixmap(0, 0, cropped1);
               painter.drawPixmap(0, 20, cropped2);
               }
      
           ~CustomQGraphicsItem()
           {
                 delete original;
            }
      };
      

      Done this way, do I have to delete anything concerning the deep copy, or it's sufficient for cropped1 and cropped2 to go out of the scope of the paint function, for all the memory to be freed, and just delete original, when the object is destroyed?
      P.S. I'm writing this on a PC without IDE, so there might be some errors. I haven't written the boundingRect.

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

      @Hristo-Konstantinov
      QPixmaps are maintained by Qt as shared data. You do not need to keep your pixmap in scope after assigning/drawing it, so stack should be fine, with no need to delete.

      H 1 Reply Last reply
      1
      • JonBJ JonB

        @Hristo-Konstantinov
        QPixmaps are maintained by Qt as shared data. You do not need to keep your pixmap in scope after assigning/drawing it, so stack should be fine, with no need to delete.

        H Offline
        H Offline
        Hristo Konstantinov
        wrote on last edited by
        #3

        @JonB cool, thanks!

        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