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. Notifications from Stdin on Windows
Forum Updated to NodeBB v4.3 + New Features

Notifications from Stdin on Windows

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.3k Views 4 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.
  • A Offline
    A Offline
    Ananym
    wrote on 25 Feb 2019, 12:21 last edited by Ananym
    #1

    I'm trying to make an application that can process messages sent to stdin without blocking the event loop, on Windows.
    I've been trying both QSocketNotifier, initialized as notifier_(STDIN_FILENO,QSocketNotifier::Read,this) and QWinEventNotifier, initialized as notifier_(GetStdHandle(STD_INPUT_HANDLE),this).

    QSocketNotifier appears to never fire at all. My suspicions are that this is expected behavior in windows, but the docs don't seem to mention its use in conjunction with stdin on windows.

    QWinEventNotifier fires constantly, regardless of whether anything is in the stdin buffer or not. The docs do state "The state of the event is not modified in the process, so if it is a manual reset event, you will need to reset it after the notification." but calling ResetEvent on the handle provided by the signal doesn't do anything - the signal still emits continuously.

    Am I doing something wrong, or should I just expect QWinEventNotifier to be clogging up my event loop like this? Is there a better way to get signals on windows corresponding to data actually being available from stdin?

    I'm not sure what the right way to approach actually reading from stdin from inside this slot is. Since the signal doesn't actually mean there's anything available to read, any attempt to read causes my program to hang inside that slot forever until some data is written. The std::cin.eof() check always reports false even when no data is present. I tried std::cin.rdbuf()->in_avail(), but this always reports 0 even when data is written. QTextStream.atEnd() doesn't seem to work here. If I can't fix the signal to actually mean that data is available, is there at least some kind of means of checking for available data before I start an infinitely hanging read call?

    K 1 Reply Last reply 25 Feb 2019, 21:44
    1
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 25 Feb 2019, 20:43 last edited by
      #2

      What does the STDIN_FILENO macro evaluate to? Is it the handle number, or a pointer to the handle number? Read the docs for the QSocketNotifier constructor carefully.

      1 Reply Last reply
      0
      • A Ananym
        25 Feb 2019, 12:21

        I'm trying to make an application that can process messages sent to stdin without blocking the event loop, on Windows.
        I've been trying both QSocketNotifier, initialized as notifier_(STDIN_FILENO,QSocketNotifier::Read,this) and QWinEventNotifier, initialized as notifier_(GetStdHandle(STD_INPUT_HANDLE),this).

        QSocketNotifier appears to never fire at all. My suspicions are that this is expected behavior in windows, but the docs don't seem to mention its use in conjunction with stdin on windows.

        QWinEventNotifier fires constantly, regardless of whether anything is in the stdin buffer or not. The docs do state "The state of the event is not modified in the process, so if it is a manual reset event, you will need to reset it after the notification." but calling ResetEvent on the handle provided by the signal doesn't do anything - the signal still emits continuously.

        Am I doing something wrong, or should I just expect QWinEventNotifier to be clogging up my event loop like this? Is there a better way to get signals on windows corresponding to data actually being available from stdin?

        I'm not sure what the right way to approach actually reading from stdin from inside this slot is. Since the signal doesn't actually mean there's anything available to read, any attempt to read causes my program to hang inside that slot forever until some data is written. The std::cin.eof() check always reports false even when no data is present. I tried std::cin.rdbuf()->in_avail(), but this always reports 0 even when data is written. QTextStream.atEnd() doesn't seem to work here. If I can't fix the signal to actually mean that data is available, is there at least some kind of means of checking for available data before I start an infinitely hanging read call?

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 25 Feb 2019, 21:44 last edited by
        #3

        Something along these lines should be working, right?

        QFile * file = new QFile;
        if (!file->open(stdin, QIODevice::ReadOnly))
            QTextStream(stdout) << "Can't open the standard input";
        
        QObject::connect(qApp, &QCoreApplication::aboutToQuit, file, &QObject::deleteLater);
        QObject::connect(file, &QIODevice::readyRead, file, [file] () ->  {
            QTextStream(stdout) << file->readAll();
        });
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • A Offline
          A Offline
          Ananym
          wrote on 27 Jul 2020, 16:47 last edited by
          #4

          The above won't work - QFile never emits readyRead, according to docs.

          The perpetually activating QWinEventNotifier remains a mystery - but surely there's some way to read stdin via a QFile that doesn't block forever if there's no data available?

          M 1 Reply Last reply 27 Jul 2020, 17:08
          0
          • A Ananym
            27 Jul 2020, 16:47

            The above won't work - QFile never emits readyRead, according to docs.

            The perpetually activating QWinEventNotifier remains a mystery - but surely there's some way to read stdin via a QFile that doesn't block forever if there's no data available?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 27 Jul 2020, 17:08 last edited by
            #5

            @Ananym
            Hi
            I dont think they ever added that option.
            waitForReadyRead seems to do nothing so no easy option to use QFile for stdin.

            https://bugreports.qt.io/browse/QTBUG-19345

            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