Qt QML:有条件地设置属性组的不同属性
如何一次性有条件地设置属性组的不同属性 示例:假设有一个上下文属性Qt QML:有条件地设置属性组的不同属性,qt,qml,Qt,Qml,如何一次性有条件地设置属性组的不同属性 示例:假设有一个上下文属性\u 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
\u 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
}
}
}
我在Qt5.3中使用QtQuick 2.0。谢谢 您可以尝试以下方法(未测试):
此外,可以使用空大括号重置属性值:
Item {
property var first: {} // nothing = undefined
property var second: {{}} // empty expression block = undefined
property var third: ({}) // empty object
}
看起来像horizontalCenter:\u context.condition?parent.horizontalCenter:0等等,好的,我不知道值0对应“notset”。我去看看。笨重,但至少有些东西。谢谢!事实上,我对此不确定,请尝试:)也许,您可以指定“undefined”而不是0至少对我的示例有效。发布一个正确的答案,你将获得学分!:)我刚刚发现使用{}而不是“0”是一种更为中性的方式来表示“未定义”。除此之外,我接受你的解决方案。我有文档的更新链接,其中对锚的描述是准确的。。。请检查这个
Item {
property var first: {} // nothing = undefined
property var second: {{}} // empty expression block = undefined
property var third: ({}) // empty object
}