代码之家  ›  专栏  ›  技术社区  ›  Thomi

使用QTextStream以非阻塞方式读取stdin

  •  9
  • Thomi  · 技术社区  · 16 年前

    QSocketNotifier *pNot = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read, this);
    connect(pNot, SIGNAL(activated(int)), this, SLOT(onData()));
    pNot->setEnabled(true);
    

    onData()

    void CIPCListener::onData()
    {
        qDebug() << Q_FUNC_INFO;
        QTextStream stream(stdin, QIODevice::ReadOnly);
    
        QString str;
    
        forever
        {
            fd_set stdinfd;
            FD_ZERO( &stdinfd );
            FD_SET( STDIN_FILENO, &stdinfd );
            struct timeval tv;
            tv.tv_sec = 0;
            tv.tv_usec = 0;
            int ready = select( 1, &stdinfd, NULL, NULL, &tv );
            if( ready > 0 )
            {
                str += stream.readLine();
            }
            else
            {
                break;
            }
        }
    
        qDebug() << "Recieved data:" << str;
    }
    

    2 回复  |  直到 16 年前
        1
  •  4
  •   Gunther Piez    16 年前

        2
  •  1
  •   Community Mohan Dere    9 年前

    https://stackoverflow.com/a/7389622/721929

    我用它来实现一个基于QT控制台的应用程序,该应用程序有一个文本菜单供用户选择。