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. Read text from Windows console
Forum Updated to NodeBB v4.3 + New Features

Read text from Windows console

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.2k 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.
  • M Offline
    M Offline
    mrdebug
    wrote on 4 Jun 2014, 08:24 last edited by
    #1

    Is there a way to get text from a windows console not in blocking mode?
    (Without use std::cin.get)

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jeroentjehome
      wrote on 4 Jun 2014, 09:52 last edited by
      #2

      Without the cin use, nup, don't think so. Only when you make the terminal yourself and use QProcess or include it in your own program should that be possible. Why don't you want to use the cin?

      Greetz, Jeroen

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrdebug
        wrote on 4 Jun 2014, 13:29 last edited by
        #3

        Because cin is a blocking function.
        I want to have an event when someone click on keyboard or verify if there is something (with a timer) to read before read.
        That in a console application.

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hskoglund
          wrote on 4 Jun 2014, 21:07 last edited by
          #4

          Hi, if you're not planning porting your app to Linux/Macs you have a bonanza of oldstyle Win32 console API's to choose from. here's a simple example:

          Create a Qt vanilla console app, erase everything in main.cpp and paste this in:
          @
          #include <iostream>
          #include <conio.h>
          #include <QDateTime>
          #include <QThread>

          int main()
          {
          std::cout << "Type q to exit\n";

          for (;;)
          {
              if (_kbhit())
                  if ('q' == _getch())
                      exit(0);
          
              std::cout << QDateTime::currentDateTime().toString("ss").toStdString() << " ";
              QThread::sleep(1);
          }
          

          }
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on 4 Jun 2014, 23:29 last edited by
            #5

            Create a background thread like:

            @class MyThread : public QThread
            {
            signals:
            void inputReceived(const QString &text);
            protected:
            virtual void run(void);
            };

            // ---------------

            void MyThread::run(void)
            {
            for(;;)
            {
            char buffer[1024];
            std::cin.get(buffer, 1024)
            const QString text = QString::fromLatin1(buffer).trimmed();
            if(!text.empty()) emit inputReceived(text);
            }
            }@

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrdebug
              wrote on 5 Jun 2014, 09:17 last edited by
              #6

              On Windows is perfect but on Linux I have to press q and then return. Can you suggest me the way to press only q on Linux?

              On Windows:

              @ #include "conio.h"

              QString BufferIn;
              while (kbhit()!= 0) BufferIn.append(getch());
              if (BufferIn.compare("q", Qt::CaseInsensitive)== 0) QCoreApplication::instance()->quit();@
              

              On Linux:

              @ #include "stdio.h"
              #include <termios.h>
              #include <sys/ioctl.h>

              int QCKeyboard::kbhit(void) {
                  struct timeval tv;
                  fd_set read_fd;
                  tv.tv_sec= 0;
                  tv.tv_usec= 0;
                  FD_ZERO(&read_fd);
                  FD_SET(0, &read_fd);
                  if (select(1, &read_fd, NULL, NULL, &tv)== -1) return 0;
                  if (FD_ISSET(0, &read_fd)) return 1;
                  return 0;
              }
              
              QString BufferIn;
              while (kbhit()!= 0) BufferIn.append(getchar());@
              

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hskoglund
                wrote on 5 Jun 2014, 14:46 last edited by
                #7

                Re. Linux, almost there: turn off the console buffering at app launch, like this:
                @
                struct termios ts;
                tcgetattr(0, &ts);
                ts.c_lflag &= ~ICANON; /* disable buffered i/o */
                tcsetattr(0,TCSANOW,&ts);
                @

                1 Reply Last reply
                0

                1/7

                4 Jun 2014, 08:24

                • Login

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