Running a system command through QML - for Qt 5.4
-
Hi,
Is there anyone who's figured out how to run a system command from within QML, with the current QtQuick? All the online examples I've seen are pretty old, and don't seem to reflect the current reality in Qt 5.4.2
I've also tried to follow the online documentation, but it seems rather poor in explaining how one can connect external C++ objects into QML. From what I've seen, there are two general ways:
1 - Define a new QML type, and somehow make it visible in main.qml. It's the somehow I haven't figured out...
2 - add a property into some QQmlContext.
Anybody over at Digia can give me a working example, based on your "Hello, World" QtQuick 2.0 app that QtCreator gives me? Or, at least a clear sequence of steps to follow to add in new functionality?
Or, even better: add extra syntax that allows a user to execute a system() command from inside QML. Something like Python, Perl, and other high-level scripting languages do. No need to wrange with binding C++ classes either.
Thanks, C
-
Hi @Charles-M and Welcome,
Running System Commands:
First, to execute system commands in Qt you will need to useQProcess
which is a C++ API and thus needs to be done on C++ side. Then you have to somehow utilise this C++ code (from a class ofcourse ) from QML side. You have figured out correctly the 2 ways through which it can be done.Define a new QML type, and somehow make it visible in main.qml. It's the somehow I haven't figured out...
The idea here is to create a QObject-derived C++ class and expose it to QML using qmlRegisterType. After that you just need to import it on QML side and you are ready to access its functionality.
You can find more info here.add a property into some QQmlContext.
In this method too you need to create a QObject-derived C++ class and expose it to QML by setting it as property using setContextProperty.
Then to be able to access its functions you can either- declare it as a public slot
- make it
Q_INVOKABLE
More info here
A running example using
qmlRegisterType
is hereOr, even better: add extra syntax that allows a user to execute a system() command from inside QML. Something like Python, Perl, and other high-level scripting languages do. No need to wrange with binding C++ classes either.
Since QML doesnot have access to system directly there's no way to do it purely in QML. You will have to resort to C++ classes.