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. Loading RGB888 from vector to QImage/QPixmap
Forum Updated to NodeBB v4.3 + New Features

Loading RGB888 from vector to QImage/QPixmap

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 926 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    solarisx
    wrote on last edited by
    #1

    Hi
    I have an unsigned char buffer with RGB888 data and have problems to load it into a QImage or QPixmap. The image is shown in a QML scene.

    Following solutions work or not:

    Works. But releasing the pointer is cumbersome. And slower than using a vector.

    unsigned char * buffer = new unsigend char[buffer_size]
    QImage img(buffer, img_width, img_height, QImage::Format_RGB888)
    

    Works, but creating the QPixmap and a QImage takes time:

    QVector<unsigned char> buffer(buffer_size);
    QPixmap img = QPixmap::fromImage(QImage(buffer.data(), img_width, img_height, QImage::Format_RGB888));
    

    Works NOT. Crash in QImage. Why? I think this could be the fastest way:

    QVector<unsigned char> buffer(buffer_size);
    QImage imgQImage(buffer.data(), img_width, img_height, QImage::Format_RGB888);
    

    Works NOT. No image shown. Why?

    QVector<unsigned char> buffer(buffer_size);
    QPixmap img(img_width, img_height );
    img.loadFromData(buffer.data(), buffer.size(), ,Qt::ImageConversionFlag::NoFormatConversion);
    

    Any help is welcome. Greets solarsix

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      You don't show the real code so we can just guess. But please read what the ctor doc for QImage tells you about the memory pointer you pass to the ctor.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        All four are exactly the same but your image data is not initialized - so what do you really want to do?
        The fifth version is also nearly the same except a useless initialization of the QPixmap.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • S Offline
          S Offline
          solarisx
          wrote on last edited by solarisx
          #3

          Thanks for reply.
          The image data is initialized. I didn't write the code for this for simplicity's sake. The first and the second version work. If i replace the code with the third or the fourth version, it works not.

          I don't know if it matters; I use Qt 5.14.1, self compiled with libjpeg_turbo (-system-libjpeg)

          Any idea?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            You don't show the real code so we can just guess. But please read what the ctor doc for QImage tells you about the memory pointer you pass to the ctor.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            4
            • S Offline
              S Offline
              solarisx
              wrote on last edited by
              #5

              Thanks. Read the documentation again and adjusted the lifetime of the vector to that of the qimage. Now it works.
              Greets solarisx

              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