Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-10200

QImage isn't copied when expected

    XMLWordPrintable

Details

    Description

      A short example from Qt Creator (src/plugins/debugger/watchhandler.cpp, line 1431):

      QLabel *l;
      int width, height, format;
      uchar *bits;
      // assign reasonable values to the above...
      QImage im(bits, width, height, QImage::Format(format));
      l->setPixmap(QPixmap::fromImage(im));
      

      The ctor of Qimage uses the memory "bits" points to without copying. This is okay, as the docs mention this.
      On Windows, neither fromImage() nor setPixmap() copy the data so that the QLabel still uses the same memory and shows random pixels after the memory "bits" points to was deleted. Calling QPixmap::detach() doesn't help as the reference count is 1.

      I don't think that this is the intended behavior. If it is then it must be documented.

      We worked around this by adding the following lines in between. If you have a better idea, please let me know.

      // enforcing copy of image data
      QImage im2(im);
      im.detach();
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              docteam Qt Documentation Team
              rlohning Robert Löhning
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes