Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved]QImageReader strange behaviour

    General and Desktop
    3
    7
    2636
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      poorBob last edited by

      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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • P
          poorBob last edited by

          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

          1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            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();
            @

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • P
              poorBob last edited by

              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

              1 Reply Last reply Reply Quote 0
              • raven-worx
                raven-worx Moderators last edited by

                [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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply Reply Quote 0
                • P
                  poorBob last edited by

                  Ok, that's clear now.

                  Thank You for Your help :).

                  Robert

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post