c++ - 使用 QProcess 执行 CMD 命令并将其保存在 QString 中
问题描述
我想执行 cmd 命令"wmic share get name"
,QProcess
然后将命令的结果保存在QString
变量中。此外,我想展示它QMessageBox
或......我该怎么做?
解决方案
你可以用QProcess
这个。假设我要执行g++
. 例子:
QProcess p;
p.setProgram("g++");
p.setArguments({"-O3", "filename.cpp"});
p.start();
// wait for the process to finish executing
// returns true on success
if (!p.waitForFinished()) {
qDebug() << "Failed to execute!!";
const QString error = p.readAllStandardError();
if (!error.isEmpty()) {
qDebug () << "Exit status: " << p.exitStatus() << ", Error: " << error;
}
return;
}
// read output
const QString output = p.readAllStandardOutput();
qDebug () << output;
// read error
const QString error = p.readAllStandardError();
if (!error.isEmpty()) {
qDebug () << error;
}
//do whatever you want with output
推荐阅读
- pyspark - 如何按元数据字段(例如通过 _id)过滤从 Elasticsearch 读取的 PySpark SQL 数据帧?
- r - 如何在传单中放大更精细的步骤?
- mysql - 如何总结每个终端代码的所有交易
- sql - Template_searchpath 在 Airflow 中给出 TemplateNotFound 错误并且找不到 SQL 脚本
- python - Kubernetes CronJob 运行 Python 脚本
- angular - Mat Tabs:在 selectedTabChange 中有没有办法使用选定的选项卡组件方法?
- python - 当未找到 eventStream 数据时,我的脚本崩溃
- sql - 在 Oracle SQL 中将两个报表合二为一
- css - 如何在 Blogger 中收紧页眉空间
- html - CSS 动画,仅在特定元素上渲染覆盖