tom*_*msk -1 c++ qt screen-capture terminal-emulator
我想在 Qt 中从 Linux 上的运行进程中获取输出。
我的代码如下所示:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qprocess.h>
#include <qthread.h>
QProcess process;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
process.start("htop");
connect(&process, SIGNAL(readyReadStandardOutput()), this, SLOT(getData()));
}
void getData(){
QByteArray out;
out = process.readAllStandardOutput();
}
MainWindow::~MainWindow()
{
delete ui;
}
Run Code Online (Sandbox Code Playgroud)
但是我想从htop获取实时(更改)输出并将其保存到字符串。
Because the sample "htop" interests me, here's a hint.
htop is an "interactive" terminal application (using curses to "draw" a animated terminal image), as opposed to a run-of-the-mill UNIX-style filter (that takes input from a file-like source, and provides a sequential output stream to any file-like destination).
So it's not quite as easy to "capture" it live. In fact the only class of application that supports this is called a terminal emulator. Let's use tmux as a terminal emulator that is capable of writing "screenshots" to a file.
$ SESS_ID=$(uuidgen)
$ COLUMNS=80 LINES=25 tmux new-session -s "$SESS_ID" -d htop
Run Code Online (Sandbox Code Playgroud)
This starts a new session, running htop in the background. We generated a unique ID so we can control it without interfering with other tmux sessions. You can list it to check what the name is:
$ tmux list-sessions
a9946cbf-9863-4ac1-a063-02724e580f88: 1 windows (created Wed Dec 14 21:10:42 2016) [170x42]
Run Code Online (Sandbox Code Playgroud)
Now you can use capture-pane to get the contents of that window:
$ tmux capture-pane -t "$SESS_ID" -p
Run Code Online (Sandbox Code Playgroud)
In fact, running it repeatedly gives you a (monochrome) live mirror of the htop (every 2 seconds, by default):
$ watch tmux capture-pane -t "$SESS_ID" -p
Run Code Online (Sandbox Code Playgroud)
Now. You want color, of course. Use ansifilter:
$ tmux capture-pane -t "$SESS_ID" -p -e | ansifilter -H > shot.html
Run Code Online (Sandbox Code Playgroud)
Voila. I'm sure Qt has a nice Widget to display HTML content. I tested it running this
$ while sleep 1; do tmux capture-pane -t "$SESS_ID" -p -e | ansifilter -H > shot.html; done
Run Code Online (Sandbox Code Playgroud)
并shot.html在我的浏览器中打开。每次我重新加载时,我都会得到一个最新的屏幕截图:
哦,PS 完成后使用
$ tmux kill-session -t "$SESS_ID"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
702 次 |
| 最近记录: |