0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

when i write Item { var oneVar =10; .... rest of code} then it gives me Syntax error on deceleration var. where can i define global variables for java script? and is it possible to load an QML page from the JavaScript.
You don't use "var", instead you should use "property". "var" is can be used only inside of function or in separate JS file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.