Nesting items (e.g. picture inside a picture frame) in QML
-
In QML, what is a better way of creating a reusable picture frame (or nesting items inside other items)?
By that, I mean that I have something like a picture frame. A picture frame can contain a picture. The picture must fill the picture frame, but it must fill only the inside of the picture frame, not the whole picture frame...
i.e. the child must not fill the parent. It must fill the space that the parent wants it to fill.
I could set the margins of the picture to be the same as the thickness of the frame.
Or as attached, I could create a PictureFrame with a property (let's call it "inner") that is an Item that already has the correct margins. And then fill that.
It works, but does QML have a more standard way of doing this?
(Without adding custom properties and without the picture knowing too much about the frame or vice-versa).PictureFrame.qml
import QtQuick Rectangle { id: myframe anchors.fill: parent color: Qt.color("red") property Item inner: myinner Item { id: myinner anchors.fill: parent anchors.margins: 20 } }
import QtQuick Item { width: 480 height: 480 PictureFrame { Rectangle { id: mypicture anchors.fill: parent.inner color: Qt.color("blue") } } }
Qt 6.5.1