View Single Post
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#2
You can't do it purely in QML. You would need to write a simple class in Qt that uses QProcess to launch the script.

scriptlauncher.h

Code:
#ifndef SCRIPTLAUNCHER_H
#define SCRIPTLAUNCHER_H

#include <QObject>
#include <QProcess>

class ScriptLauncher : public QObject
{

    Q_OBJECT

public:
    explicit ScriptLauncher(QObject *parent = 0);
    Q_INVOKABLE void launchScript();

private:
    QProcess *m_process;
};

#endif
scriptlauncher.cpp

Code:
#include "scriptlauncher.h"

ScriptLauncher::ScriptLauncher(QObject *parent) :
    QObject(parent),
    m_process(new QProcess(this))
{
}

void ScriptLauncher::launchScript()
{
    m_process->start("sh script.sh");
}
In the main.cpp file, get a pointer to the QDeclarativeContext object and register the ScriptLauncher as a context property:

Code:
#include "scriptlauncher.h"

...

ScriptLauncher launcher;
QDeclarativeContext *context = view->rootContext();
context->setContextProperty("scriptLauncher", &launcher);
The ScriptLauncher object is now accessible in QML as scriptLauncher:

Code:
ToolButton {
        id: myButton
        x: 121
        y: 410
        width: 119
        height: 62
        onClicked: scriptLauncher.launchScript()
}
I should also mention that you can use GConf in Qt directly. So, instead of using QProcess to launch a script, you could write a simple class that sets the GConf variable. This would be a better approach.

EDIT: Here's a nice example of using GConf in Qt/QML: http://harmattan-dev.nokia.com/docs/..._to_GConf.html
__________________
'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; 2012-10-25 at 23:32.
 

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