[SOLVED] Conversion from 'QByteArray' to 'const uchar*' is ambiguous?



  • I am using the overloaded method:

    @bool QPixmap::loadFromData ( const QByteArray & data, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor )@

    When I get this error. Why does Qt even try to cast the byte array to a uchar* when I am using an overload that is supposed to work directly with the byte array? I have a valid QByteArray, loaded form a file:

    @finalBrush.loadFromData(brushPrototype, QImage::Format_Indexed8);@

    finalBrush is the pixmap, brushPrototype is the byte array. Any ideas?

    Edit: I do not get the error message if I exclude the format and use the default 0, but it doesn't do me any good this way. Just noting the format has something to do with it, but I still can't find a way to resolve it. brushPrototype does indeed contain an array of 8 bit greyscale values.

    I get the feeling I am not using the format parameter properly, the image format is supposed to be an enum, and this method accepts a char * as a format parameter.

    After failing to find anything about the proper usage and format of "format" in the documentation I decided to drill into the source code so I can find usage of it. There was no direct usage of it in QPixmap, nor in QPixmapData, nor in QImageReader, the format parameter is just being passed on deeper and deeper, until I hit QImageReaderPrivate and with it a dead end...



  • "format" parameter is supposed to be "one of the following format":http://qt-project.org/doc/qt-4.8/qimagereader.html#supportedImageFormats



  • Hmm... well, I don't use any of those formats, I am using a RAW image format of 8 bit grayscale values for every pixel.



  • You cannot construct a QPixmap in this way. Instead use "this QImage constructor":http://qt-project.org/doc/qt-4.8/qimage.html#QImage-5 and then convert to QPixmap

    Edit:

    bq. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen



  • Ok, I used exactly the same constructor, but the application crashes the moment I try to draw that image:

    @ QImage test(brushData, 100, 100, QImage::Format_Indexed8);
    painter.drawImage(location, test);@

    It locks up, and I am left with:

    bq. exited with code -1073741819



  • Make sure :

    bq. the buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer



  • I am pretty sure, as everything happens in the same block of code, a few lines apart. I have noticed such odd problems with using QImage before. Note that it works if I create a QPixmap::fromImage, so the QImage is valid, it just crashes the app when I try to draw it.



  • Also for grayscale image to see properly, need to add color table:
    @
    QVector<QRgb> vectorColors(256);
    for (int c = 0; c < 256; c++)
    vectorColors[c] = qRgb(c, c, c);

    yourImage.setColorTable(vectorColors)
    @



  • [quote author="ddriver" date="1332417697"]Note that it works if I create a QPixmap::fromImage, so the QImage is valid[/quote]

    Where you do this conversion ? In paintEvent ? If yes, then data buffer seems to be valid, also is 32-bit aligned. I have no problem with these QImage constructor and then paint the image.



  • Yes, it was in the paint event... anyway...

    Thanks the color table really did the trick for me. My goal was basically this:

    !http://i41.tinypic.com/2cp9tw7.jpg(this)!

    Using a grayscale image as a brush stencil prototype and colorizing it with a user selected color, creating another pixmap that uses the prototype for alpha.

    I really didn't expect this to take two hours LOL :D


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.