2

I'm developing an application for SailfishOS using the QML language. I want to dynamically set the id property of a Label by using an if condition.

This is my code:

    Label {
        id: {
            if(myBool == false) {
                thisText()
            } else {
                notThatText()
            }
        }
        width: parent.width
        horizontalAlignment: Text.AlignRight
        text: ""
        font.pixelSize: Theme.fontSizeLarge
    }

This code is placed into my CoverPage.qml file, the one that display things on the application's cover while in background. By doing this, the cover is simply black, nothing is displayed.

Is it possible in QML to do this?

Thanks in advance!

PeppeLaKappa
  • 139
  • 11
  • Why would you want that? Maybe your actual problem has a better solution than a dynamic id... http://meta.stackexchange.com/a/66378/237563 – GabrielF Apr 05 '14 at 00:15

2 Answers2

2

The Qt doc says this.

While it may look like an ordinary property, the id attribute is not an ordinary property attribute, and special semantics apply to it;

You cannot set the id of a QML component at runtime.(Correct me if I am wrong). You might find objectName property useful. But I don't understand why you are trying to assign dynamic id.

Programmer
  • 1,290
  • 2
  • 12
  • 16
1

I have a use case where use a dynamic/specific id could be useful. The id could be view with Gammaray, it can help for debugging.

GridLayout {
  id: layout
  Repeater {
    model: foo
    Bar {
      id: bar_X_Y // bar_{model.row}_{model.column}
    }     
  }
}

But as far as I know, it's not possible.