'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.
Solution 1:[1]
You can run shell or bash passing your script as a parameter :
QProcess process;
process.startDetached("/bin/sh", QStringList()<< "/Path/to/myScript.sh");
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nejat |
| Solution 2 | vedg |
