Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching a shell script when Button Pressed on GUI made with Qt

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.

like image 377
RicoRicochet Avatar asked May 20 '14 17:05

RicoRicochet


3 Answers

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.

Blocking code

#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";

Non-blocking

#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";
like image 195
lpapp Avatar answered Sep 19 '22 09:09

lpapp


You can run shell or bash passing your script as a parameter :

QProcess process;
process.startDetached("/bin/sh", QStringList()<< "/Path/to/myScript.sh");
like image 41
Nejat Avatar answered Sep 22 '22 09:09

Nejat


Use one of QProcess::startDetached overloads: http://qt-project.org/doc/qt-4.8/qprocess.html#startDetached. This one is the most complete:

bool QProcess::startDetached ( const QString & program, const QStringList & arguments, const QString & workingDirectory, qint64 * pid = 0 ) [static]

Usage example:

QProcess::startDetached("/.../script", QStringList { "-i", "a.txt", ... });

QStringList constructor above uses C++11 feature. You can always construct QStringList in a C++03-supported way, e.g. QStringlist list; list << "-i" << "a.txt"; Or if you do not need to pass any parameters to your script, just call QProcess::startDetached("/.../script").

If you need to receive your script's output, you can create QProcess object and call start. It is well described here: http://qt-project.org/doc/qt-4.8/qprocess.html#details

like image 25
vedg Avatar answered Sep 23 '22 09:09

vedg