Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPixmap::toImage() question



  • I have a question about QImage QPixmap::toImage() const.

    I am a graphics beginner. Yes I have RTFM! Yes I know QImages are different from QPixmaps.

    I have a background JPEG for my QGraphicsScene. I read that from file into a QPixmap. I create a QGraphicsPixmapItem, 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 call painter->drawImage() of the pixmap. This is where the question comes.

    While QGraphicsPixmapItem takes const QPixmap &, QPainter::drawImage() requires a QImage (unless anyone knows better). I have to call QBitmapQPixmap::toImage() to pass it. And my question is: how "expensive" is that as an operation? Does it have to scan/transform/copy my QBitmap QPixmap to make a QImage? If I start with a QBitmap QPixmap and I'm going to want to call that a lot for drawImage(), should I store the resulting image somewhere for reuse or is toImage() so cheap it doesn't matter? I'd like to understand, as an exercise.


  • Lifetime Qt Champion

    Hi,

    Are you sure about QBitmap::toImage ?

    Why not store the QImage you are going to paint ?


  • Lifetime Qt Champion

    Hi
    But what about
    https://doc.qt.io/qt-5/qpainter.html#drawPixmap-8

    Or 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 using drawImage() from an example elsewhere, and tried to pass my const QPixmap & to it (failed, found toImage()). I just didn't look for a drawPixmap() :(

    @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") and QImage("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 typed QBitmap I meant QPixmap, sorry. I am only talking about QPixmap versus QImage everywhere. I have corrected that now.


  • Lifetime Qt Champion

    Hi
    I think it most cases they are not expensive

    It 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. These QPixmaps versus QImages 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? :)


  • Lifetime Qt Champion

    @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.


  • Moderators

    @mrjj that has to do a deep copy, right?
    or you would invalidate the Image on the caller side?


Log in to reply