如何一次性有條件地設置屬性組的不同屬性?QML:有條件地設置屬性組的不同屬性
示例:假設有一個上下文屬性_context.condition
可用。鑑於這個價值,我想爲qml項目設置不同的錨點。
// Some item...
Rectangle {
id: square
width: 50
height: 50
// For simple properties this should work:
color: { if (_context.condition) "blue"; else "red" }
// But how to do it for complex properties like 'anchors'?
// Note that I set different properties for different values of the condition.
// Here is how I would do it, but this does not work:
anchors: {
if (_context.condition) {
// Anchors set 1:
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
bottomMargin: 20
} else {
// Anchors set 2:
verticalCenter: parent.verticalCenter
right: parent.right
rightMargin: 20
}
}
}
我在Qt 5.3中使用QtQuick 2.0。謝謝!
樣子horizontalCenter:_context.condition? parent.horizontalCenter:0等等 – 2014-10-02 16:06:46
好的,我不知道值0對應於「未設置」。我會檢查一下。麻煩,但至少有一些東西。謝謝! – normanius 2014-10-02 16:10:05
實際上,我不確定這個,只是嘗試:)也許,你可以分配'undefined'而不是0 – 2014-10-02 16:15:05