- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个程序,在启用了 C++ 11 的 Ubuntu 16.04 Qt 5.5.1 上使用 QProcess 框架在 Qt 中运行进程。我将流程输出流定向到 QTextEdit。
我想通过使用嵌入式 ANSI 转义颜色序列将此输出着色为使用 native 终端解释的相同颜色。但是,我无法解析转义序列,因为它们似乎从 QProcess 输出中丢失了。我最初以为 QString 正在剥离它们,但经过一些测试后我不相信是这样。
我找到了 some information如果我可以将转义序列保留在 QProcess 输出中,请指出 ANSI 转义颜色解释方向。
这是我在 Qt 代码中所做的示例项目。
源文件...
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QProcess>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList input = {"gcc will_not_build.c"};
QProcess * proc = new QProcess();
proc->setReadChannel(QProcess::StandardOutput);
proc->setProcessChannelMode(QProcess::MergedChannels);
proc->setWorkingDirectory("/path/to/test/c/file/");
//Start bash
proc->start("bash");
proc->waitForStarted();
// Write as many commands to this process as needed
foreach(QString str, input){
proc->write(str.toUtf8() + "\n");
proc->waitForBytesWritten(-1);
}
// Let bash close gracefully
proc->write("exit $?\n");
proc->waitForBytesWritten(-1);
proc->closeWriteChannel();
proc->waitForFinished();
proc->waitForReadyRead();
QByteArray read_data = proc->readAll();
// The use of tr(read_data) also works here.
QString output = tr(read_data);//QString::fromStdString (read_data.toStdString ());
proc->closeReadChannel(QProcess::StandardOutput);
proc->close();
delete proc;
// Add the output to the text box
ui->textEdit->append (output);
}
MainWindow::~MainWindow()
{
delete ui;
}
头文件...
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
表单文件...
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>33</x>
<y>19</y>
<width>331</width>
<height>211</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>19</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
C 源文件...
int main(){
// Intentionally will not build
I will not build :)
}
我的输出看起来像这样:
QProcess gcc 输出
native Linux 终端的输出如下所示:
带颜色的 Linux 终端 gcc 输出
有谁知道如何在 QProcess 输出中保留 ANSI 转义颜色序列以便模拟 Linux 终端颜色?
作为旁注,我在 Qt Creator 源代码中进行了深入研究,发现有一个类可以将 ANSI 转义颜色转换为 Rich Text 颜色,所以我知道有人走上了这条路。然后,在构建项目时,Qt Creator 出于某种原因不会在其自己的终端中为构建输出着色。
最佳答案
QProcess
不会干扰进程输出,只是 gcc
- 与许多其他发出彩色输出的程序一样 - 默认情况下仅当它发出颜色转义序列时检测到它正在 TTY 设备上写入。
如果您想禁用此启发式并要求始终生成彩色输出,则必须将 -fdiagnostics-color=always
选项添加到编译器命令行。
关于linux - 在 QProcess 输出中保留 ANSI 转义序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46371370/
第一次在stackoverflow上提问。而且我是一个中国女孩,如果我对这个问题的描述有太多的语法错误,以至于你不能轻易理解,我很抱歉。下面是我的问题:头文件: class AdbDriver : p
我正在使用 QProcess 来运行其他程序。但是当我在调用 QProcess.start() 后退出我的应用程序时,它在调试控制台中说: QProcess: Destroyed while proc
我想运行一个 qprocess(程序 adb),当该过程完成时将结果返回给调用函数。但是,adb 很有可能会发现自己陷入循环,将诸如“ADB 服务器未确认”之类的错误消息打印到标准输出,而永远不会完成
我有一个 PyQt5 GUI 应用程序,想要执行外部程序并在 QTextEdit 小部件上显示外部程序的 stdout、stderr 和 stdin。我已经设法对标准输出和标准错误执行此操作。我需要有
Qt 文档给出了这样的解释: QProcess::开始: Starts the given program in a new process, if none is already running,
我阅读了一些文档,但对我来说还不够清楚。我知道“结束”进程和 kill() 都是为了强制它结束,但是 terminate() 应该做什么呢? 最佳答案 不知道你有没有写清楚: void QProces
我正在尝试将序列化图像传递到我在 Qt 程序中启动的进程。但是,这样做时我收到此错误消息: QObject::connect: Cannot queue arguments of type 'QPro
平台:Windows10我使用 QProcess::start 执行 Python 文件(在同一个目录中),但是我 无法从 readAllStandardOutput 函数获取结果。 Python文件
我有以下代码: proc = new QProcess(); proc->startDetached("C:\\ffmpeg.exe", QStringList() <<"-i"<< "C:\\pic
平台:Qt 4.8.2,Win 7 请考虑以下逻辑流程: 1. App started 2. functionA() triggered 3. the app periodically capture
A 在尝试阻止我的 QProcess 时遇到问题在它的父析构函数中。这是我的代码: AbstractProcess::~AbstractProcess() { if((m_process->s
我有一个必须暂停和恢复的 QProcess。我用 kill(pid_t(process->pid()), SIGSTOP); 和 kill(pid_t(process->pid()), SIGCONT
我尝试实现 subprocess Popen blocking PyQt GUI 的建议但似乎 onFinished 函数永远不会被调用。 class MyApp(QtWidgets.QMainWin
我有以下使用 QProcess 运行可执行文件的代码。代码运行良好,新的可执行文件运行正常。 QString fileName = ui.textBrowser_csvFile->toPlainTex
我想创建一个 QProcess 并在后台运行它。我有一个调度程序,它维护要作为 QProcesses 启动的作业队列。这些 QProcess 具有在 lsf 机器中运行的命令。要求是,一旦 QProc
我只是想通过以下源代码用 QProcess 创建一个文件: void Processmethod() { QDialog *ProcessMessage = new QDialog;
我正尝试在 QProcess 下的 Raspberry Pi (Raspbian) 中启动 CEC 命令。 如果我在我的 shell 中执行这个: echo 'standby 0' | cec-cli
一段时间以来,我一直在努力解决这个基本问题。我正在尝试从一个线程启动一个 QProcess。启动进程工作正常并且进程运行正常,但我的问题是 finished() 信号永远不会发出。 这是我的例子: 我
在我的 Qt C++ 程序中,我创建了一个进程,如下所示: myProcess = new QProcess(); myProcess->start(programpath, arguments);
出于某种原因,我无法在 Ubuntu 上使用 QProcess 启动进程,我不明白为什么... int main(int argc, char *argv[]) { //Run the pro
我是一名优秀的程序员,十分优秀!