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(); }
Use
QSocketNotifier
to be notified of things being available onstdin
.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-blockinggetch()
until it returnsERR
meaning that no more data is available at the moment.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.