Can not convert A QByteArray to QImage on windows7
-
QT version 5.13.2
I read a image file from server,and cast into QImage
QByteArray filebyte = pReply->readAll(); QImage current_image = QImage::fromData(filebyte);
It's run success on windows10 and 11
but on windows7,I get an empty QImage
I just output the QByteArray into a file,and this file can normally opened as a imageAnd I trid to use QImageReader to figure out what happened
QImageReader reader(filebyte); MTLOG_INFO << QStringLiteral("Image conversion failed. Error::%1").arg(reader.errorString()); // just do some log
the error message is Image conversion failed. Error::File not found
Any god like developer can help me? I‘m alreay reach the deadline
-
First of all, please consider upgrading to Qt 6. Qt 5.13 is EOL and out of support. I haven't got a copy of it and my answer is based on Qt 6.
I can just guess what the first problem is, but it looks as if
filebyte
is emply, or the image reader can't guess the format.
Try debugging the output ofQImageReader::supportedFormats()
. Check if your image format is still supported. Try setting it explicitly by using e.g.QImage::fromData(filebyte, "image/png")
.The image reader attempt wouldn't compile in Qt 6, because the used constructor expects a file name as a QString. There is no (more) constructor of QString taking a byte array, but maybe in 5.13 there was one. I don't know.
In any case, the constructor of the image reader takes aQIODevice *
, e.g. a file and a format. In Qt 6, there is no constructor taking a plain byte array with data. -
Hi, @Axel-Spoerl : upgrading to Qt6 makes it much more difficult to run the app on Windows 7, so maybe in this case Qt5 is a better choice,
@Neether : Window 7 does not support as many different file formats as Windows 10 and 11 do, try changing from .jpg to .png format. -
@Neether said in Can not convert A QByteArray to QImage on windows7:
QByteArray filebyte = pReply->readAll();
what is pReply?
-
@Christian-Ehrlicher
it's QNetworkReply,I request a image from remote server -
@Neether And you read it after requestFinished()?
I would save it to a file and try to read it with another image reader. Also make sure the Qt jpeg image plugin is available on your system. (plugins/imageformats folder in your Qt installation).