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. Do I need to use pointer to QImage to save memory?
Forum Update on Monday, May 27th 2025

Do I need to use pointer to QImage to save memory?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 259 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.
  • L Offline
    L Offline
    lansing
    wrote on 22 Oct 2021, 00:00 last edited by
    #1

    I have a QImage created and stored as a member variable in a class, and I want to use it in other class, so I created public function that returns the QImage. I want to reuse the original image to save memory, but I got confused if I should be returning the image itself or a pointer to the image.

    // somewhere
    m_image = QImage();
    
    QImage getImage() {
        return m_image;
    }
    
    QImage * getImage() {
        return &m_image;
    }
    

    Which one is the correct way to do it?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 22 Oct 2021, 05:09 last edited by
      #2

      Returning QImage will create a copy of the object. Returning QImage * or QImage & will not copy.

      However, QImage is a implicitly shared class, so as long as you don't modify the object, returning QImage will not copy the actual image data.

      (Z(:^

      1 Reply Last reply
      3
      • C Offline
        C Offline
        ChrisW67
        wrote on 22 Oct 2021, 05:11 last edited by ChrisW67
        #3

        @lansing No, not usually. QImage (most Qt structures holding larger data) have an internal mechanism that shares the bulk data internally if at all possible. The internal sharing will only cease if a copy is modified. This is generally what you want. For example:

        QImage SomeClass::getImage() const {
            return m_image;
        }
        

        used like this

        QImage img = objSomeClass-> getImage();
        // the actual image data is shared between img and objSomeClass->m_image until
        ...
        img.setPixel(0, 0, Qt::green);
        // when a deep copy will be triggered
        

        will do.

        Returning a pointer to an actual member variable will allow direct access to the member variable from outside the class. Often a good reason to stop and think about your design.

        L 1 Reply Last reply 22 Oct 2021, 05:45
        5
        • C ChrisW67
          22 Oct 2021, 05:11

          @lansing No, not usually. QImage (most Qt structures holding larger data) have an internal mechanism that shares the bulk data internally if at all possible. The internal sharing will only cease if a copy is modified. This is generally what you want. For example:

          QImage SomeClass::getImage() const {
              return m_image;
          }
          

          used like this

          QImage img = objSomeClass-> getImage();
          // the actual image data is shared between img and objSomeClass->m_image until
          ...
          img.setPixel(0, 0, Qt::green);
          // when a deep copy will be triggered
          

          will do.

          Returning a pointer to an actual member variable will allow direct access to the member variable from outside the class. Often a good reason to stop and think about your design.

          L Offline
          L Offline
          lansing
          wrote on 22 Oct 2021, 05:45 last edited by
          #4

          @ChrisW67

          Thank you for the explanation, I will stop returning image pointer now.

          1 Reply Last reply
          0

          1/4

          22 Oct 2021, 00:00

          • Login

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