Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPixmap::toImage() question
Forum Update on Monday, May 27th 2025

QPixmap::toImage() question

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 530 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you sure about QBitmap::toImage ?

      Why not store the QImage you are going to paint ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        Or did i miss the point ?

        JonBJ 1 Reply Last reply
        2
        • mrjjM mrjj

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

          Or did i miss the point ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            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.

            JonBJ J.HilkJ 2 Replies Last reply
            1
            • mrjjM mrjj

              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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @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? :)

              mrjjM 1 Reply Last reply
              0
              • JonBJ JonB

                @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? :)

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • mrjjM mrjj

                  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.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

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


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved