AndreasT
AndreasT

Reputation: 9811

Qt4.5: Implicitly shared QImage: are methods like .bits() always copying (documentation unclarity)

I am writing a Qt application that has to handle big QImage s. QImage uses implicit sharing, which means it reference counts an internal data pointer. Whenever the refcount is > 1 the object counts as "shared" and any even only potentially data modifying call issues a deep copy of the image data.

In short: I don't want deep copies to happen.

I make a number of calls like setPixel(), bits() etc. that can trigger a copy. The documentation sometimes reads as if certain calls would always trigger a deep copy (detach call) even if I try my hardest to keep the refcount at 1. Like here: QImage::setPixel()

So I want to know:

  1. Is the doc only formulated a bit clumsily and these calls are reliably copying only shared objects (as in refcount > 1)?
  2. Can I ask an object what it's current refcount is, for debugging reasons and the like?
  3. Can I force Qt not to implicitly share specific objects/instances (<- well here my educated guess is "no")

Upvotes: 2

Views: 2036

Answers (1)

rpg
rpg

Reputation: 7787

  1. Operations that could modify the shared instance will detach. setPixel detaches.
  2. Try QImage::isDetached() which does a return d && d->ref == 1;. By using a debugger, you can get to the actual refcount.
  3. Other than passing by reference/shared pointer no.

Upvotes: 1

Related Questions