Important: Please read the Qt Code of Conduct -

QPixmap::loadFromData(const uchar *, len, const char * format) failing

  • Hi,

    I am trying to load an image from data. When I run my code separately [different program], the images are loaded. But when I run the same on a different program, it fails. Considering that the input is same [qt versions differ in the programs, its working on 5.9, but failing on 5.6.2], what might be the reason for the failure?

    Sample Data:

    static const unsigned char image0_data[] = {
        0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
        0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x16,
        0x08, 0x06, 0x00, 0x00, 0x00, 0xc4, 0xb4, 0x6c, 0x3b, 0x00, 0x00, 0x04,
        0xb6, 0x49, 0x44, 0x41, 0x54, 0x18, 0x95, 0x85, 0x95, 0x4d, 0x6c, 0x1b,
        0x45, 0x14, 0xc7, 0x7f, 0x33, 0xbb, 0xb3, 0xb6, 0xd7, 0xb1, 0xe3, 0x7c,
        0x35, 0x04, 0xa8, 0x1b, 0x40, 0x7c, 0x96, 0x42, 0xe5, 0x00, 0x55, 0x91,
        0x40, 0x08, 0x04, 0x55, 0x69, 0x8b, 0x10, 0x20, 0x84, 0x00, 0x71, 0x2b,
        0x6a, 0x6f, 0xdc, 0xe0, 0xc6, 0x05, 0xa9, 0x17, 0xae, 0xa8, 0x87, 0x45,
        0x5c, 0x11, 0x22, 0x82, 0x03, 0x87, 0x22, 0x8a, 0x10, 0xa2, 0x04, 0x04,
        0x14, 0x57, 0xe9, 0x07, 0xa5, 0xa5, 0x4d, 0xe9, 0x87, 0x48, 0x62, 0x27,
        0x8e, 0xd7, 0x5f, 0xbb, 0xf6, 0xee, 0xce, 0x70, 0x70, 0x9b, 0x12, 0x68,
        0xcb, 0x93, 0xe6, 0x30, 0x33, 0x6f, 0x7e, 0xf3, 0x9f, 0x37, 0x6f, 0xde,
        0x08, 0x6e, 0x60, 0xa5, 0xbd, 0xb3, 0x0a, 0xcc, 0x18, 0xe8, 0x09, 0x8c,
        0x71, 0xc1, 0x00, 0x26, 0xc0, 0xe8, 0x05, 0xa5, 0x1b, 0x95, 0x9f, 0xbc,
        0xa7, 0x7b, 0xd7, 0x5b, 0x2b, 0x6e, 0x00, 0xdd, 0x0a, 0xbc, 0x0c, 0x66,
        0x33, 0x82, 0x22, 0x42, 0x65, 0x01, 0xd0, 0xdd, 0x0e, 0x26, 0xb9, 0x08,
        0xcc, 0x0a, 0x1d, 0x4f, 0x97, 0xcc, 0x47, 0x33, 0x9e, 0xe7, 0x25, 0xff,
        0x0b, 0x2e, 0xed, 0x9d, 0xbd, 0x03, 0xc4, 0x5b, 0x58, 0xea, 0xd5, 0x81,
        0xac, 0x5b, 0xb8, 0x69, 0x34, 0x2f, 0xc7, 0x47, 0xd2, 0xdc, 0x3d, 0xda,
        0x24, 0xa3, 0x12, 0xce, 0xd6, 0x06, 0xf8, 0xb3, 0xa2, 0x59, 0xaa, 0x56,
        0x75, 0xcd, 0x0f, 0x02, 0x1d, 0x07, 0x33, 0xca, 0xb4, 0xde, 0x7b, 0xc0,
        0x7c, 0xfc, 0xa3, 0xe7, 0x79, 0xf1, 0x35, 0xc1, 0xa5, 0xbd, 0xb3, 0x1b,
        0x40, 0xec, 0xc7, 0xc9, 0x6f, 0xdf, 0x7c, 0x7b, 0x86, 0x67, 0x1f, 0xce,
        0xb1, 0x69, 0xbd, 0x61, 0x5d, 0x41, 0x61, 0xc9, 0xbe, 0xab, 0x01, 0x56,
        0x9a, 0x31, 0x73, 0x0b, 0x11, 0x07, 0xca, 0x21, 0x87, 0x8e, 0xad, 0xd0,
        0x0b, 0x1a, 0x27, 0x53, 0xba, 0xf6, 0xf6, 0xfd, 0x66, 0xfa, 0xc0, 0x15,
        0xb8, 0xf5, 0x2f, 0xa5, 0x1f, 0x0c, 0xe4, 0xf3, 0xdb, 0x9e, 0xdb, 0x3a,
        0x28, 0xde, 0x78, 0x22, 0xc3, 0x9d, 0x13, 0x16, 0x29, 0x65, 0x11, 0x25,
        0xf0, 0xed, 0x51, 0x9f, 0xb3, 0xf3, 0x21, 0x67, 0xe7, 0x43, 0x26, 0xc7,
        0xd3, 0x8c, 0xe6, 0x6d, 0x36, 0x6d, 0x50, 0x8c, 0x0f, 0x29, 0xce, 0x55,
        0xcc, 0x68, 0x23, 0x14, 0x0f, 0x2d, 0x9b, 0xdb, 0xca, 0xdb, 0xa7, 0x32,
        0x97, 0xca, 0xe5, 0xb2, 0x59, 0x05, 0x4f, 0x3c, 0xbc, 0x67, 0x1f, 0x4e,
        0xee, 0x95, 0x1d, 0x8f, 0x14, 0xc4, 0xae, 0x29, 0x85, 0xa3, 0x24, 0xbd,
        0xd8, 0xd0, 0x8d, 0x0c, 0xbf, 0xfe, 0xd1, 0xe2, 0xf9, 0x47, 0x47, 0xb8,
        0x7f, 0x32, 0xcb, 0x3d, 0xeb, 0x33, 0x7c, 0x55, 0xae, 0x33, 0x98, 0xb5,
        0x31, 0xc6, 0x70, 0xf3, 0xb0, 0x22, 0xe7, 0x2a, 0xf1, 0xdb, 0xa5, 0x64,
        0x24, 0xec, 0x19, 0x75, 0x33, 0x47, 0x0e, 0x4e, 0x4d, 0x4d, 0x45, 0x56,
        0x5f, 0xed, 0xaf, 0x5b, 0x85, 0x95, 0xda, 0xf7, 0xe0, 0x1d, 0xf9, 0xf4,
        0xf6, 0x92, 0x42, 0x4a, 0xe8, 0x45, 0x9a, 0x30, 0xd2, 0x9c, 0xf9, 0x2b,
        0x60, 0xd7, 0x96, 0x61, 0x1c, 0x25, 0xfb, 0xb1, 0x13, 0x82, 0xfb, 0x36,
        0xb8, 0x7c, 0x7b, 0xd4, 0x27, 0x75, 0x79, 0xf3, 0xe1, 0x01, 0x49, 0xbb,
        0x67, 0x73, 0xa1, 0x1a, 0x4d, 0x56, 0x93, 0xf5, 0xb3, 0xe3, 0x9c, 0x3c,
        0x67, 0x95, 0xf6, 0x1c, 0x56, 0xc0, 0x5b, 0xb9, 0x5c, 0xee, 0xf1, 0xa7,
        0x36, 0x67, 0xc5, 0xd0, 0x00, 0x74, 0x23, 0x43, 0x18, 0x19, 0xaa, 0x7e,
        0xc4, 0xce, 0x2d, 0xc3, 0xa4, 0x1d, 0xf9, 0x9f, 0xac, 0xb9, 0xeb, 0xd6,
        0x0c, 0x3f, 0x9f, 0x6a, 0x22, 0x10, 0x84, 0x91, 0xc6, 0xb1, 0x7a, 0xcc,
        0x2d, 0x6a, 0xa7, 0x1d, 0xc6, 0x91, 0x15, 0xfb, 0x87, 0x6c, 0x60, 0x0c,
        0xd8, 0x9c, 0xcf, 0x0f, 0xca, 0x42, 0x3a, 0xa6, 0xd1, 0xbe, 0xa2, 0x0c,
        0x76, 0x3c, 0x32, 0x74, 0x4d, 0x28, 0x80, 0x6d, 0x09, 0x5e, 0x7a, 0x6c,
        0x94, 0xe9, 0x43, 0x4b, 0x7c, 0x71, 0x74, 0x80, 0x46, 0xe0, 0xd2, 0x64,
        0x40, 0x6a, 0xea, 0x1b, 0x1b, 0xa2, 0x58, 0x94, 0xc0, 0x04, 0x42, 0x16,
        0x47, 0x73, 0x82, 0xa0, 0xa7, 0xa9, 0xb7, 0x62, 0x84, 0x80, 0x6d, 0x53,
        0x05, 0x32, 0xa9, 0xfe, 0x15, 0x44, 0xb1, 0x5e, 0x03, 0x8d, 0x13, 0x83,
        0x31, 0x06, 0x80, 0x9d, 0x5b, 0x46, 0xd8, 0xf5, 0x40, 0x8b, 0xe5, 0x4e,
        0x0a, 0xec, 0x2c, 0x08, 0x6b, 0x02, 0xcb, 0x29, 0x4a, 0xc0, 0x45, 0xa6,
        0xb2, 0xae, 0x8a, 0xa8, 0xb7, 0x35, 0x8d, 0x8e, 0xe6, 0x99, 0x52, 0x81,
        0x9c, 0x6b, 0x03, 0x50, 0x6b, 0x44, 0x7c, 0x3e, 0x53, 0x5b, 0x03, 0x9e,
        0x9e, 0x69, 0x52, 0x9e, 0xeb, 0xa7, 0x6c, 0xda, 0xe9, 0x2b, 0xbf, 0x7d,
        0x5d, 0x3f, 0xfe, 0xc8, 0x54, 0x46, 0x40, 0x6e, 0xf5, 0x9c, 0x8d, 0x50,
        0xb1, 0xe8, 0x4b, 0x2a, 0x0d, 0xc9, 0xf7, 0x27, 0x3a, 0x00, 0xb4, 0x02,
        0xcd, 0xfb, 0x5f, 0x74, 0x39, 0x70, 0x3c, 0xbd, 0x06, 0x3c, 0xb7, 0x92,
        0xe7, 0xdd, 0xcf, 0x14, 0x33, 0xa7, 0xfa, 0xfd, 0xaf, 0x8f, 0xc3, 0x82,
        0x7f, 0xf5, 0x51, 0x08, 0xb4, 0xb4, 0xc1, 0x04, 0xe8, 0x6e, 0x67, 0xb1,
        0x9d, 0xa1, 0x9d, 0xa4, 0xc1, 0xc0, 0x99, 0xef, 0xe0, 0xc3, 0xef, 0x02,
        0x9a, 0x3d, 0x07, 0x63, 0x32, 0xd7, 0x8c, 0xb3, 0x1f, 0xc0, 0x3b, 0x9f,
        0x5c, 0xed, 0x0b, 0x01, 0x02, 0x03, 0x49, 0x18, 0x0a, 0x13, 0x77, 0x6d,
        0x8c, 0x5e, 0x00, 0x2e, 0xfa, 0x1d, 0x7d, 0x5b, 0x22, 0x32, 0x6b, 0x16,
        0x1b, 0xe4, 0x0d, 0xaa, 0xc9, 0x55, 0xb3, 0x2d, 0x90, 0x40, 0x2b, 0xe8,
        0x82, 0x49, 0x2a, 0x52, 0xb7, 0x17, 0xa4, 0xd2, 0x8d, 0x0a, 0x30, 0x4b,
        0xb7, 0xa6, 0x95, 0x65, 0x70, 0xfe, 0xd1, 0x52, 0x56, 0x02, 0xc6, 0x70,
        0xf9, 0x9e, 0xd6, 0x98, 0xb2, 0x34, 0xae, 0x4a, 0x70, 0x55, 0x42, 0xda,
        0x4e, 0x70, 0xec, 0x04, 0xd1, 0x5b, 0xd6, 0x36, 0xe1, 0xe9, 0x82, 0x39,
        0x3f, 0x6f, 0xff, 0xe4, 0x3d, 0xdd, 0x9b, 0x7a, 0xf3, 0xc7, 0xe9, 0xa4,
        0xeb, 0xbf, 0xae, 0x92, 0xcc, 0xd0, 0x50, 0xce, 0x59, 0x23, 0x52, 0x1b,
        0x70, 0x1d, 0x01, 0xe4, 0x56, 0xc7, 0x46, 0xdd, 0x80, 0xf5, 0xf9, 0x68,
        0xd5, 0xaf, 0x5f, 0x3f, 0x7a, 0xe8, 0x70, 0xa5, 0x9d, 0x49, 0x16, 0x0f,
        0x0e, 0x5a, 0x4b, 0x35, 0x09, 0x50, 0x32, 0x1f, 0xcd, 0xe8, 0x38, 0xf8,
        0xb4, 0x59, 0xaf, 0x32, 0xe8, 0x74, 0x18, 0x73, 0x03, 0xc6, 0xb2, 0x01,
        0x63, 0x6e, 0xc0, 0x78, 0x36, 0x60, 0xd4, 0x0d, 0x58, 0x69, 0xf5, 0x2b,
        0x63, 0xd0, 0xd3, 0xd8, 0x74, 0x59, 0x97, 0xbd, 0xea, 0x33, 0xe8, 0x74,
        0x68, 0xd6, 0xab, 0xe8, 0x9e, 0xff, 0xcd, 0xdd, 0xf2, 0xeb, 0x1f, 0x80,
        0xa6, 0x05, 0x50, 0x2e, 0x97, 0xcd, 0xad, 0xa5, 0xd7, 0xe6, 0xba, 0x11,
        0xf7, 0x6a, 0x9d, 0x6c, 0xb8, 0x65, 0xc4, 0x12, 0x6e, 0x4a, 0x90, 0x52,
        0x86, 0x94, 0x32, 0xd8, 0xd2, 0x70, 0xbe, 0xd2, 0xa5, 0x15, 0x68, 0x8e,
        0x9e, 0x0b, 0x38, 0x3b, 0x1f, 0xf6, 0xe7, 0x6c, 0x83, 0x20, 0xe2, 0xf4,
        0x85, 0x86, 0x5e, 0xae, 0xf9, 0x87, 0x47, 0x92, 0x13, 0xfb, 0x0b, 0xe2,
        0xaf, 0x13, 0xab, 0x60, 0x80, 0x1d, 0x25, 0xb5, 0xbc, 0x6c, 0x8a, 0x87,
        0xfd, 0xc0, 0xde, 0xd8, 0xe8, 0xc4, 0x93, 0x39, 0x57, 0x31, 0x9c, 0xb3,
        0x71, 0x94, 0xc4, 0xb1, 0x05, 0xbd, 0xd8, 0x30, 0x5f, 0x8b, 0xa8, 0xb7,
        0x13, 0x1c, 0x5b, 0xa0, 0x2c, 0x58, 0xf2, 0x23, 0x8e, 0xcd, 0xf9, 0x2c,
        0x54, 0x1b, 0x87, 0xc7, 0xe2, 0x23, 0xfb, 0x8a, 0xb2, 0xfc, 0x0b, 0xb0,
        0xe4, 0x79, 0x5e, 0xb2, 0x0a, 0x2e, 0x97, 0xcb, 0x66, 0xfb, 0x94, 0x5b,
        0x5b, 0x36, 0x93, 0x47, 0xfc, 0xc0, 0x72, 0xab, 0x7e, 0x34, 0xd9, 0xec,
        0xc4, 0x4e, 0xda, 0x8e, 0x45, 0x36, 0x63, 0x63, 0x4b, 0x89, 0x14, 0x60,
        0x4c, 0xc2, 0xf2, 0x4a, 0x9b, 0xe3, 0x7f, 0x76, 0xf4, 0xef, 0x17, 0x9a,
        0xad, 0x7a, 0xbd, 0xf1, 0xe5, 0x48, 0x72, 0x62, 0xff, 0x65, 0x68, 0xc5,
        0xf3, 0xbc, 0x08, 0xae, 0x91, 0x4c, 0xbb, 0x77, 0xef, 0x96, 0x40, 0xf6,
        0x18, 0x2f, 0x3e, 0x19, 0xcb, 0x81, 0x17, 0x34, 0x6a, 0xa3, 0x94, 0x62,
        0x22, 0x97, 0xb1, 0x32, 0x00, 0xcd, 0x20, 0x09, 0x75, 0xa2, 0x2b, 0x36,
        0xe1, 0xe9, 0x74, 0xb2, 0x78, 0xf0, 0x72, 0x4c, 0x17, 0x81, 0xfa, 0x75,
        0x7f, 0x90, 0x7f, 0xc0, 0x05, 0xe0, 0x54, 0x92, 0xc9, 0x42, 0x43, 0x14,
        0x8b, 0x58, 0x4e, 0x51, 0x40, 0x4e, 0xa0, 0xa5, 0x30, 0x71, 0x57, 0xea,
        0xf6, 0x42, 0xc1, 0x9c, 0x9f, 0x1f, 0xb4, 0x96, 0x6a, 0x40, 0x13, 0x08,
        0x3c, 0xcf, 0x5b, 0x53, 0x50, 0xfe, 0x06, 0x92, 0x1b, 0x42, 0x64, 0xc8,
        0x47, 0x19, 0x58, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae,
        0x42, 0x60, 0x82

    Usage method :

    image0.loadFromData(image0_data, sizeof(image0_data), "PNG")

    The function returns false and no image is loaded. Could someone confirm that qt 5.6.2 has some problems for loadFromData(...) ?

  • Lifetime Qt Champion

    @Ankit.Jain Can you load PNG files with Qt 5.6.2 in general? Maybe PNG plug-in is missing?

  • Moderators

    I have it somewhere in my mind, that the char array needs to have an even number of bytes to be loaded in a Pixmap.
    I might be totally wrong here, but that char array at least has an uneven number of Bytes in it.

  • @J.Hilk Like I said, previously, 5.9 [and 5.10] are able to convert the data to an image. Hence I'll reject that option for the time being.

    @jsulm I am not aware of any plug-in requirements for PNG, might I be suggested how to check. Also, I don't know about 5.6.2, but 4.8 and 5.9+ seem to be able to convert the data to an image (the bug has been there since we updated from 4.8 to 5.6.2).

  • Lifetime Qt Champion

    @Ankit.Jain said in QPixmap::loadFromData(const uchar *, len, const char * format) failing:

    might I be suggested how to check

    Simply try to load a PNG image in your app (from a file).

Log in to reply