Qt 在QML中重新分配可视项的更好方法
在QML的设计中,似乎没有真正“设想”用户重新分配,因为即使有可能,它也涉及到创建和更改状态,这只是不方便添加到每个项目中Qt 在QML中重新分配可视项的更好方法,qt,qml,qt-quick,qtquick2,reparenting,Qt,Qml,Qt Quick,Qtquick2,Reparenting,在QML的设计中,似乎没有真正“设想”用户重新分配,因为即使有可能,它也涉及到创建和更改状态,这只是不方便添加到每个项目中 import QtQuick 1.0 Item { width: 200; height: 100 Rectangle { id: redRect width: 100; height: 100 color: "red" } Rectangle { id
import QtQuick 1.0
Item {
width: 200; height: 100
Rectangle {
id: redRect
width: 100; height: 100
color: "red"
}
Rectangle {
id: blueRect
x: redRect.width
width: 50; height: 50
color: "blue"
states: State {
name: "reparented"
ParentChange { target: blueRect; parent: redRect; x: 10; y: 10 }
}
MouseArea { anchors.fill: parent; onClicked: blueRect.state = "reparented" }
}
}
我想知道是否有一种更优雅的方式来重新分配项目,而不污染具有不必要状态的项目?不确定是否需要使用QtQuick 1.0,但在2.0中,这同样有效,而且更直接
ParentChange
实际上不适用于独立对象在其自己的文件中(具有内部可见的id,如root
)。这个答案也适用于那个场景。
import QtQuick 2.0
Item {
width: 200; height: 100
Rectangle {
id: redRect
width: 100; height: 100
color: "red"
}
Rectangle {
id: blueRect
x: redRect.width
width: 50; height: 50
color: "blue"
MouseArea { anchors.fill: parent; onClicked:
{ blueRect.parent = redRect; blueRect.x = 10; blueRect.y = 10 }
}
}
}