【问题标题】:Qt Quick Controls 2 Loading another qml ui on pressing OK in messageQt Quick Controls 2 在消息中按 OK 时加载另一个 qml ui
【发布时间】:2017-12-27 01:27:00
【问题描述】:

我正在尝试使用 Qt Quick Controls 2 为 Windows 桌面应用程序构建登录表单。我希望登录页面显示成功或失败消息。在用户按下Ok 成功后,登录表单应从视图中隐藏,并应显示另一个SettingsForm。登录失败时,用户按下Ok 后应再次显示登录表单 登录表单和设置表单都有各自的qmlui.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!" 之后,但它不起作用。

编辑:

登录成功后的对话框如下所示:

【问题讨论】:

  • 你在哪里实例化你的DialogDialog 是什么类型的?来自QtQuick.Dialogs 或来自QtQuick.Controls 2.x 的那个?
  • 在 Page1Form.ui.qml 文件中实例化。它来自 QtQuick.Controls
  • 在问题中添加了消息对话框的图像

标签: qt qml qtquickcontrols2


【解决方案1】:

我已经在你的 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 {

    }
    }

【讨论】:

    【解决方案2】:

    将登录成功存储在一个标志中。然后处理对话框的accepted()-signal,触发自定义loginSucceeded()-signal,然后在其他任何地方使用它继续前进。

    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

    【讨论】:

    • “那么你可能会要求 SwipeView 继续前进,onLoginSucceeded”我没有得到这部分。如何从这里进入 SettingsForm?
    • 好吧,我想我应该提到它是针对 Windows 应用程序的。另外,我不确定为什么要使用 SwipeView,它已经存在于默认模板中,而且作为新手,我没有多想,只是编辑了代码片段。我相信我应该使用Loader。现在,当我尝试用Loader 替换SwipeView 时,整个设计移到左上角并且背景图像消失了。
    • SwipeView 在桌面应用程序上工作得非常好,但Loader 可能会更好。如果您对Loader 有疑问,请打开另一个问题。不要忘记发布Page1Form 的代码以重现您的错误。另外,如果是,请不要忘记将此答案标记为正确。如果您不了解所有内容,请解释您的问题。
    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多