[英]Qt Quick Controls 2 Loading another qml ui on pressing OK in message
我正在尝试使用Qt Quick Controls 2为Windows桌面应用程序构建登录表单。我希望登录页面显示成功或失败消息。 用户按“ Ok
”后成功后,应从视图中隐藏登录表单,并显示另一个SettingsForm
。 在用户按下后,登录失败的登录表单应再次显示Ok
两个登录表单,并设置表单有自己独立的qml
和ui.qml
文件。
登录成功后,我无法实现切换到SettingsForm的功能。 到目前为止,这是我得到的:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.3
ApplicationWindow {
visible: true
width: 900
height: 600
title: qsTr("Welcome!")
maximumHeight: height
maximumWidth: width
minimumHeight: height
minimumWidth: width
x: Screen.width / 2 - width / 2
y: Screen.height / 2 - height / 2
SwipeView {
id: swipeView
anchors.fill: parent
Page1Form {
login.onClicked: {
if (username.text == "a" && password.text == "a") {
message.title = "Login success!"
message.visible = true
}
else {
message.title = "Login Failed! Try Again."
message.visible = true
}
}
}
}
消息代码对话框:
Dialog
{
id: message
width: 300
height: 100
x:-10
standardButtons: Dialog.Ok
modal: true
onAccepted: visible=false
}
我尝试插入:
SettingsForm {
}
在message.title = "Login success!"
但这不起作用。
编辑:
成功登录后,对话框如下所示:
将登录成功存储在标志中。 然后处理对话框的accepted()
-信号,以触发自定义的loginSucceeded()
-信号,然后将其用于其他任何地方。
Page1Form {
id: p1f
signal loginSucceeded()
property bool success: false
login.onClicked: {
if (username.text == "a" && password.text == "a") {
success = true
message.title = "Login success!"
message.visible = true
}
else {
success = false
message.title = "Login Failed! Try Again."
message.visible = true
}
}
Connections {
target: p1f.message // I guess the Dialog is exposed via the property 'message'
onAccepted: if (p1f.success) loginSucceeded()
}
然后,例如,您可以要求SwipeView
继续运行, onLoginSucceeded
(我想,您打算使用SwipeView
来管理屏幕吗?)
SwipeView {
id: formSwiper
interactive: false // otherwise someone might swipe away your login screen.
// Since you use QtQuick.Controls 2.2 it should be available
Page1Form {
onLoginSucceeded: formSwiper.currentIndex++ // Move on!
[...]
}
SettingsForm {
[...]
}
}
免责声明:我没有QtQuick.Controls 2.2,所以我无法测试该
Dialog
我已经在SwipeArea实现中添加了从LoginForm页面滑动到SettingsForm页面的实现:
SwipeView {
id: swipeView
anchors.fill: parent
property int loginPage : 0 // index for Login page
property int settingsPage : 1 // index for Settings page
Page1Form {
login.onClicked: {
if (username.text == "a" && password.text == "a") {
message.title = "Login success!"
message.visible = true
swipeView.currentIndex = swipeView.settingsPage // Swiping the view to settings page
}
else {
message.title = "Login Failed! Try Again."
message.visible = true
swipeView.currentIndex = swipeView.loginPage // current page view is kept as login page (if you want you can remove this line)
}
}
}
SettingsForm {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.