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. QML - reference to image element
QtWS25 Last Chance

QML - reference to image element

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 2.6k 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
    ben80
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      dcape
      wrote on last edited by
      #2

      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
      1
      • X Offline
        X Offline
        xargs1
        wrote on last edited by
        #3
        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
        0
        • B Offline
          B Offline
          ben80
          wrote on last edited by
          #4

          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
          0
          • X Offline
            X Offline
            xargs1
            wrote on last edited by
            #5

            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
            0
            • B Offline
              B Offline
              ben80
              wrote on last edited by
              #6

              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
              0
              • X Offline
                X Offline
                xargs1
                wrote on last edited by
                #7

                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
                0
                • X xargs1

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

                  B Offline
                  B Offline
                  ben80
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

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