Issues with loadFromData function in QImage
-
Hello, programmers!
I am using taglib to read music file cover image and that load it into QImage. Here is my code:
QImage Widget::getCover (QString filename) { QImage cover; MPEG::File * f = new MPEG::File(filename.toStdWString().c_str()); ID3v2::Tag * id3v2tag = f->ID3v2Tag(); auto frameList = id3v2tag->frameList("APIC"); if (id3v2tag && !frameList.isEmpty()) { TagLib::ID3v2::AttachedPictureFrame * frame = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(frameList.front()); qDebug() << "Data: " << frame->picture().size(); if (cover.loadFromData((const uchar *)frame->picture().data(), frame->picture().size())) { qDebug() << "Image loaded!"; } qDebug() << cover.size(); } else { qDebug() << "id3v2 not present"; } delete f; return cover; }
Anything works fine (only with MP3 files). But when i put this code into another project, it stops working. I checked that all the code runs perfectly, the data from the file is read without problems and has the same content. Only the loadFromData function does not work as expected and returns false.
What could be the problem? Thanks in advance! (Sorry for bad english)
-
Hi and welcome to devnet,
One thing you could do is check that you get the same data with both your application.
Another thing I would do is call picture() only once, store the returned value and work with it rather than call it multiple times. Two successive calls are not guaranteed to return the same object.
-
@Nick-Redwill said in Issues with loadFromData function in QImage:
filename.toStdWString().c_str()
This doesn't look quite right.
PS.
Nor does this:
frame->picture().data()
-
@SGaist I checked, in both applications data and its size absolutely the same. Also, as i already said, in the first app all works fine, meanwhile in second project same code doesn't work. Debugging shows me, that problem in loadFromData function.
-
@kshegunov said in Issues with loadFromData function in QImage:
This doesn't look quite right.
This code works fine. Its just convert qstring to wstring and then to wchar_t *. This part works identical in both applications.
The problem occurs on the line with loadFromData function.
-
@Nick-Redwill said in Issues with loadFromData function in QImage:
Its just convert qstring to wstring and then to wchar_t *
No it's not. After the conversion from
QString
towstring
you're taking a pointer to a temporary. -
@kshegunov said in Issues with loadFromData function in QImage:
No it's not. After the conversion from QString to wstring you're taking a pointer to a temporary.
Okay, I'll fix this later. It doesn't matter now. The problem is in another part of the code.
-
I also noticed, that in application where code doesn't work occurs warning:
libpng warning: ICCP: known incorrect sRGB profile
Its occurs right after loadFromData function call.
-
@Nick-Redwill Google
libpng warning: ICCP: known incorrect sRGB profile
-
QByteArray bytes (pic.data(), pic.size()); QFile file ("test.png"); file.open(QIODevice::WriteOnly); file.write(bytes); file.close(); if (coverImg.load("test.png")) { qDebug() << "Image loaded!"; } else { qDebug() << "Error image!"; }
I changed the code to this and it also doesn't work in one project and works in another. Moreover, the file in both projects is created correctly (i can open it using the windows image viewer). The libpng warning is still displayed.
-
Why does QImage behave differently in different projects?
-
@Nick-Redwill said in Issues with loadFromData function in QImage:
I changed the code to this and it also doesn't work in one project and works in another.
What means 'does not work'.
Please proper error description... -
I solved the problem! I just needed to add the imageformats folder with dll files to the exe file folder.
-
@Nick-Redwill This is what windeployqt is for...