maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   How to run a sh script from QML button? (https://talk.maemo.org/showthread.php?t=87580)

flotron 2012-10-25 22:42

How to run a sh script from QML button?
 
Hi i'm beginning with QML and i would like to know how i execute a script.sh with a QML button.

I have the script.sh:
Code:

#!/bin/sh

gconftool -s --type=string /meegotouch/theme/name blood
/sbin/initctl restart xsession/mthome
exit 0

And i have the QML:
Code:

    ToolButton {
        id: myButton
        x: 121
        y: 410
        width: 119
        height: 62
        onClicked: {
            HERE I WANT TO EXECUTE THE SCRIPT
        }
    }
}

Is it possible?
I must package the sh file with QT or i must write the sh script inside QML?

marxian 2012-10-25 23:19

Re: How to run a sh script from QML button?
 
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

flotron 2012-10-25 23:47

Re: How to run a sh script from QML button?
 
Wow i thought that QML was more complete :)

Thank you marxian, i'm going to try what happens

EIPI 2012-10-26 00:18

Re: How to run a sh script from QML button?
 
And this is just what I was looking to do with my next app! Will try it out!

flotron 2012-10-26 00:22

Re: How to run a sh script from QML button?
 
i'm stuck with this errors:

main.cpp: In function 'int main(int, char**)':
main.cpp:16: error: 'view' was not declared in this scope


This is my main.cpp

Code:

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "scriptlauncher.h"
#include "qdeclarativecontext.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/untitled/main.qml"));
    viewer.showExpanded();

    ScriptLauncher launcher;
    QDeclarativeContext *context = view->rootContext();
    context->setContextProperty("scriptLauncher", &launcher);

    return app->exec();
}


marxian 2012-10-26 00:36

Re: How to run a sh script from QML button?
 
Quote:

Originally Posted by flotron (Post 1285613)
i'm stuck with this errors:

main.cpp: In function 'int main(int, char**)':
main.cpp:16: error: 'view' was not declared in this scope


This is my main.cpp

Code:

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "scriptlauncher.h"
#include "qdeclarativecontext.h"

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/untitled/main.qml"));
    viewer.showExpanded();

    ScriptLauncher launcher;
    QDeclarativeContext *context = view->rootContext();
    context->setContextProperty("scriptLauncher", &launcher);

    return app->exec();
}


'view' is the QDeclarativeView/QmlApplicationViewer, so in your case, it needs to be 'viewer.rootContext()' instead of 'view->rootContext()'.

flotron 2012-10-26 02:18

Re: How to run a sh script from QML button?
 
Ok! no errors.

Now, where i place the "script.sh" file? under QML "Other files"?

And then it executes with "onClicked: scriptLauncher.launchScript()"?
or i must add for ex: "onClicked: scriptLauncher.launchScript(/opt/something/string.sh)"?

sixwheeledbeast 2012-10-26 06:40

Re: How to run a sh script from QML button?
 
Quote:

Originally Posted by flotron (Post 1285630)
Ok! no errors.

Now, where i place the "script.sh" file? under QML "Other files"?

And then it executes with "onClicked: scriptLauncher.launchScript()"?
or i must add for ex: "onClicked: scriptLauncher.launchScript(/opt/something/string.sh)"?

I would add a folder called script to the package and put your script in there. You will need to add this to the package in .pro file.

AFAIU the m_procces location is done in the slot (were "sh script.sh" is in Marxian's example)
This is were you need to put "sh /opt/mypackage/script/myscript.sh"
onClicked can be left alone.

flotron 2012-10-26 16:08

Re: How to run a sh script from QML button?
 
i added the script folder with script.sh inside but (with right click to project, add existing file) when i install i explore the opt folder on phone and i only see "bin" and "qml" folders.

This is the pro file:
OTHER_FILES += \
qtc_packaging/debian_harmattan/rules \
qtc_packaging/debian_harmattan/README \
qtc_packaging/debian_harmattan/manifest.aegis \
qtc_packaging/debian_harmattan/copyright \
qtc_packaging/debian_harmattan/control \
qtc_packaging/debian_harmattan/compat \
qtc_packaging/debian_harmattan/changelog \
script/script.sh

marxian 2012-10-26 16:36

Re: How to run a sh script from QML button?
 
Quote:

Originally Posted by flotron (Post 1285927)
i added the script folder with script.sh inside but (with right click to project, add existing file) when i install i explore the opt folder on phone and i only see "bin" and "qml" folders.

This is the pro file:
OTHER_FILES += \
qtc_packaging/debian_harmattan/rules \
qtc_packaging/debian_harmattan/README \
qtc_packaging/debian_harmattan/manifest.aegis \
qtc_packaging/debian_harmattan/copyright \
qtc_packaging/debian_harmattan/control \
qtc_packaging/debian_harmattan/compat \
qtc_packaging/debian_harmattan/changelog \
script/script.sh

In your .pro file, you need something like this:

Code:

script.path = /opt/<application_name>/script
script.files += script/script.sh

INSTALLS += script

The example assumes that the script folder is a direct subfolder of your main project folder. In the ScriptLauncher, you set the command to "sh /opt/<application_name>/script/script.sh", or you could change the launchScript() method to take a QString argument, and you would then be able to launch different scripts from QML (there is also the option of subclassing QProcess and exposing its methods/properties to QML, which would ultimately be more flexible).


All times are GMT. The time now is 06:04.

vBulletin® Version 3.8.8