gpt4 book ai didi

c++ - 获取 Qt 应用程序输出到 Qwidget C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:46 27 4
gpt4 key购买 nike

我构建了一个通过 C++ Qt GUI 执行 linux 命令的应用程序我从文件中读取并正常显示输出但有时文件的输出是数据=“”以及 - 在终端中正常显示 - 在应用程序输出中显示的输出所以我想将应用程序输出到 Qwidget,例如 QTextEdit

喜欢
cat::/home/user/Desktop: 是广告目录,我使用的功能是

QString operation :: commands(std::string usercommand){
const char * convertor = userCommand.c_str();
string data;
FILE *f =popen(convertor,"r");
char buffer [1024];
while (fgets(buffer,sizeof(buffer)-1,f)!=NULL){data=data+buffer;}
pclose(f);
QString returning = QString::fromStdString(data); return returning; }

最佳答案

如果你正在使用 Qt,你应该使用 QProcess

QString operation::commands(QString program)
{
QProcess process;
process.start(program);
while (process.waitForFinished()){
;
}
QString resp = QString::fromLocal8Bit(process.readAllStandardOutput());
QString error = QString::fromLocal8Bit(process.readAllStandardError());
return resp + error;
}

使用:

QString usercommand = "cat /home/user/Desktop";
commands(usercommand);

关于c++ - 获取 Qt 应用程序输出到 Qwidget C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43748212/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com