您可以将其设置为阻塞或非阻塞。这取决于您是要阻止主进程还是以异步模式在后台运行 shell 脚本。
此外,由于您不需要输出,因此您甚至不需要在此处实例化,只需使用静态方法即可。
#include <QString>
#include <QFileDialog>
#include <QProcess>
#include <QDebug>
...
// Get this file name dynamically with an input GUI element, like QFileDialog
// or hard code the string here.
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Script"), "/", tr("Script Files (*.sh)"));
if (QProcess::execute(QString("/bin/sh") + fileName) < 0)
qDebug() << "Failed to run";
Run Code Online (Sandbox Code Playgroud)
#include <QString>
#include <QFileDialog>
#include <QProcess>
#include <QDebug>
...
// Get this file name dynamically with an input GUI element, like QFileDialog
// or hard code the string here.
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Script"), "/", tr("Script Files (*.sh)"));
// Uniform initialization requires C++11 support, so you would need to put
// this into your project file: CONFIG+=c+11
if (!QProcess::startDetached("/bin/sh", QStringList{fileName}))
qDebug() << "Failed to run";
Run Code Online (Sandbox Code Playgroud)
您可以运行shell或bash将脚本作为参数传递:
QProcess process;
process.startDetached("/bin/sh", QStringList()<< "/Path/to/myScript.sh");
Run Code Online (Sandbox Code Playgroud)