【Qt】Linux代码中调用shell命令

1. system()

  • 来源:Linux C函数库
  • 阻塞
  • 程序无法获取指令返回的内容,只返回成功与否
int ret = system("ls");
qDebug()<<"ret:" <<ret;

2. QProcess::start(),QProcess::waitForFinished()

  • start()非阻塞,但是waitForFinished()阻塞
  • 有时也可以和connect finished联合使用
#include 
 
QProcess process;
process.start("ls",QStringList()<< "/usr/local");
process.waitForFinished();
//    connect(&process, QOverload::of(&QProcess::finished),
//        [=](int exitCode, QProcess::ExitStatus exitStatus){  qDebug()<<"-----"; });
QString result = process.readAllStandardOutput();
qDebug()<<"result:\n" <<result;

//结果
result:
"bin\ncuda\ncuda-12\ncuda-12.1\ndoc\n"

3. QProcess::execute()

  • 阻塞
  • 执行结果与system()类似
  • 执行结果无法在程序中获得
QProcess process;

int ret = process.execute("lsb_release", QStringList()<< "-d");

qDebug()<<"ret:" <<ret;

你可能感兴趣的:(Linux,Qt,qt,linux,开发语言)