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

How to create a QImage or QPixmap from raw pixel data (TGA)



  • I am working on a school project where we have to develop a game in C++, VIsual Studio. We had to implement a mechanism to read a TGA image into a data structure which works fine. Now I have to display the images on a QLabel, preferably with QPainter. However I can't get the Pixel Data to be read into a QImage or QPixmap.
    The pixel data is stored in a vector<char> and seperated from any header data. I have tried using QImage constructors (can't get rid of errors) or converting the vector to QByteArrays but nothing seems to be working as there is never an actual image displayed.

    I hope someone can point me to a solution which I could use so I can start in actually working with the images and painter. Thank you in advance for any help!


  • Lifetime Qt Champion

    You want the QImage ctor which takes a char* - https://doc.qt.io/qt-5/qimage.html#QImage-6



  • @Christian-Ehrlicher First of all, thank you for your help! I'm not sure what values I would put for bytesPerLine, CleanupFunction and *cleanupinfo. Could you help me with that? This is what I have thus far:
    QImage myQImage = QImage(pixelData, 32, 32, QImage::Format_ARGB32);


  • Lifetime Qt Champion

    @Meeuxi said in How to create a QImage or QPixmap from raw pixel data (TGA):

    bytesPerLine

    You must know this since you created the raw image data

    CleanupFunction and *cleanupinfo.

    Since char* data need to stay alive until the whole lifetime of the QImage object (as stated in the documentation) you have two options

    a) create a QImage::copy() of the qimage so the bytes are copied
    b) make sure to delete the bytes when the image gets destroyed - this is what the cleanup function is for. So pass a function which cleans up your image data.


Log in to reply