View Single Post
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#2
You can execute scripts from QML via QProcess. To do this, you first have to write a wrapper class for QProcess and using the Q_PROPERTY macro to make properties available to QML. You can then either create an instance of this class from the C++ side and make it available via QDeclarativeContext::setContextProperty(), or register it as a QML type to allow instances of the class to be created in QML.

I have already written a QProcess wrapper class called QDeclarativeProcess for my Qt Components Hildon package. The source code is here: https://github.com/marx1an/qt-compon...components/src (qdeclarativesettings.h and qdeclarativesettings.cpp). If you include these two files in your project, you can use the QProcess API via QML as follows:

In your main.cpp file:

Code:
#include "qdeclarativesettings.h"
#include <qdeclarative.h> // you might not need this depending on your existing #includes

qmlRegisterType<QDeclarativeProcess>("com.components.processes", 1, 0, "Process");

qmlRegisterUncreatableType<QDeclarativeProcessEnums>("com.components.processes", 1, 0, "Processes");
The "com.components.processes" can be replaced with whatever import URI you prefer.

In QML:

Code:
import com.components.processes 1.0

Process {
    id: process

    command: // command to launch the script.
}

Button {
    id: button

    text: "Launch script"
    onClicked: process.start()
}
You can see a more detailed example of how it's used from QML here: https://github.com/marx1an/qt-compon...rocessPage.qml
__________________
'Men of high position are allowed, by a special act of grace, to accomodate their reasoning to the answer they need. Logic is only required in those of lesser rank.' - J K Galbraith

My website

GitHub

Last edited by marxian; 2013-01-28 at 20:50.
 

The Following 3 Users Say Thank You to marxian For This Useful Post: