I want to run a system command from inside my application. It's suppose to run a command on a remote server using SSH. But that's not really the point. The point is that I don't know how to run any kind of command from the app. I asked in the mail list and they referred me to build a QML extension using C++. But I don't know C++ and it seems that I have to learn so much only to run a simple command.
In Python (as in PHP) it's easy to run a system command. Is there any other way of doing it in my Touch app, or is there someone that could help me even more? Or perhaps have a better solution to my problem?
This isn't something that QML supports, the typical answer is to write a C++ plugin to handle that kind of thing.
However, the SDK team is planning out various extensions to provide to QML app developers, and this may be something that they implement in a generic plugin that you can use.
Update: For 14.04 see the much simplified answer by int_ua.
Original Text:
At http://talk.maemo.org/showthread.php?t=87580 there is a basic overview of how to add the extension to QML. I decided to give it a shot using the ubuntu-sdk instead, which is slightly different. I'll document below.
For this project I selected Ubuntu Touch/Simple UI with C++ Backend in QtCreator. This creates a project with two separate parts, the backend and the touchui frontend written in QML. To the backend we are going to add two files for the Launcher class.
launcher.h:
launcher.cpp:
This class simply uses QProcess to execute a program, waits for it to finish, reads its stdout, and returns it as a string.
Next we need to modify backend/backend.cpp to include the class. This requires two lines. Append an include:
and in BackendPlugin::registerTypes add a line:
There should already be a line for MyType, which is the included example. After this we should be able to build the backend. The only thing that remains is to use it in the main.qml file. For this I added a line:
and to the Button's onClick handler, set:
At this point all that remains is to start it up and test it out. Here is where I ran into a problem, since QtCreator doesn't seem to set everything up properly by default. As I workaround, in the terminal navigate to your QtCreator project directory and:
Then copy the libUbuntuExample.so file from ProjectBuildDir/backend to Ubuntu/Example, and the qmldir file from ProjectName/backend/qmldir. Then you can run:
I'm sure there is probably a simple way to rig this all up so Build/Run just works.
Ubuntu 14.04
The concept of QProcess Launcher type is now working without problems in Trusty with
ubuntu-sdk-team
PPA. Just createQML Extension Library + Tabbed UI
project (don't use hyphens in project name yet), replace contents ofmytype.h
mytype.cpp
and change
qmlRegisterType
in thebackend.cpp
toNext, just clean all
MyType
remains from QML files and addwherever you like and
in the beginning.
Optional
I also use this wrapper:
If you need root access, add
pkexec
.You really don't need to know a lot about c++ to get access to terminal commands. Just put the following in any file ending with .cpp, for instance runPython.cpp.
All you have to find out now is how to get the c++ code running in QML but I'm sure that's very well documented.
Note that you can add any linux command you like by following the same syntax
system("linux command");
.Hope this helps!
You shall use PlasmaCore.DataSource with the engine "executable", as in this example.
Mind that the call is asynchronous. Hence the command sends a signal when it finishes, and a different part of the code picks the signal and continues with the next step you want to perform.