在 QML 中重新设置视觉项的更好方法 [英] Better way to reparent visual items in QML

查看:90
本文介绍了在 QML 中重新设置视觉项的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 QML 的设计中,用户重父似乎并没有真正设想",因为即使有可能,它也涉及创建和更改状态,这只是不方便添加到每个项目中.

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?

推荐答案

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

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

项目{宽度:200;高度:100

Item { width: 200; height: 100

这篇关于在 QML 中重新设置视觉项的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆