2017-07-21 47 views
0

上按OK加載另一個qml ui我試圖爲使用Qt Quick Controls 2的Windows桌面應用程序構建登錄表單。我希望登錄頁面顯示成功或失敗消息。在用戶按下Ok後成功登錄表單應該被隱藏,並且應該顯示另一個SettingsForm。在登錄失敗登錄表單應在用戶按下後再次顯示Ok登錄表單和設置表單都有其單獨的qmlui.qml文件。Qt Quick Controls 2在消息

我無法實現登錄成功時切換到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

+0

你在哪裏實例化你的'Dialog'?它是什麼樣的'Dialog'? ['QtQuick.Dialogs'](http://doc.qt.io/qt-5/qtquickdialogs-index.html)或['QtQuick.Controls 2.x'](https:// doc。 qt.io/qt-5/qml-qtquick-controls2-dialog.html)? – derM

+0

在Page1Form.ui.qml文件中實例化。它來自QtQuick.Controls –

+0

爲問題 –

回答

0

存放在標誌登錄成功。然後處理對話框的accepted() -signal,以發射自定義的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

+0

添加了消息對話框的圖像「然後,您可能會要求SwipeView繼續前進,onLoginSucceeded」我沒有得到這部分。如何從這裏轉到SettingsForm? –

+0

我希望你瞭解編輯。 – derM

+0

好吧我想我應該提到它是一個Windows應用程序。此外,我不知道爲什麼SwipeView被使用,它已經在默認模板中,作爲一個新手,我沒有多想,只是編輯了代碼段。我應該使用'Loader'我相信。現在,當我嘗試用'Loader'替換'SwipeView'時,整個設計轉移到左上角,背景圖像消失。 –

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 { 

    } 
    }