C
默认状态的限制
在Qt Quick Ultralite中,默认状态是固定的,仅包含在定义该QML对象的同一QML文件中定义的属性绑定。然而,在Qt Quick 中,默认状态是组件当前属性及其值的快照。当从其他状态切换回默认状态时,将恢复此快照。
// MyItem.qml Button { id: root states: [State { name: "state1"; PropertyChanges { target: root; x: 15 } }] x: 5 // defines the default state value onClicked: { root.x = 10 root.state = "state1" // x changes to 15 root.state = "" // x changes to 5, not to 10 } } // Other.qml MyItem { x: 11 // does not change the default state value of x to 5 }
解决方法
您可以通过将对象的属性绑定到本地声明的属性,从而创建可自定义的默认状态,以此规避此限制:
// MyItem.qml Text { property string defaultText: "default state" text: defaultText // this generates a binding which is then used by the \l {qmltocpp} to generate a default state ... } // Other.qml MyItem { defaultText: "new default state" // MyItem displays this text instead of "default state" when swtiching to the default state }
在某些 Qt 许可证下可用。
了解更多。