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. When does QImage’s detach method perform a deep copy of the data?
Forum Updated to NodeBB v4.3 + New Features

When does QImage’s detach method perform a deep copy of the data?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 338 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    John Van
    wrote on last edited by
    #1
        uchar buf[1000] = {};
        QImage img(buf,10,10,QImage::Format_Grayscale8);
        auto d1 = img.constBits();
        img.detach();
        auto d2 = img.constBits();
        img = img.copy();
        auto d3 = img.constBits();
        img.detach();
        auto d4 = img.constBits();
    

    "detach" has never been effective.

    Christian EhrlicherC 1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #3

      When does QImage’s detach method perform a deep copy of the data?

      What QImage::detach() method? Cannot see this documented anywhere (Qt 5, Qt 6).

      There is an internal buffer that is detached (deep copied) from other, shared copies of that buffer when one of the QImage instances calls a non-const method on itself. If there is no sharing of a buffer occurring, then there is no deep copy to be done. If there is only one QImage then there cannot be any sharing of a buffer.

      jsulmJ 1 Reply Last reply
      0
      • J John Van
            uchar buf[1000] = {};
            QImage img(buf,10,10,QImage::Format_Grayscale8);
            auto d1 = img.constBits();
            img.detach();
            auto d2 = img.constBits();
            img = img.copy();
            auto d3 = img.constBits();
            img.detach();
            auto d4 = img.constBits();
        

        "detach" has never been effective.

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

        @John-Van said in When does QImage’s detach method perform a deep copy of the data?:

        "detach" has never been effective.

        Why should it? The reference count is never > 1 so no detach needed.
        See https://doc.qt.io/qt-6/implicit-sharing.html

        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
        1
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #3

          When does QImage’s detach method perform a deep copy of the data?

          What QImage::detach() method? Cannot see this documented anywhere (Qt 5, Qt 6).

          There is an internal buffer that is detached (deep copied) from other, shared copies of that buffer when one of the QImage instances calls a non-const method on itself. If there is no sharing of a buffer occurring, then there is no deep copy to be done. If there is only one QImage then there cannot be any sharing of a buffer.

          jsulmJ 1 Reply Last reply
          0
          • C ChrisW67

            When does QImage’s detach method perform a deep copy of the data?

            What QImage::detach() method? Cannot see this documented anywhere (Qt 5, Qt 6).

            There is an internal buffer that is detached (deep copied) from other, shared copies of that buffer when one of the QImage instances calls a non-const method on itself. If there is no sharing of a buffer occurring, then there is no deep copy to be done. If there is only one QImage then there cannot be any sharing of a buffer.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @ChrisW67 Well @John-Van calls constBits. But by looking into the documentation it is clear that constBits does not detach:
            "Note that QImage uses implicit data sharing, but this function does not perform a deep copy of the shared pixel data, because the returned data is const."
            https://doc.qt.io/qt-6/qimage.html#constBits

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • J John Van has marked this topic as solved on

            • Login

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