[Solved]QImageReader strange behaviour
-
Hello Qt developers,
I'm using the QImageReader in my application and I faced very strange problem. I use method QImageReader::read() but it seems to not work as I expect, because when I call this method for second time an error ""Unable to read image data" appears..
Here's an example code how I use QImageReader
@
class View : public QWidget
{
// ...private:
QImageReader* m_pReader;
};View::View()
{
m_pReader = new QImageReader();
m_pReader->setFileName( "home/image.jpg");
QImage oImage = m_pReader->read();// so far everything is ok :)
// but when I call:
QImage oImageCopy = m_pReader->read();
qDebug() << m_pReader->errorString();// No image is read and I recieve: "Unable to read image data" error
}
@Can anyone point what I'm doing wrong here?
Edit: I'm using Qt 4.8.5
Robert
-
Hi,
You don't need to use QImageReader directly, just do
@QImage oImage("path_to_image/image.jpg");
QImage oImageCopy = oImage.copy();@and you're good to go
-
Hello SGaist,
of course You're right with using the QImage::copy() and QImage itself :). I already tried this already and this works :).
But I would like to use QImageReader, because I use it in many places in my class...
Robert
-
what happens when you do this:
@
m_pReader = new QImageReader();
m_pReader->setFileName( "home/image.jpg");
QImage oImage = m_pReader->read();m_pReader->device()->seek(0); //Note: error handling left out
QImage oImageCopy = m_pReader->read();
@ -
Hello raven-worx,
When using this line:
@m_pReader->device()->seek(0); //Note: error handling left out@
image can be read again.I have also noticed that setting the file name once again with QImageReader::setFileName(...) will fix the problem, but the image is reseted i.e. any manipulation (size,rotation) are lost. And since I need those manipulation is not solution for me...
So Yours suggestion solved my problem, but I still don't have any idea why do I have to call it... So I will wait before adding [solved] tag to the topic.
Robert
-
[quote author="poorBob" date="1384946576"]
So Yours suggestion solved my problem, but I still don't have any idea why do I have to call it... So I will wait before adding [solved] tag to the topic.
[/quote]
You have to call it because QImageReader uses a QIODevice internally to read from the image file. Either way Qt does also reset the position internally or it's just not necessary because QImageReader is only used once every time it is created for reading image files. You can check this in the Qt code if you like.Nevertheless you can read on the "QIODevice docs":http://qt-project.org/doc/qt-4.8/qiodevice.html#pos why you have to do this in detail. Basically you have to reset it so the next read() call starts again from the beginning of the file and not from the position where the last access has left off.
-
Ok, that's clear now.
Thank You for Your help :).
Robert