psp1
psp1

Reputation: 537

Integrating JavaScript and QML in the same file

can we integrate QML and JavaScript code in the same .qml file as we do it in HTML JS
For example:

//test.qml
import QtQuick 1.0
Item 
{
    function pollLoginStatus()
    {
        var receiveReq = new XMLHttpRequest();
    }
    ....
QML Code

I know that we have to use JavaScript using.

    import "jsCode.js" as jsCode

    Item
    {
         jsCode.jsfunction();

Is there anyother way of doing it, I want to integrate QML and JavaScript in the same file.

Thanks.

Upvotes: 0

Views: 1627

Answers (1)

Marco
Marco

Reputation: 2190

Yes, you can have JS in QMl files.

See: http://doc.qt.nokia.com/4.7-snapshot/qdeclarativejavascript.html

First example:

 Item {
     function factorial(a) {
         a = parseInt(a);
         if (a <= 0)
             return 1;
         else
             return a * factorial(a - 1);
     }

     MouseArea {
         anchors.fill: parent
         onClicked: console.log(factorial(10))
     }
 }

Keep the restrictions in mind: http://doc.qt.nokia.com/4.7-snapshot/qdeclarativejavascript.html#qml-javascript-restrictions

Upvotes: 2

Related Questions