我正在尝试从 Qt 应用程序中读取屏幕分辨率,但不使用 GUI 模块。
所以我尝试使用:
xrandr |grep \* |awk '{print $1}'
命令通过QProcess,但它显示警告并且不给出任何输出:
unknown escape sequence:'\\*'
重写它\\\*
没有帮助,因为它会导致以下错误:
/usr/bin/xrandr: unrecognized option '|grep'\nTry '/usr/bin/xrandr --help' for more information.\n
我该如何解决这个问题?
您必须使用 bash 并在引号中传递参数:
#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess process;
QObject::connect(&process, &QProcess::readyReadStandardOutput, [&process](){
qDebug()<<process.readAllStandardOutput();
});
QObject::connect(&process, &QProcess::readyReadStandardError, [&process](){
qDebug()<<process.readAllStandardError();
});
process.start("/bin/bash -c \"xrandr |grep \\* |awk '{print $1}' \"");
return a.exec();
}
Output:
"1366x768\n"
Or:
QProcess process;
process.start("/bin/bash", {"-c" , "xrandr |grep \\* |awk '{print $1}'"});
Or:
QProcess process;
QString command = R"(xrandr |grep \* |awk '{print $1}')";
process.start("/bin/sh", {"-c" , command});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)