Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Reference a QML object id from a JavaScript function

Reference a QML object id from a JavaScript function

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 316 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    biskero
    wrote on 13 Jan 2021, 22:30 last edited by biskero
    #1

    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

    E K 2 Replies Last reply 13 Jan 2021, 23:00
    0
    • B biskero
      13 Jan 2021, 22:30

      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

      E Offline
      E Offline
      eyllanesc
      wrote on 13 Jan 2021, 23:00 last edited by
      #2

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

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • B biskero
        13 Jan 2021, 22:30

        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

        K Offline
        K Offline
        KroMignon
        wrote on 14 Jan 2021, 06:42 last edited by
        #3

        @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

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        1
        • B Offline
          B Offline
          biskero
          wrote on 14 Jan 2021, 12:20 last edited by biskero
          #4

          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

          1 Reply Last reply
          0

          2/4

          13 Jan 2021, 23:00

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved