Better way to reparent visual items in QML

末鹿安然 提交于 2020-01-02 05:46:07

问题


It would seem that in the design of QML user reparent was not really "envisioned", because even though it is possible, it involves creating and changing states, which is just not convenient to add to each and every item.

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

I was wondering if there is a more elegant way to reparent items without polluting items with unnecessary states?


回答1:


not certain if you need to use QtQuick 1.0, but with 2.0 this also works and is imo more straight forward.

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



来源:https://stackoverflow.com/questions/24109184/better-way-to-reparent-visual-items-in-qml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!