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

Loading RGB888 from vector to QImage/QPixmap



  • 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


  • Qt Champions 2019

    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 Champions 2019

    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.



  • 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?


  • Qt Champions 2019

    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.



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


Log in to reply