Active Topics

 


Reply
Thread Tools
flotron's Avatar
Posts: 418 | Thanked: 506 times | Joined on Jan 2012 @ Argentina
#1
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?
 

The Following User Says Thank You to flotron For This Useful 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:
flotron's Avatar
Posts: 418 | Thanked: 506 times | Joined on Jan 2012 @ Argentina
#3
Wow i thought that QML was more complete

Thank you marxian, i'm going to try what happens
 
EIPI's Avatar
Posts: 794 | Thanked: 784 times | Joined on Sep 2007 @ /Canada/Ontario/GTA
#4
And this is just what I was looking to do with my next app! Will try it out!
__________________
Mobile Tablets Blog
Follow me on
Twitter

 
flotron's Avatar
Posts: 418 | Thanked: 506 times | Joined on Jan 2012 @ Argentina
#5
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();
}

Last edited by flotron; 2012-10-26 at 00:25.
 
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#6
Originally Posted by flotron View Post
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()'.
__________________
'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-26 at 00:39.
 

The Following 2 Users Say Thank You to marxian For This Useful Post:
flotron's Avatar
Posts: 418 | Thanked: 506 times | Joined on Jan 2012 @ Argentina
#7
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)"?
 
Posts: 2,290 | Thanked: 4,134 times | Joined on Apr 2010 @ UK
#8
Originally Posted by flotron View Post
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.
__________________

Wiki Admin
sixwheeledbeast's wiki
Testing Squad Subscriber
- mcallerx - tenminutecore - FlopSwap - Qnotted - zzztop - Bander - Fight2048 -


Before posting or starting a thread please try this.
 

The Following User Says Thank You to sixwheeledbeast For This Useful Post:
flotron's Avatar
Posts: 418 | Thanked: 506 times | Joined on Jan 2012 @ Argentina
#9
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's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#10
Originally Posted by flotron View Post
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).
__________________
'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
 

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


 
Forum Jump


All times are GMT. The time now is 16:15.