1

I want to put a condition if the condition is true then show the images. But when i run this code i'm getting an error. It shows image is undefined here is my code sample

Row{
  spacing:5
  Component.onCompleted:{
          if( my condition is true ==true)
            {
            image.source="image_path"
            }
  }
Repeater{
   model: 4
 Image {
      id: image
      Layout.alignment: Qt.AlignHCenter
      fillMode: Image.PreserveAspectFit
    }
  }
}
2
  • The Image is inside a Repeater, which is basically a different scope, so from the main qml image is not known. Moreover, which of the 4 image would you like to address? If you show a bit more about your condition, an suited answer can be written
    – Amfasis
    Dec 11, 2020 at 12:28
  • suppose i have a condition is like if my password=='y' then i have to show the images. if i'm using a single image then it is working fine but inside a repeater it's not working Dec 11, 2020 at 12:40

1 Answer 1

2

Referencing an id inside Repeater does not work, since the Repeater breaks the scope a bit.

But if you turn around the logic it works: you can bind the source property inside the delegate to a new property on the main control (which I named control here):

Row {
    id: control
    spacing:5

    property bool show : false
    
    Repeater{
        model: 4
        Image {
            id: image
            Layout.alignment: Qt.AlignHCenter
            fillMode: Image.PreserveAspectFit
            source: control.show ? "image_path" : ""
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.