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] stdin with QIODevice
Forum Updated to NodeBB v4.3 + New Features

[Solved] stdin with QIODevice

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.5k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    t3chNo
    wrote on last edited by
    #1

    Hi i have a class named StandardInputReader inherited QIODevice. It gets input from stdin. All iodevice functionality works except readAll function. When i call readAll, it waits forever. But if i call @device.read(device.bytesAvailable());@ all works fine. I redefined
    @
    bool isSequential() const;
    qint64 bytesAvailable() const;
    bool waitForReadyRead(int msecs);

    protected:
    qint64 readData(char *data, qint64 maxSize);
    qint64 writeData(const char data, qint64 maxSize); / Does nothing */
    @

    these functions. I even looked at QIODevice source code and didn't figure out what causing this. Any suggestions?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      if you look at the source of QIODevive::read all, you find:

      @
      QByteArray QIODevice::readAll()
      {
      ....
      do {
      result.resize(result.size() + QIODEVICE_BUFFERSIZE);
      readResult = read(result.data() + readBytes, result.size() - readBytes);
      if (readResult > 0 || readBytes == 0)
      readBytes += readResult;
      } while (readResult > 0);
      ...
      }
      @

      This means, unless StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        [quote author="Gerolf" date="1318504064"]
        This means, unless StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...[/quote]
        The other way around, or?

        You mean, when StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3chNo
          wrote on last edited by
          #4

          I think i figured it out. QIODevice::read function as Gerolf calls my readData function. i used standard c read function(man 2 read) to read from stdin. when readData calls read, process is blocked until new data available for reading. Because of this, QIODevice::read enters infinite loop and never returns. I just added @
          if (bytesAvailable() == 0)
          return 0;
          @ before read and problem solved. Thanks for replies.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            [quote author="koahnig" date="1318507175"]
            [quote author="Gerolf" date="1318504064"]
            This means, unless StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...[/quote]
            The other way around, or?

            You mean, when StandardInputReader::read returns something bigger then 0 the while loop goes on iterating...[/quote]

            right,....

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            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