【问题标题】:Dynamically set Label's "id" property动态设置 Label 的“id”属性
【发布时间】:2014-05-17 10:48:40
【问题描述】:

我正在使用 QML 语言为 SailfishOS 开发应用程序。 我想使用if 条件动态设置标签的id 属性。

这是我的代码:

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

此代码被放入我的CoverPage.qml 文件中,该文件在后台显示应用程序封面上的内容。 这样一来,封面就变成了黑色,什么都没有显示。

在 QML 中可以做到这一点吗?

提前致谢!

【问题讨论】:

标签: qml qt5 sailfish-os


【解决方案1】:

Qt doc 这么说。

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

您不能在运行时设置 QML 组件的 id。(如果我错了,请纠正我)。您可能会发现 objectName 属性很有用。但我不明白您为什么要尝试分配动态 ID。

【讨论】:

    【解决方案2】:

    我有一个用例,其中使用动态/特定 id 可能很有用。 id可以用Gammaray查看,有助于调试。

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

    但据我所知,这是不可能的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      相关资源
      最近更新 更多