Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Reference a QML object id from a JavaScript function



  • I have a QML object with an id and I want to change some color inside the QML object from a function.
    What is the syntax?

    Rectangle {
    id: rect
    .......
    }
    Something like this:
    function changeColor() {
    .....
    rect.color = "black"
    }

    Can't figure out how is done, any suggestions?
    thx



  • @biskero That code should work, do you have an error message?



  • @biskero said in Reference a QML object id from a JavaScript function:

    Can't figure out how is done, any suggestions?

    Why do you say this does not work? You only have to ensure that the is in the scope of your function
    ==> https://doc.qt.io/qt-5/qtqml-documents-scope.html



  • I get "Unreachable (M28)" message at the line where I do rect.color = "black";

    This happens if I keep the function within the Rectangle Object or outside.
    That's why I am confused.

    Just to clarify what I am trying to do.
    I have a slider that changes the color of another object.
    Once I change the color, I store that color RGB in a db.
    Once I restart the app, I want to set the slider to the stored RGB color, and the slider to the same RGB value.

    So I have the function, called rgbToHsl, that gets as input the HEX value of color, it converts to HSL, and returns the value to the slider.
    But some how when I do reference the slider and I assign the value, I get that error.
    The slider has property color colorValue, so I am trying to set the colorValue like

    rect.color = colorValue


Log in to reply