如何創建一些可重用的QML對象,它可以注入另一個對象?如何製作一些可重用的QML對象,它可以注入另一個QML對象?
我曾嘗試使用Component
& Loader
,但似乎不是我想要的。 (它仍然封裝整個QML類型和彈性的缺乏,難以重用)
用例:
Card.qml
import QtQuick 2.0
import QtQuick.Layouts 1.3
Rectangle {
default property var innerObject
property string titleText: "[Hello Untitled Title]"
id: root
color: "#fff"
ColumnLayout {
anchors.fill: parent
Rectangle {
id: header
height: 10
width: parent.width
color: "#666"
RowLayout {
Text { text: titleText; color: "#fff" }
}
}
// How to inject innerObject in here ?
}
}
main.qml
import QtQuick 2.0
import QtQuick.Layouts 1.3
Card {
titleText: "Image Information"
ColumnLayout { /* .......*/ } // innerObject
}
Card {
titleText: "Image Viewer"
Rectangle { /* .......*/ } // innerObject
}
的可能的複製[如何你是否將QML項目分配給QML中的組件屬性,然後在組件內部使用該對象?](http://stackoverflow.com/questions/5021350/how-do-you-assign-a-qml-item-to -a-component-property-in-qml-and-then-use-that-ob) –
尤其見:http://stackoverflow.com/a/10031214/6845813 –