I have a shell script which takes backup on a remote server when executed on a touchscreen PC (Uubntu Lucid Lynx). Now I want this to be automated by making a small Button in a GUI application that is running on it. The application is built using Qt and C++.
Until now, I am able to use the QFileDialog
to open a folder browser and navigate to the .sh file, but is it possible to directly open the defined .sh file (i.e. by defining the name and location)?
There were some hints that QProcess should be used but I am kinda confused about its implementation. Thanks in advance.
You could make it either blocking or non-blocking. It depends on whether you would like to block your main process or run the shell script in the background in an async mode.
Also, since you do not need the output, you do not even need to instantiate here, just use the static methods.
#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";
#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";