我有一个自定义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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个的使用很简单:
Window {
visible: true
Footer {
}
}
Run Code Online (Sandbox Code Playgroud)
但现在我想在一个视图中添加一个“ButtonA” RowLayout,Footer在另一个视图中添加一个“ButtonB”。
我怎样才能做到这一点?
看这个答案。
您必须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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这确保了声明为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()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用它StackView作为在视图之间导航的便捷方式。
| 归档时间: |
|
| 查看次数: |
2368 次 |
| 最近记录: |