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

Ncurses 和 Qt 互操作性

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

問(wèn)題描述

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

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.

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

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

使用下面的代碼時(shí),定時(shí)器不起作用.

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

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

推薦答案

  1. 使用 QSocketNotifier 來(lái)通知 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ù)可用時(shí),通知程序才會(huì)通知,但這并不意味著它會(huì)通知每個(gè)字符!如果您一次收到多個(gè)字符,您通常只會(huì)收到一個(gè)通知 - 因此您必須繼續(xù)發(fā)出非阻塞 getch() 直到它返回 ERR 意味著沒(méi)有更多數(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.

下面的代碼在接收到輸入時(shí)回顯,并且每秒額外輸出一個(gè) *.這適用于 Linux 和 OS X,不能移植到 Windows.要退出,請(qǐng)按 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.

在需要時(shí)將 ncurses 用于傳統(tǒng)文本模式用戶界面,同時(shí)將 Qt 用于其他所有方面(計(jì)時(shí)、網(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 互操作性的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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)形?)
主站蜘蛛池模板: 精品免费视频一区二区 | 亚洲在线中文字幕 | 黄色毛片在线播放 | 亚洲国产精品va在线看黑人 | 国产精品视频yy9299一区 | 国产ts人妖一区二区三区 | 国产精品99久久久久久动医院 | 日韩在线播放视频 | 国产精品资源在线观看 | 岛国av一区二区三区 | 亚洲福利片 | 男女羞羞视频在线观看 | 国产高清久久 | 一区二区在线不卡 | 欧美日韩一区二区电影 | 精品一区二区在线观看 | 免费国产黄 | 91av在线视频观看 | 日韩欧美在线观看 | 亚洲女人的天堂 | 久久久久久久久久久福利观看 | 亚洲精品视频在线播放 | 久久99精品久久久久久琪琪 | 成人在线观看网站 | 色爱综合网 | 国产小视频在线 | 日韩电影免费观看中文字幕 | 91在线观看 | 国产精品特级片 | 国产精品久久久久久吹潮 | 精品熟人一区二区三区四区 | 一区亚洲 | 美国黄色一级片 | 免费在线观看av片 | av在线免费播放 | 成人在线免费电影 | 免费在线观看av网址 | 国产一级特黄视频 | 亚洲最大av| 欧美日韩国产一区二区三区 | 中文字幕精品一区久久久久 |