3

Please, is it possible to access and change a property of a qml element from javascript function? I read that document.getElementById() should do the trick, but when I try that I get:

ReferenceError: document is not defined

Test.qml

import QtQuick 2.0
import QtQuick.Controls 1.2
import Test.js as JS 

Rectangle{
    id: rect
    color: "red"

    onClicked: {
        JS.changeMe();
    }
}

Test.js

function changeMe(){
   //change the color of element "rect"
   //document.getElementById("rect")
}
1
  • QML is not like HTML document. QML elements are referenced by ids. Commented Jul 18, 2014 at 10:41

1 Answer 1

1

this worked:

Rectangle{
    id: rect
    color: "red"

    onClicked: {
        JS.changeMe(rect);
    }
}

function changeMe(rect){
   rect.color = "yellow";
}
Sign up to request clarification or add additional context in comments.

Comments

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.