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. readyRead() on stdin
Forum Updated to NodeBB v4.3 + New Features

readyRead() on stdin

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.5k Views 2 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    Is it possible to use the signal readyRead() on stdin?

    QFile _file;
    
    MyClient::MyClient(QObject *parent) : QObject(parent)
    {
        _file.open(stdin, QIODevice::ReadOnly);
        connect(&_file, &QFile::readyRead, this, &MyClient::readyRead);
    }
    
    void MyClient::readyRead()
    {
        qDebug() << "<<" << _file.readLine();
    }
    

    But when I type something on stdin and press enter, nothing happens.
    I tried other ways but none worked, example:

    https://github.com/juangburgos/QConsoleListener

    returns the warning "QSocketNotifier: Can only be used with threads started with QThread" and doesn't receive anything.

    JonBJ 1 Reply Last reply
    0
    • M Mark81

      Is it possible to use the signal readyRead() on stdin?

      QFile _file;
      
      MyClient::MyClient(QObject *parent) : QObject(parent)
      {
          _file.open(stdin, QIODevice::ReadOnly);
          connect(&_file, &QFile::readyRead, this, &MyClient::readyRead);
      }
      
      void MyClient::readyRead()
      {
          qDebug() << "<<" << _file.readLine();
      }
      

      But when I type something on stdin and press enter, nothing happens.
      I tried other ways but none worked, example:

      https://github.com/juangburgos/QConsoleListener

      returns the warning "QSocketNotifier: Can only be used with threads started with QThread" and doesn't receive anything.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Mark81
      Start by:

      • Check the return result of your _file.open(), always, and certainly if you having problems.

      • Look at passing some extra flags for QIODevice::OpenMode mode, try QIODevice::Text and/or QIODevice::Unbuffered.

      • Try using the bool QFile::open(int fd, ...) instead, with fd == 0, does that make any difference?

      • Do you need to close stdin/0 if they are already open for this Qt open() to work?

      • What kind of application is your code, GUI or console-only?

      Please always state what platform you are on for a question of this nature.

      returns the warning "QSocketNotifier: Can only be used with threads started with QThread"

      So are you using threading anywhere in your code/the code tested or not?

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @Mark81
        Start by:

        • Check the return result of your _file.open(), always, and certainly if you having problems.

        • Look at passing some extra flags for QIODevice::OpenMode mode, try QIODevice::Text and/or QIODevice::Unbuffered.

        • Try using the bool QFile::open(int fd, ...) instead, with fd == 0, does that make any difference?

        • Do you need to close stdin/0 if they are already open for this Qt open() to work?

        • What kind of application is your code, GUI or console-only?

        Please always state what platform you are on for a question of this nature.

        returns the warning "QSocketNotifier: Can only be used with threads started with QThread"

        So are you using threading anywhere in your code/the code tested or not?

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @JonB said in readyRead() on stdin:

        • Check the return result of your _file.open(), always, and certainly if you having problems.

        Unfortunately it returns true.

        • Look at passing some extra flags for QIODevice::OpenMode mode, try QIODevice::Text & QIODevice::Unbuffered.

        I guess you're suggesting to use | instead of &. Anyway, nothing has changed.

        Please always state what platform you are on for a question of this nature.

        It's an embedded Linux system with Qt5.11:

        # uname -a
        Linux MarkSys 4.19.49 #1 SMP PREEMPT Sun Jun 9 07:17:25 UTC 2019 armv7l armv7l armv7l GNU/Linux
        

        So are you using threading anywhere in your code/the code tested or not?

        I created a console test application. I added the source code (h, cpp files) of the example as a class and declared it into main:
        I don't use threads in my code. The only one should be the one needed to use QSocketNotifier but the warning comes before it has moved to the new thread:

         QThread _thread;
        _notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read); // <-- here the warning is printed
        _notifier->moveToThread(&_thread);
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          If you want to use QSocketNotifier in a different thread, then you need to create it in the context of that thread. You can't use moveToThread for that.

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

          M 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            If you want to use QSocketNotifier in a different thread, then you need to create it in the context of that thread. You can't use moveToThread for that.

            M Offline
            M Offline
            Mark81
            wrote on last edited by
            #5

            @SGaist said in readyRead() on stdin:

            If you want to use QSocketNotifier in a different thread, then you need to create it in the context of that thread. You can't use moveToThread for that.

            So, how the code is supposed to work?
            The author says it works!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Author ? Which one ?

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

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                Author ? Which one ?

                M Offline
                M Offline
                Mark81
                wrote on last edited by
                #7

                @SGaist said in readyRead() on stdin:

                Author ? Which one ?

                In the first question there's the link:

                https://github.com/juangburgos/QConsoleListener

                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