Ncurses and Qt Interoperability

谁说我不能喝 提交于 2019-12-17 06:16:12

问题


Having a Qt and ncurses based application, what is the best way to refresh the screen every second, while waiting for user input? (e.g. show the clock and get user input).

I need the best compromise between CPU usage and application responsiveness.

To be more specific with the question, how to get user input and still use QTimer and the signal-slot mechanism?

When using the code below, the timers doen't work.

nodelay(stdscr,true); while(1) { sleep(1); getch(); processInput(); }

回答1:


  1. Use QSocketNotifier to be notified of things being available on stdin.

  2. Call nonblocking getch() in a loop until no more input is available. This is vitally important: the notifier will notify only when new data is available, but this doesn't mean that it notifies on every character! If you receive multiple characters at a time, you will usually get just one notification - thus you must keep issuing non-blocking getch() until it returns ERR meaning that no more data is available at the moment.

  3. You should also read all of the data that became available in the time before the socket notifier became attached.

The code below echoes the input as it receives it, and additionally outputs a * every second. This works on Linux and OS X, and is not portable to Windows. To quit, press Q.

Using ncurses for a legacy text-mode user interface, where desired, while leveraging Qt for everything else (timing, networking, data models with text-based views, XML, QObjects, etc.) is a perfectly valid approach.

// https://github.com/KubaO/stackoverflown/tree/master/questions/ncurses-20606318
#include <QtCore>
#include <ncurses.h>

class Worker : public QObject
{
   Q_OBJECT
   QSocketNotifier m_notifier{0, QSocketNotifier::Read, this};
   QBasicTimer m_timer;
   Q_SLOT void readyRead() {
      // It's OK to call this with no data available to be read.
      int c;
      while ((c = getch()) != ERR) {
         printw("%c", (char)(c <= 255 ? c : '?'));
         if (c == 'q' || c == 'Q') qApp->quit();
      }
   }
   void timerEvent(QTimerEvent * ev) {
      if (ev->timerId() != m_timer.timerId()) return;
      printw("*");
      refresh();
   }
public:
   Worker(QObject * parent = 0) : QObject(parent) {
      connect(&m_notifier, SIGNAL(activated(int)), SLOT(readyRead()));
      readyRead(); // data might be already available without notification
      m_timer.start(1000, this);
   }
};

int main(int argc, char *argv[])
{
   QCoreApplication a{argc, argv};
   Worker w;
   auto win = initscr();
   clear();
   cbreak(); // all input is available immediately
   noecho(); // no echo
   printw("Press <q> to quit\n");
   keypad(win, true); // special keys are interpreted and returned as single int from getch()
   nodelay(win, true); // getch() is a non-blocking call
   auto rc = a.exec();
   endwin();
   return rc;
}

#include "main.moc"



回答2:


I found the answer to signal-slot mechanism not working inside while loop, is QCoreApplication::processEvents(); So, to receive the signals:

while(1)
{
    sleep(1);
    getch();
    processInput();
    QCoreApplication::processEvents();
}


来源:https://stackoverflow.com/questions/20606318/ncurses-and-qt-interoperability

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!