【问题标题】:Better way to reparent visual items in QML在 QML 中重新设置视觉项目的更好方法
【发布时间】:2014-06-08 18:03:48
【问题描述】:

在 QML 的设计中,user reparent 似乎并不是真正“设想的”,因为即使有可能,它也涉及创建和更改状态,这不方便添加到每个项目中。

 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" }
     }
 }

我想知道是否有一种更优雅的方式来重新设置项目而不用不必要的状态污染项目?

【问题讨论】:

    标签: qt qml qt-quick qtquick2 reparenting


    【解决方案1】:

    不确定是否需要使用 QtQuick 1.0,但在 2.0 中也可以使用,而且更直接。

    导入 QtQuick 2.0

    项目{ 宽度:200;高度: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 } } } }

    【讨论】:

    • ParentChange 实际上不适用于它们自己文件中的独立对象(具有内部可见的 id,例如 root)。这个答案也适用于那种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2021-05-30
    • 2013-03-15
    • 1970-01-01
    相关资源
    最近更新 更多