Best way to get visual elements in QML

It would seem that the reparent user was not really “conceived” in the QML design, because although it is possible, it involves creating and changing states, which is simply not convenient to add to each element.

 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 of repeating elements without polluting elements with unnecessary states?

+4
source share
1 answer

not defined if you need to use QtQuick 1.0, but with 2.0 it also works and is more direct.

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

+3
source

Source: https://habr.com/ru/post/1543770/


All Articles