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. How to change img of Rect outside of the Rect?
Qt 6.11 is out! See what's new in the release blog

How to change img of Rect outside of the Rect?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 457 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.
  • qazaq408Q Offline
    qazaq408Q Offline
    qazaq408
    wrote on last edited by
    #1
    //ChangeImg.qml
    Rectangle {
        width : 100;
        height : 100
        Image{
            id:imgx
            source : "A1.png"
            anchors.fill : parent
        }
    }
    
    //mian.qml
    Column{
        ChangeImg{
            id:a1
            width : 100 
            height : 100
        }
        ChangeImg{
            id:a2
            width : 100 
            height : 100
        }
        Component.onCompleted : {
            //a2.Image.source = "A2.png";   #1
            //ChangeImg.imgx.source = "A2.png";   #2
            //a1.imgx.source = "know.png";   #3
        }
    }
    

    I want to change IMG of Rect in Column because of some special reason,but it doesn't work. When i use cmd

    qml main.qml
    

    #1 Nothing happend,the img of Rect (whether a1 or a2) is "A1.png"

    #2 "file:///D:/QML/TestImg/main.qml:16: TypeError: Value is undefined and could not be converted to an object"

    #3 The same as #2

    so, how to change IMG of Rect outside of the Rect?

    1 Reply Last reply
    0
    • johngodJ Offline
      johngodJ Offline
      johngod
      wrote on last edited by
      #2

      Hi
      You have to create a alias to be able to access the image from outside

      Rectangle {
          width : 100;
          height : 100
          property alias imgx: imgx
          Image{
              id:imgx
              source : "A1.png"
              anchors.fill : parent
          }
      }
      
      qazaq408Q 2 Replies Last reply
      2
      • johngodJ johngod

        Hi
        You have to create a alias to be able to access the image from outside

        Rectangle {
            width : 100;
            height : 100
            property alias imgx: imgx
            Image{
                id:imgx
                source : "A1.png"
                anchors.fill : parent
            }
        }
        
        qazaq408Q Offline
        qazaq408Q Offline
        qazaq408
        wrote on last edited by
        #3

        @johngod
        Thank You Very Much

        1 Reply Last reply
        0
        • qazaq408Q qazaq408 has marked this topic as solved on
        • johngodJ johngod

          Hi
          You have to create a alias to be able to access the image from outside

          Rectangle {
              width : 100;
              height : 100
              property alias imgx: imgx
              Image{
                  id:imgx
                  source : "A1.png"
                  anchors.fill : parent
              }
          }
          
          qazaq408Q Offline
          qazaq408Q Offline
          qazaq408
          wrote on last edited by
          #4

          @johngod
          hi,i met another problem.......

          In fact,i need change IMG by JS function,yestaday,i try to use "alias" and make sure it can change IMG outside Rect,but now ,i write a JS funtion look like

          function changeImg(){
              //do something.....
              a1.imgx = "A2.png";
          }
          

          And then ,when execute main.qml,complier tell me
          " file:///E:/QML/ChangeImg.js:23: TypeError: Cannot assign to read-only property "imgx"
          "

          Could you help me again to solve this problem?

          1 Reply Last reply
          0
          • qazaq408Q qazaq408 has marked this topic as unsolved on

          • Login

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