Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QImageReader strange behaviour
QtWS25 Last Chance

[Solved]QImageReader strange behaviour

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.1k Views
  • 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 Offline
    P Offline
    poorBob
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • P Offline
        P Offline
        poorBob
        wrote on last edited by
        #3

        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
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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
          0
          • P Offline
            P Offline
            poorBob
            wrote on last edited by
            #5

            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
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              [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
              0
              • P Offline
                P Offline
                poorBob
                wrote on last edited by
                #7

                Ok, that's clear now.

                Thank You for Your help :).

                Robert

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved