久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Ncurses 和 Qt 互操作性

Ncurses and Qt Interoperability(Ncurses 和 Qt 互操作性)
本文介紹了Ncurses 和 Qt 互操作性的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

有一個基于 Qt 和 ncurses 的應(yīng)用程序,在等待用戶輸入時每秒刷新屏幕的最佳方法是什么?(例如,顯示時鐘并獲取用戶輸入).

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).

我需要在 CPU 使用率和應(yīng)用程序響應(yīng)能力之間取得最佳折衷.

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

更具體地說,如何獲取用戶輸入并仍然使用 QTimer 和信號槽機(jī)制?

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. 使用 QSocketNotifier 來通知 stdin 上可用的東西.

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

在循環(huán)中調(diào)用非阻塞 getch() 直到?jīng)]有更多輸入可用.這一點(diǎn)非常重要:只有當(dāng)數(shù)據(jù)可用時,通知程序才會通知,但這并不意味著它會通知每個字符!如果您一次收到多個字符,您通常只會收到一個通知 - 因此您必須繼續(xù)發(fā)出非阻塞 getch() 直到它返回 ERR 意味著沒有更多數(shù)據(jù)目前可用.

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.

您還應(yīng)該讀取在連接套接字通知程序之前可用的所有數(shù)據(jù).

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

下面的代碼在接收到輸入時回顯,并且每秒額外輸出一個 *.這適用于 Linux 和 OS X,不能移植到 Windows.要退出,請按 Q.

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.

在需要時將 ncurses 用于傳統(tǒng)文本模式用戶界面,同時將 Qt 用于其他所有方面(計時、網(wǎng)絡(luò)、具有基于文本的視圖的數(shù)據(jù)模型、XML、QObjects 等)是一種完全有效的方法.

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
");
   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"

這篇關(guān)于Ncurses 和 Qt 互操作性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: av女优天堂| 人人艹人人爱 | 91在线成人 | 亚洲视频在线看 | 国产欧美精品一区二区三区 | 六月激情 | 免费三级网站 | 97色婷婷| 香蕉久久久 | 久久视频免费看 | 激情五月综合色婷婷一区二区 | 一级毛片在线免费观看 | 久久av红桃一区二区小说 | 少妇在线 | 欧美日韩免费在线观看 | 97精品国产97久久久久久免费 | 福利av在线 | 欧美国产精品一区二区 | 日韩福利片 | 国产成人在线视频 | 国产综合久久 | 亚洲黄色av | 91l九色lporny| 激情高潮到大叫狂喷水 | 一级黄色免费视频 | 五月天激情视频 | 日韩av免费| 成人在线观看免费爱爱 | 亚洲欧美综合另类 | 午夜激情网 | 亚洲人精品 | 激情网站在线观看 | 一区二区三区在线免费 | 日韩在线欧美 | 国产精品成人一区二区三区 | 久草手机在线视频 | 日本不卡视频在线观看 | 欧美一级在线 | 免费黄色片视频 | 美女福利视频 | 开心激情婷婷 |