如何在QML應用中設計自己的Dialog,qml應用dialog
對話方塊Dialog的設計在許多的QML應用是經常用到的。許多新的開發人員剛開始接觸QML,有時找不到頭緒。也許是由於QML的設計太過靈活,所以實現的方法有非常多。這裡介紹幾種簡單的方式。
1)使用Ubuntu SDK提供的標準API
我們可以使用Ubuntu SDK提供的標準Dialog介面。使用的方法非常簡單:
import QtQuick 2.4import Ubuntu.Components 1.2import Ubuntu.Components.Popups 1.0Item { width: units.gu(80) height: units.gu(80) Component { id: dialog Dialog { id: dialogue title: "Save file" text: "Are you sure that you want to save this file?" Button { text: "cancel" onClicked: PopupUtils.close(dialogue) } Button { text: "overwrite previous version" color: UbuntuColors.orange onClicked: PopupUtils.close(dialogue) } Button { text: "save a copy" color: UbuntuColors.orange onClicked: PopupUtils.close(dialogue) } } } Button { anchors.centerIn: parent id: saveButton text: "save" onClicked: PopupUtils.open(dialog) }}
就像文檔中介紹的那樣,我們需要import Ubuntu.Components.Popups 1.0模組來完成這個。這是目前最簡單的方法,而且使用這樣的方法,我們可以得到Ubuntu的look and feel。在調用時也可以傳人我們的caller:
Component { id: dialog Dialog { id: dialogue title: "Save file" text: "Are you sure that you want to save this file?" Button { text: "cancel" onClicked: PopupUtils.close(dialogue) } Button { text: "overwrite previous version" color: UbuntuColors.orange onClicked: PopupUtils.close(dialogue) } Button { text: "save a copy" color: UbuntuColors.orange onClicked: { console.log("caller: " + caller ); if ( caller !== null ) { caller.callback("file is saved!"); } PopupUtils.close(dialogue); } } } } Column { anchors.centerIn: parent spacing: units.gu(2) Button { id: saveButton text: "save" onClicked: PopupUtils.open(dialog) function callback(message) { console.log("returned: " + message); } } Button { id: anotherSave text: "Another Save" onClicked: PopupUtils.open(dialog, anotherSave) function callback(message) { console.log("returned: " + message); } }}
這樣我們可以通過caller來“callback”回調我們的方法。
2)建立我們自己的Dialog Component並動態調用它
我們可以自己建立一個自己的Dialog.qml檔案來存放為我們的Dialog所需要的內容:
import QtQuick 2.0Item { id: dialogComponent anchors.fill: parent // Add a simple animation to fade in the popup // let the opacity go from 0 to 1 in 400ms PropertyAnimation { target: dialogComponent; property: "opacity"; duration: 400; from: 0; to: 1; easing.type: Easing.InOutQuad ; running: true } // This rectange is the a overlay to partially show the parent through it // and clicking outside of the 'dialog' popup will do 'nothing' Rectangle { anchors.fill: parent id: overlay color: "#000000" opacity: 0.6 // add a mouse area so that clicks outside // the dialog window will not do anything MouseArea { anchors.fill: parent } } // This rectangle is the actual popup Rectangle { id: dialogWindow width: 300 height: 300 radius: 10 anchors.centerIn: parent Text { anchors.centerIn: parent text: "This is the popup" } // For demo I do not put any buttons, or other fancy stuff on the popup // clicking the whole dialogWindow will dismiss it MouseArea{ anchors.fill: parent onClicked: { // destroy object is needed when you dynamically create it dialogComponent.destroy() } } }}
我們可以通過如下的方法來動態建立這個Dialog:
Button { id: mydialog text: "My customized dialog" onClicked: { Qt.createComponent("Dialog.qml").createObject(mainpage, {}); } }
這個動態建立的Dialog在點擊自己的頁面時,被自動消除了。
MouseArea{ anchors.fill: parent onClicked: { // destroy object is needed when you dynamically create it dialogComponent.destroy() } }
這裡的設計因人而異。我們可以設計自己的按鈕來銷毀建立的Dialog,並同時傳回自己的選擇。這個方法的好處是設計的UI不必綁定特定的作業系統,比如Ubuntu。這樣這樣的代碼可以在多個平台上運行。
3)建立一個不可見的頁面,在需要是顯現
這個方法的實現也是非常簡單,也在許多的QML應用中被使用。在建立頁面時,我們可以同時建立不可見的部分。它們可以是重疊在一起的。只是在某個時間,只有一個頁面可以被顯示。通常不可見的頁面在啟動時被設定為“Dialog”。只有在需要的時候才可以被設定為可見。
為了說明問題,我們同樣設定了一個自己的AnotherDialog.qml檔案:
import QtQuick 2.0Item { id: dialogComponent anchors.fill: parent // Add a simple animation to fade in the popup // let the opacity go from 0 to 1 in 400ms PropertyAnimation { target: dialogComponent; property: "opacity"; duration: 400; from: 0; to: 1; easing.type: Easing.InOutQuad ; running: true } // This rectange is the a overlay to partially show the parent through it // and clicking outside of the 'dialog' popup will do 'nothing' Rectangle { anchors.fill: parent id: overlay color: "#000000" opacity: 0.6 // add a mouse area so that clicks outside // the dialog window will not do anything MouseArea { anchors.fill: parent } } // This rectangle is the actual popup Rectangle { id: dialogWindow width: 300 height: 300 radius: 10 anchors.centerIn: parent Text { anchors.centerIn: parent text: "This is the popup" } // For demo I do not put any buttons, or other fancy stuff on the popup // clicking the whole dialogWindow will dismiss it MouseArea{ anchors.fill: parent onClicked: { // destroy object is needed when you dynamically create it console.log("it is clicked!"); dialogComponent.visible = false; } } }}
在我們的Main.qml中:
Button { id: myanotherdialog text: "My another dialog" onClicked: { dialog1.visible = true; } } ... AnotherDialog { id: dialog1 anchors.fill: parent visible: false }
我們通過這樣的方法,可以設計出我們自己想要的任何一個Dialog。如果不採用Ubuntu的任何API,你可以讓它運行到任何一個你想要的平台之中。相比較第二種方法,這個方法需要佔用額外的記憶體,並且在應用一啟動時,就會被裝載到記憶體中。
運行我們的測試應用:
整個項目的源碼在:git clone https://gitcafe.com/ubuntu/dialog.git