问题描述
假设我有一个QML窗口,其中包含许多不同的控件:
.whl
}
现在,我希望我的窗口在特定事件上更改“模式”,并显示一组完全不同的控件。
想象一下它是多屏形式的一部分。当前状态是表单的第一页。当用户单击“下一步”按钮时,它将转到表单的第2页。我想添加一组代表第2页的控件。
在QtQuick / QML中进行组织的正确方法是什么?
解决方法
通常的方法是使用StackView。该组织将是这样的:
Window {
StackView {
id: stackView
initialItem: page1
}
Item {
id: footerItem
// Maybe add other buttons here too
Button {
id: nextBtn
text: "Next"
onClicked: {
stackView.push(page2);
}
}
}
Component {
id: page1
Page1 {
// Define this in separate Page1.qml file
// This is where your page 1 controls go.
}
}
Component {
id: page2
Page2 {
// Define this in separate Page2.qml file
// This is where your page 2 controls go.
}
}
}
,
我可能会使用StackLayout使用“返回”和“下一步”按钮来实现模式更改视图或表单。 StackLayout
类提供了一堆项目,一次只能看到一个项目。您可以通过更新currentIndex
进入下一个或上一个模式。
StackLayout {
id: layout
anchors.fill: parent
currentIndex: 1
Rectangle {
color: 'teal'
implicitWidth: 200
implicitHeight: 200
}
Rectangle {
color: 'plum'
implicitWidth: 300
implicitHeight: 200
}
}