Copy QPixmap, which way is more efficient
-
Hi all:
Question 1:
I wan to copy QPixmap 'SRC' to Qpixmap 'DST'
Currently I am doing in this way :@QPainter p( &DST );
p.drawPixmap( x, y, SRC );@Another way is
2)
@QPixmap DST = SRC.copy(0,0,SRC.width(), SRC.height() ) const@Which way is better ? 1 or 2 in terms of performance and memory?
Question 2:
I want to copy QBitmap SRC to QBitmap DST
using@QBitmap DST = SRC.copy(0,0, SRC.width(), SRC.height()); @
It won't compile using g++ 4.2.This is the API from Qt documant:
@QPixmap QPixmap::copy ( int x, int y, int width, int height ) const@What is the correct way of doing QBitmap copy.
Thanks!
-Todd
-
- I think it's the same
- I think:
@
QBitmap DST(SRC.width(), SRC.height());
DST = SRC.copy(0,0, SRC.width(), SRC.height());
@
-
Since you are copying the entire pixmap / bitmap, why not just do:
@QPixmap DST = SRC;@
Or, for bitmaps:
@QBitmap DST = SRC@
(provided that SRC is a bitmap, of course).Pixmaps and bitmaps are implicitly shared, but that's not really something you have to worry about - it just means that copying will be really fast, since the actual copying will not be done until you actually modify DST (or SRC) so that they are no longer identical.
Copying should be faster (or at least not slower) than painting.
-
If you really need a deep copy, the just call
@
QPixmap dst = src.copy();
@But I would recommend copy-on-write semantics with Qt's implicitly shared mechanism, which adheres to QPixmap.
See the docs on "Implicit Sharing":http://doc.qt.nokia.com/4.7/implicit-sharing.html for more details.
-
As stated above implicit sharing is best in terms on memory & performance but from the options you gave.
QPixmap DST = SRC.copy(0,0,SRC.width(), SRC.height() ) const
is slightly faster. As it performs a deep copy.
The QPainter method will also perform a deep copy if the SRC pixmap is opaque or you specify the composition mode QPainter::CompositionMode_Source. In which case the difference between the methods is the time to setup the QPainter. As a side note if your source pixmap is not opaque you must specify CompositionMode_Source for the two operations to give the same result.
-
By the way, what would be the fastest way to do this if we intended the new pixmap to be larger than than the original? (without resizing the original contents).
I do it using a painter, but it feels kind of odd, having to start a painter just to expand a QPixmap:
@
QPixmap old = ...
QPixmap canvas = QPixmap(size);QPainter p(&canvas);
p.drawPixmap( QPoint(0,0), old);@QPixmap.copy() can take a size, but this is only useful if we are cropping the original pixmap, not expanding it.
-
To anyone reading this decade-old topic, you can use this in Qt5/6:
QPixmap clone = original.copy();
-
This post is deleted!