Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved QPixmap copy question

    General and Desktop
    2
    3
    98
    Loading More Posts
    • 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
      Hristo Konstantinov last edited by 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.

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @Hristo Konstantinov last edited by

        @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 Reply Quote 1
        • H
          Hristo Konstantinov @JonB last edited by

          @JonB cool, thanks!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post