Qt快速控制2按下消息

时间:2017-07-21 06:02:05

标签: qt qml qtquickcontrols2

我正在尝试使用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!"之后

,但它不起作用。

编辑:

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

enter image description here

2 个答案:

答案 0 :(得分:0)

将登录成功存储在标志中。然后处理对话框的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

答案 1 :(得分:0)

我在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 {

    }
    }