Important: Please read the Qt Code of Conduct -

Saving a JPG from a qpixmap into a byte array

  • In my program I need to allow the user to select an image, associate the image with a room, display that image in a qlable, and save off the image data into a memory block and write that memory to a file so it can be reloaded again later from a binary load.

    simple images seem to save fine but when I write JPG files to the save data memory the code crashes (and QT creater's GDB)

    I need to be able to handle a verity of images types and eventually video.

    when I call the WRITE_IMAGE this is a room_data and saveobj is a struct room
    WRITE_IMAGE(this->picture, saveobj->picture_inx, saveobj->picture_size, GMemMan);

    I have tried this with a memory block 8 times the size of the bArray.size() and it still will not work.

    All WriteMem should be doing is taking the data at the pointer and copying into the memory space based on the size value with memcpy

    Does anyone have an idea what I could be doing wrong?

    #define WRITE_IMAGE(Picture, pictureindexloc, picturesizeloc, MeM) { \
        QByteArray bArray; \
        QBuffer buffer(&bArray); \; \
        Picture->save(&buffer, "PNG");\  //or "JPG" when using JPGs
        pictureindexloc =  MeM.WriteMem(, bArray.size());\
        picturesizeloc = bArray.size();\
    #define READ_IMAGE(Picture, pictureindexloc, picturesizeloc, MeM) { \
        const char* picturedata = TO_PTR(const char, pictureindexloc, MeM); \
        QByteArray picturearray(QByteArray::fromRawData(picturedata, picturesizeloc)); \
        Picture = new QPixmap(); \
        Picture->loadFromData(picturearray,"PNG"); \  //or "JPG" when using JPGs
    int64_t GameMemoryMan::WriteMem(void* pointer, int64_t size)
        int64_t pointerdist = WritePoint - GameData;
        memcpy ( WritePoint, pointer, size );
        int64_t temp = GetIndex(WritePoint);
        WritePoint = WritePoint + size;
        return temp;
    struct room
        int64_t Type = 3;
        int64_t Parent_inx; //002
        int64_t siblings_inx;//002 or 003
        int64_t name_inx; //string
        int64_t exits_inx;
        int64_t picture_inx;
        int64_t picture_size;
        int64_t items_inx;
        int64_t discription_inx;
        int64_t id;
        int64_t packing[16];
    class room_data : roomtreenode
        room_data(room* loadGameData, location_node_data* Parent_in = nullptr){Load(loadGameData, Parent_in);}
        room* saveobj;
        int64_t Save(bool Size);
        void Load(room* loadGameData, location_node_data* Parent_in = nullptr);
        int GetType(){ return 3;}
        location_node_data* Parent = nullptr; //002
        roomtreenode* siblings = nullptr;//002 or 003
        std::string name; //string
        exit_data* exits = nullptr;
        QPixmap* picture = nullptr;
        int64_t items_inx;
        std::string discription;
        int64_t id;

  • Lifetime Qt Champion

    That is hard to guess on as its a debuggers job.
    Do you know what line it does crash in ?
    It is while saving it ?
    not trying to load it again ?

  • the GDB dies when the QByteArray bArray is deallocated.

  • @mrjj its when i am trying to write. I can write and read some simple PNGs that i use for icons but can't save more complex images. I tried converting my jpg into a png (with another program) and it still did not work.

  • Lifetime Qt Champion

    You most likely overwrite your stack with your memcpy - why do you need to copy the data around in such a strange way at all?
    Make sure WritePoint is large enough.

  • @Christian-Ehrlicher you were correct the WritePoint was not set correctly. sorry for wasting everyone time and thanks for the help.

Log in to reply