我有一个习惯Footer
Component
我想在 QML 应用程序的不同位置重用它:
Rectangle {
color: "gold"
height: 50
anchors {
bottom: parent.bottom
left: parent.left
right: parent.right
}
RowLayout {
anchors.fill: parent
anchors.margins: 10
Button {
text: "quit"
}
}
}
这个的使用很简单:
Window {
visible: true
Footer {
}
}
但现在我想添加一个“按钮”RowLayout
of my Footer
在一个视图中为“按钮”,在另一个视图中为“按钮”。
我怎样才能做到这一点?
See this https://stackoverflow.com/questions/26527103/how-propagate-tableviewcolumn-list-to-child-tableview/26535543#26535543 answer.
你必须声明一个default
财产在Footer.qml
:
import QtQuick 2.0
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
Rectangle {
color: "gold"
height: 50
default property alias content: rowLayout.children
anchors {
bottom: parent.bottom
left: parent.left
right: parent.right
}
RowLayout {
id: rowLayout
anchors.fill: parent
anchors.margins: 10
Button {
text: "quit"
}
}
}
这确保了声明为子项的任何项目Footer
实例将被添加到其RowLayout
.
main.qml
:
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
width: 640
height: 480
visible: true
StackView {
id: stackView
anchors.fill: parent
initialItem: viewAComponent
}
Component {
id: viewAComponent
Rectangle {
id: viewA
color: "salmon"
Footer {
id: footerA
Button {
text: "Go to next view"
onClicked: stackView.push(viewBComponent)
}
}
}
}
Component {
id: viewBComponent
Rectangle {
id: viewB
color: "lightblue"
Footer {
id: footerB
Button {
text: "Go to previous view"
onClicked: stackView.pop()
}
}
}
}
}
I used StackView http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html作为在视图之间导航的便捷方式。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)