Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QML - reference to image element

    QML and Qt Quick
    3
    8
    2160
    Loading More Posts
    • 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
      ben80 last edited by

      Hi everyone,

      Is it possible in qml to use some kind of a reference to an image element and then use this reference to set properties?

      I have two image elements (id:img1, id:img2).
      Depending on the value of a signal i would like to change several properties, either of img1 or of img2.

      Example:
      imgX = <Referenc_to_img1>
      imgX.width : 100
      imgX.x : 200
      imgX.source : <filename>
      ...

      Thanks for any help,

      Ben

      1 Reply Last reply Reply Quote 0
      • D
        dcape last edited by

        Yep, you can do: var imgX = img1 ...assuming you're setting imgX in the same function you're manipulating the properties.
        Here's an example but using rectangles:

        function changeImage() {
            var imgX = img1;
            imgX.width = 150;
            imgX.height = 50;
        }
        
        Rectangle {
            id: img1
            color: "red"
            width: 100
            height: 100
        }
        Rectangle {
            id: img2
            color: "blue"
            x: 200
            width: 100
            height: 100
        }
        
        1 Reply Last reply Reply Quote 1
        • X
          xargs1 last edited by

          function f() {
              var imgX;
              imgX = img1;
              imgX.source = filename;
          }
          

          Alternately, imgX could be a property if you need it to have wider scope:

          property var imgX

          1 Reply Last reply Reply Quote 0
          • B
            ben80 last edited by

            Cool, thanks. The first solution works well.

            When using the "property" solution, why does the following code not work?
            -> Error while compiling: "Cannot assign a value directly to a grouped property"

            property var idX
            idX : rectRed
            idX.color : "green" -> ERROR

            Rectangle{
                id : rectRed
                color: "red"
            }
            
            1 Reply Last reply Reply Quote 0
            • X
              xargs1 last edited by

              We can't tell what you're doing from a couple of lines of code with no context. Where are you declaring the property and trying to do "idX.color: "green"? The colon is a binding operator, not an assignment; you can't use it in place of an = operator everywhere.

              1 Reply Last reply Reply Quote 0
              • B
                ben80 last edited by

                I'm sorry, i wanted to keep the post short. Here comes my testcode.
                Thanks for your help,
                Ben

                import QtQuick 2.3
                import QtQuick.Window 2.2

                Window {
                visible: true
                width: 360
                height: 360

                Rectangle{
                    id : top
                    anchors.fill: parent
                
                    property var idX
                    idX : rectRed
                    idX.color : "green"
                
                     Rectangle{
                        id : rectRed
                        width : 100
                        height: width
                        color: "red"
                    }
                }
                

                }

                1 Reply Last reply Reply Quote 0
                • X
                  xargs1 last edited by

                  You can do "idX.color = "green" in a JavaScript context, but I don't think you can do a binding like that.

                  B 1 Reply Last reply Reply Quote 0
                  • B
                    ben80 @xargs1 last edited by

                    ok, thanks a lot.
                    The variable inside a js function works well for me.
                    Ben

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post