Qt Quick之StackView詳解(2),qtstackview
在“StackView詳解(1)”中,我們學習了StackView的基本用法,這次呢,我們來講delegate的定製、被管理的View的生命週期、尋找View等主題。
本文還會用到“StackView詳解(1)”中的樣本,如有需要可以回頭看看。
附加屬性
首先看看StackView提供的附加屬性 Stack(後面會用到):
- Stack.index,index代表當前Item在StackView裡的索引,從0開始哦,和StackView.depth不同哦,depth從1開始。
- Stack.status,這是一個枚舉值,代表當前Item的狀態。
- Stack.view,指向當前Item所屬的StackView執行個體
我們可以在StackView管理的Item裡直接存取附加屬性,後面會有樣本。
尋找View
StackView有一個find方法:find(func, onlySearchLoadedItems)。這個方法讓我們提供一個比較函數,它會對StackView管理的頁面應用指定的func函數,當func返回true時,就認為找到了需要的View,find()會返回這個View。第二個參數onlySearchLoadedItems為true時,說明只尋找載入到記憶體中的Item,為false時,則尋找所有Item。
來看一個簡單的例子,基於之前的例子修改的,修改的地方我做了標註。
import QtQuick 2.4import QtQuick.Controls 1.3import QtQuick.Window 2.2Window { title: "StackViewDemo"; width: 480; height: 320; visible: true; StackView { id: stack; anchors.fill: parent; /* onBusyChanged: { console.log("busy - " + stack.busy); } */ Text { text: "Click to create first page"; font.pointSize: 14; font.bold: true; color: "blue"; anchors.centerIn: parent; MouseArea { anchors.fill: parent; onClicked: if(stack.depth == 0)stack.push(page); } } } Component { id: page; Rectangle { color: Qt.rgba(stack.depth*0.1, stack.depth*0.2, stack.depth*0.3); property alias text: txt.text; //property alias Text { id: txt; //new code anchors.centerIn: parent; font.pointSize: 24; font.bold: true; color: stack.depth <= 4 ? Qt.lighter(parent.color) : Qt.darker(parent.color); //在onCompleted中為text屬性賦值 //避免屬性綁定,方便尋找。 Component.onCompleted: { text = "depth - " + stack.depth; } } Button { id: next; anchors.right: parent.right; anchors.bottom: parent.bottom; anchors.margins: 8; text: "Next"; width: 70; height: 30; onClicked: { if(stack.depth < 8) stack.push(page); } } Button { id: back; anchors.right: next.left; anchors.top: next.top; anchors.rightMargin: 8; text: "Back"; width: 70; height: 30; onClicked: { if(stack.depth > 0) stack.pop(); } } Button { id: home; anchors.right: back.left; anchors.top: next.top; anchors.rightMargin: 8; text: "Home"; width: 70; height: 30; onClicked: { if(stack.depth > 0)stack.pop(stack.initialItem); } } Button { id: clear; anchors.right: home.left; anchors.top: next.top; anchors.rightMargin: 8; text: "Clear"; width: 70; height: 30; onClicked: { if(stack.depth > 0)stack.clear(); } } //new code Button { id: popTo3; anchors.right: clear.left; anchors.top: next.top; anchors.rightMargin: 8; text: "PopTo3"; width: 70; height: 30; onClicked: { var resultItem = stack.find( function(item){ console.log(item.text); return item.text === "depth - 3" ? true : false; } ); if(resultItem !== null)stack.pop(resultItem); } } } }}
我給id為page的組件,添加了一個PopTo3的按鈕,點擊它,會退棧,直到棧的深度為3。
find()方法尋找時的順序,是從棧頂到棧底。如果找不到,則返回null。如果調用pop(null),則會直接退到棧底,即棧深度為1。所以我在代碼裡做了判斷,如果find返回null,就不調用pop。
代碼裡還有一些變化,為了方便通過文本尋找,我給page的Rectangle添加了一個屬性別名,指向它內部的Text對象的text屬性。
其實如果使用StackView的get(index)方法,PopTo3的onClicked訊號處理器還可以重寫成下面的樣子:
onClicked: { var resultItem = stack.get(2); if(resultItem !== null)stack.pop(resultItem);}
更簡單了吧。當然如果堅持用find,也可以結合附加屬性做更有效率的尋找:
onClicked: { var resultItem = stack.find( function(item){ console.log(item.text); return item.Stack.index == 2; } ); if(resultItem !== null)stack.pop(resultItem); }
生命週期
正如我們前面提到的,StackView管理的頁面,通常是動態建立的,那這些動態建立的對象,就需要在合適的時候銷毀,否則就記憶體泄露了,就悲劇了。
StackView會接管它維護的頁面對象(View)的生命週期。當我們調用pop時,那些出棧的Item,會被銷毀掉。這一點可以通過給page組件內的Rectangle對象添加一個Component.onDestruction訊號處理器來驗證,代碼如下:
Component.onDestruction: console.log("destruction, current depth - " + stack.depth);
當你調用pop(item)或clear()時,就會觸發Item的destruction。
OK,搞明白了這一點我們就放心了。
另外,一個Item在StackView裡,其實有很多狀態:
非棧頂的Item,都處在inactive狀態。棧頂的Item,當StackView可見時處於active狀態,當StackView不可見時,棧頂的Item處於inactive狀態。
更多詳情,請參考Qt的協助吧。
為了檢測一個Item的狀態變化,可以處理StackView提供的附加屬性status的變化處理器。具體的代碼是:
Stack.onStatusChanged: console.log("status of item " + Stack.index + ": " + Stack.status);
如你所見,我們用到了Stack.status附加屬性,它是一個枚舉值,可以取下面的值:
- Stack.Inactive: 0,不可見
- Stack.Activating: 2,正在啟用
- Stack.Active: 3,活動狀態
- Stack.Deactivating: 1,正變為非啟用狀態
嗯,有了這些值,就可以對照著日誌觀察一個Item的狀態變化了。
定製過渡動畫(StackViewDelegate)
StackView的delegate屬性可以定製頁面切換時的過渡動畫(push、pop、replace),它的類型是StackViewDelegate。
StackViewDelegate有下列屬性:
- popTransition ,類型為Component,指定出棧過渡。
- pushTransition ,類型為Component,指定入棧過渡。
- replaceTransition ,類型為Component,指定替換操作的過渡。
一般地,pushTransition應該是StackViewTransition的執行個體,StackViewTransition是ParallelAnimation的子類,有exitItem和enterItem兩個屬性,我們可以指定兩個動畫,分別應用到exitItem和enterItem上,當過渡發生時,這兩個動畫會同時執行。