QML之使用Loader加載QML組件


呵呵,今晚是邊看《裸婚時代》邊敲代碼,那電影看得...!錢真他媽不是個東西.

盼望Meego火起來。

QML的Loader元素經常備用來動態加載QML組件。可以使用source屬性或者sourceComponent屬性加載。這個元素最有用的地方是它能在qml組件需要的時候再創建,即延遲創建QML的時間。

 

1、

 

[css]  view plain  copy
 
  1. main.qml  
  2. ------------------------------------  
  3. import QtQuick 1.0  
  4.   
  5. Item {  
  6.     property bool isFirst : false;  
  7.     width: 200  
  8.     height: 200  
  9.   
  10.     Loader {  
  11.         id: pageLoader  
  12.     }  
  13.   
  14.     MouseArea {  
  15.         anchors.fill: parent  
  16.         onClicked: changePage();  
  17.     }  
  18.   
  19.     function changePage() {  
  20.         if(isFirst) {  
  21.             pageLoader.source = "Page1.qml"  
  22.         } else {  
  23.             pageLoader.source = "Page2.qml"  
  24.         }  
  25.   
  26.         isFirst = !isFirst;  
  27.     }  
  28.   
  29. }  
  30.   
  31.   
  32. Page1.qml  
  33. -------------------------------------  
  34. import QtQuick 1.0  
  35.   
  36. Rectangle {  
  37.     width: 100  
  38.     height: 62  
  39.     Text {  
  40.         anchors.centerIn: parent  
  41.         text: "Page1 Test"  
  42.     }  
  43. }  
  44.   
  45.   
  46. Page2.qml  
  47. ---------------------------------------  
  48. import QtQuick 1.0  
  49.   
  50. Rectangle {  
  51.     width: 100  
  52.     height: 62  
  53.     Text {  
  54.         anchors.centerIn: parent  
  55.         text: "Page1 Test"  
  56.     }  
  57. }  

 

 

2、上面的代碼就能界面在Page1和Page2之間切換了,別忘了還能使用sourceComponent屬性:

 

[css]  view plain  copy
 
  1. main.qml  
  2. --------------------------------------  
  3. import QtQuick 1.0  
  4.   
  5. Item {  
  6.     property bool isFirst : false;  
  7.     width: 200  
  8.     height: 200  
  9.   
  10.     Loader {  
  11.         id: pageLoader  
  12.         sourceComponent: rect  
  13.     }  
  14.   
  15.     MouseArea {  
  16.         anchors.fill: parent  
  17.         onClicked: changePage();  
  18.     }  
  19.   
  20.     function changePage() {  
  21.         if(isFirst) {  
  22.             pageLoader.source = "Page1.qml"  
  23.         } else {  
  24.             pageLoader.source = "Page2.qml"  
  25.         }  
  26.   
  27.         isFirst = !isFirst;  
  28.     }  
  29.   
  30.     Component {  
  31.         id: rect  
  32.         Rectangle {  
  33.             width: 200  
  34.             height: 50  
  35.             color: "red"  
  36.             Text {  
  37.                 text: "Default Page"  
  38.                 anchors.fill: parent  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43. }  

 

上面的代碼實現了默認加載組件功能.

 

 

3、接收來自加載的qml發出的信號

使用Connections元素可以接收到任何發送自加載組件的信號。

 

[css]  view plain  copy
 
  1. main.qml  
  2. ---------------------------------------  
  3. import QtQuick 1.0  
  4.   
  5. Item {  
  6.     property bool isFirst : false;  
  7.     width: 200  
  8.     height: 200  
  9.   
  10.     Loader {  
  11.         id: pageLoader  
  12.         source: "Page1.qml"  
  13.     }  
  14.   
  15.   
  16.     Connections {  
  17.         target: pageLoader.item  
  18.         onMessage: console.log(msg);  
  19.     }  
  20.   
  21. }  
  22.   
  23. Page1.qml  
  24. ----------------------------------------------  
  25. import QtQuick 1.0  
  26.   
  27. Rectangle {  
  28.     id: myItem  
  29.     signal message(string msg)  
  30.     width: 100; height: 100  
  31.   
  32.     MouseArea {  
  33.         anchors.fill: parent  
  34.         onClicked: myItem.message("clicked!");  
  35.     }  
  36. }  

 

 

 

4、加載與被加載組件中都有相同的事件,那么需要設置Loader的屬性focus為true,且設置被加載組件 focus: true才能讓事件不被傳播到被加載組件。

 

 

[css]  view plain  copy
 
  1. main.qml  
  2. -------------------------------------  
  3. import QtQuick 1.0  
  4.   
  5. Item {  
  6.     property bool isFirst : false;  
  7.     width: 200  
  8.     height: 200  
  9.   
  10.     Loader {  
  11.         id: pageLoader  
  12.         source: "Page2.qml"  
  13.         focus: true  
  14.     }  
  15.     
  16.     Keys.onPressed: {  
  17.         console.log("Captured: ", event.text);  
  18.          event.accepted = true;  
  19.     }  
  20.   
  21. }  
  22.   
  23.   
  24. Page2.qml  
  25. ---------------------------------  
  26. import QtQuick 1.0  
  27.   
  28. Rectangle {  
  29.     width: 100  
  30.     height: 62  
  31.     Text {  
  32.         anchors.centerIn: parent  
  33.         text: "Page2 Test"  
  34.     }  
  35.     focus: true  
  36.     Keys.onPressed: {  
  37.         console.log("Loaded item captured: ", event.text);  
  38.         event.accepted = true;  
  39.     }  
  40. }  

 

 如果在Page2.qml中去掉event.accepted = true;那么main.qml和Page2.qml都會接收到按鍵事件,也就是說按鍵事件會傳播到main.qml中

 

5、Loader的 onStatusChanged和onLoaded事件

 

 

[css]  view plain  copy
 
  1. main.qml  
  2. -------------------------------------  
  3. import QtQuick 1.0  
  4.   
  5. Item {  
  6.     property bool isFirst : false;  
  7.     width: 200  
  8.     height: 200  
  9.   
  10.     Loader {  
  11.         id: pageLoader  
  12.         source: "Page2.qml"  
  13.         focus: true  
  14.         onStatusChanged:  console.log(pageLoader.status == Loader.Ready)  
  15.         onLoaded: console.log("Loaded")  
  16.     }  
  17.   
  18.     MouseArea {  
  19.         anchors.fill: parent  
  20.         onClicked: changePage();  
  21.     }  
  22.   
  23.     function changePage() {  
  24.         if(isFirst) {  
  25.             pageLoader.source = "Page1.qml"  
  26.         } else {  
  27.             pageLoader.source = "Page2.qml"  
  28.         }  
  29.   
  30.         isFirst = !isFirst;  
  31.     }  
  32.   
  33.     Component {  
  34.         id: rect  
  35.         Rectangle {  
  36.             width: 200  
  37.             height: 50  
  38.             color: "red"  
  39.             Text {  
  40.                 text: "Default Page"  
  41.                 anchors.fill: parent  
  42.             }  
  43.         }  
  44.     }  
  45.   
  46.     Keys.onPressed: {  
  47.         console.log("Captured: ", event.text);  
  48.          event.accepted = true;  
  49.     }  
  50.   
  51. }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM