QPixmap::toImage() question
-
I have a question about QImage QPixmap::toImage() const.
I am a graphics beginner. Yes I have RTFM! Yes I know
QImage
s are different fromQPixmap
s.I have a background JPEG for my
QGraphicsScene
. I read that from file into aQPixmap
. I create aQGraphicsPixmapItem
, and put that on the scene. It works fine as background.However, wanting to squeeze every nanosecond out of my code, and wanting to learn anyway, I read that for background
QGraphicsScene::drawbackground()
method may be best way. From there I want to callpainter->drawImage()
of the pixmap. This is where the question comes.While
QGraphicsPixmapItem
takesconst QPixmap &
,QPainter::drawImage()
requires aQImage
(unless anyone knows better). I have to callQBitmap
QPixmap::toImage()
to pass it. And my question is: how "expensive" is that as an operation? Does it have to scan/transform/copy myQBitmap
QPixmap
to make aQImage
? If I start with aQBitmap
QPixmap
and I'm going to want to call that a lot fordrawImage()
, should I store the resulting image somewhere for reuse or istoImage()
so cheap it doesn't matter? I'd like to understand, as an exercise. -
Hi,
Are you sure about
QBitmap::toImage
?Why not store the QImage you are going to paint ?
-
Hi
But what about
https://doc.qt.io/qt-5/qpainter.html#drawPixmap-8Or did i miss the point ?
-
Hi
But what about
https://doc.qt.io/qt-5/qpainter.html#drawPixmap-8Or did i miss the point ?
@mrjj said in QPixmap::toImage() question:
But what about
https://doc.qt.io/qt-5/qpainter.html#drawPixmap-8
Or did i miss the point ?No, I think I did :(
void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap)
. I was usingdrawImage()
from an example elsewhere, and tried to pass myconst QPixmap &
to it (failed, foundtoImage()
). I just didn't look for adrawPixmap()
:(@SGaist too --- oohh, I've just understood, see the end below...
OK, please forget my example/reason above. Just a simple question now: if you do go
QPixmap::toImage()
for whatever reason, how "expensive" (scan/transform/copy?) is that?Given I can have
QPixmap("samefile.JPEG")
andQImage("samefile.JPEG")
is it "cheap" or "expensive" to convert between those two representations of the same picture, are they very similar or very different?UPDATE
I have only just noticed in my original post. Everywhere I have typedQBitmap
I meantQPixmap
, sorry. I am only talking aboutQPixmap
versusQImage
everywhere. I have corrected that now. -
Hi
I think it most cases they are not expensiveIt seems to come down to
void QRasterPlatformPixmap::fromImage(const QImage &sourceImage, Qt::ImageConversionFlags flags) { QImage image = sourceImage; // not sure if this will deep copy due to const createPixmapForImage(std::move(image), flags); }
so if std::move does in fact move, then the overhead is mostly creating the QPixmap class and setting
some properties. -
Hi
I think it most cases they are not expensiveIt seems to come down to
void QRasterPlatformPixmap::fromImage(const QImage &sourceImage, Qt::ImageConversionFlags flags) { QImage image = sourceImage; // not sure if this will deep copy due to const createPixmapForImage(std::move(image), flags); }
so if std::move does in fact move, then the overhead is mostly creating the QPixmap class and setting
some properties.@mrjj
OK, so for a gfx beginner like me. TheseQPixmap
s versusQImage
s do not actually have their own storage of the JPEG picture, that is shared, they don't have their own representation of the pixels. They just put a "wrapper" of their own around the image. Right? :) -
@mrjj
OK, so for a gfx beginner like me. TheseQPixmap
s versusQImage
s do not actually have their own storage of the JPEG picture, that is shared, they don't have their own representation of the pixels. They just put a "wrapper" of their own around the image. Right? :)@JonB
Hi
I think i older version of Qt, (4) , they were quite different but in Qt5
they seem to share internal representations.So its more about the interface where QImage allows to fiddle with the pixels.
Do note QImage uses implicit sharing so in cases where the QImage will detach (make copy)
then fromPixmap could cost more. -
Hi
I think it most cases they are not expensiveIt seems to come down to
void QRasterPlatformPixmap::fromImage(const QImage &sourceImage, Qt::ImageConversionFlags flags) { QImage image = sourceImage; // not sure if this will deep copy due to const createPixmapForImage(std::move(image), flags); }
so if std::move does in fact move, then the overhead is mostly creating the QPixmap class and setting
some properties.