简体   繁体   English

如何使用动态更新属性更新 ListElement (QML) 值字段

[英]How to update ListElement (QML) value field with dynamically updating property

In the below code, the property __operatingModus_season updating every 30 seconds.在下面的代码中,属性__operatingModus_season每 30 秒更新一次。 But in frontend, I dont see any value or changes.但在前端,我看不到任何价值或变化。 Please give some heads up if i am wrong.如果我错了,请多多指教。

ListModel{
    id: tileListModelBetriebsmodus
    Component.onCompleted: {
        if(__is_automatik_mode_ON){
            tileListModelBetriebsmodus.append({"tileListSource": "../../images/HausScreen/operatingModeAuto.png",
                                                  "tileListText": getFrontendText("AUTO"),
                                                  "tileListValue": __operatingModus_season
                                              })
        }else{
            tileListModelBetriebsmodus.append({"tileListSource": "../../images/HausScreen/hamburgerfinger.png",
                                                  "tileListText": getFrontendText("MANUAL"),
                                                  "tileListValue": __operatingModus_season
                                              })
        }
    }
}

This way of adding items to the ListModel doesn't make them dynamic, the values are stored on the spot.这种将项目添加到 ListModel 的方式不会使它们成为动态的,这些值是当场存储的。

You might be able to Create Property Bindings like this:您可以像这样创建属性绑定

ListModel{
    id: tileListModelBetriebsmodus
    Component.onCompleted: {
            if(__is_automatik_mode_ON){
                tileListModelBetriebsmodus.append({"tileListSource": "../../images/HausScreen/operatingModeAuto.png",
                                                      "tileListText": getFrontendText("AUTO"),
                                                      "tileListValue": Qt.binding(function() { return __operatingModus_season })
                                                  })
            }else{
                tileListModelBetriebsmodus.append({"tileListSource": "../../images/HausScreen/hamburgerfinger.png",
                                                      "tileListText": getFrontendText("MANUAL"),
                                                      "tileListValue": Qt.binding(function() { return __operatingModus_season }
                                                  })
            }
        }
    }

Note I did not test this, if you would add a minimal working example I could have done so.注意我没有测试这个,如果你添加一个最小的工作示例,我可以这样做。

Amfasis' answer suggesting the use of Qt.binding() is on the right track, except Qt won't allow assigning a binding object directly to a ListElement. Amfasis 的回答表明使用Qt.binding()是在正确的轨道上,除了 Qt 不允许将绑定对象直接分配给 ListElement。 However, it allows assigning plain JS function objects.但是,它允许分配普通的 JS 函数对象。 So you have to do it in two steps instead:因此,您必须分两步完成:

Step 1: Define your dynamically evaluated ListElement role as a JS function (tip: you can also use the arrow syntax to make this more readable), for example:第 1 步:将动态评估的 ListElement 角色定义为 JS 函数(提示:您还可以使用箭头语法使其更具可读性),例如:

ListElement {
   dynamicLabel: () => config.useLabelA ? labelA : labelB
}

Step 2: Set up a binding inside your delegate between the target item and the model role:第 2 步:在目标项和模型角色之间的委托中设置绑定:

ListView {
    delegate: Text {
        Component.onCompleted: {
            text: Qt.binding(model.dynamicLabel)
        }
    }
}

Note the need to go through Component.onCompleted as you can't do text: Qt.binding(model.dynamicLabel) directly (QML spits out an error "Invalid use of Qt.binding() in a binding declaration.").请注意需要通过Component.onCompleted因为您不能直接执行text: Qt.binding(model.dynamicLabel) (QML 吐出错误“在绑定声明中使用 Qt.binding() 无效。”)。

You can't just do text: model.dynamicLabel either as that would only evaluate the JS function once at initial assignment, but not update if eg config.useLabelA changed.你不能只做text: model.dynamicLabel ,因为它只会在初始分配时评估一次 JS 函数,但如果config.useLabelA更改,则不会更新。 That's what you need the Qt.binding() wrapper for.这就是您需要Qt.binding()包装器的目的。

Building on Romain Pokrzywka's answer:基于 Romain Pokrzywka 的回答:

text: valuedUpdated() ? model.dynamicLabel : "" text: valuedUpdated() ? model.dynamicLabel : "" will be evaluated whenever value is updated. text: valuedUpdated() ? model.dynamicLabel : ""将在值更新时进行评估。 valuedUpdated() is a Q_PROPERTY, which returns true. valuedUpdated() 是一个 Q_PROPERTY,它返回 true。 Notify signal can be emitted whenever value is updated.每当值更新时,都可以发出通知信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM