http://blog.csdn.net/liuhongwei123888/article/details/6549825
2015-10-14 10:19
453 查看
QML的Loader元素经常备用来动态加载QML组件。可以使用source属性或者sourceComponent属性加载。这个元素最有用的地方是它能在qml组件需要的时候再创建,即延迟创建QML的时间。
1、
[css] view
plaincopy
main.qml
------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
}
MouseArea {
anchors.fill: parent
onClicked: changePage();
}
function changePage() {
if(isFirst) {
pageLoader.source = "Page1.qml"
} else {
pageLoader.source = "Page2.qml"
}
isFirst = !isFirst;
}
}
Page1.qml
-------------------------------------
import QtQuick 1.0
Rectangle {
width: 100
height: 62
Text {
anchors.centerIn: parent
text: "Page1 Test"
}
}
Page2.qml
---------------------------------------
import QtQuick 1.0
Rectangle {
width: 100
height: 62
Text {
anchors.centerIn: parent
text: "Page1 Test"
}
}
2、上面的代码就能界面在Page1和Page2之间切换了,别忘了还能使用sourceComponent属性:
[css] view
plaincopy
main.qml
--------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
sourceComponent: rect
}
MouseArea {
anchors.fill: parent
onClicked: changePage();
}
function changePage() {
if(isFirst) {
pageLoader.source = "Page1.qml"
} else {
pageLoader.source = "Page2.qml"
}
isFirst = !isFirst;
}
Component {
id: rect
Rectangle {
width: 200
height: 50
color: "red"
Text {
text: "Default Page"
anchors.fill: parent
}
}
}
}
上面的代码实现了默认加载组件功能.
3、接收来自加载的qml发出的信号
使用Connections元素可以接收到任何发送自加载组件的信号。
[css] view
plaincopy
main.qml
---------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
source: "Page1.qml"
}
Connections {
target: pageLoader.item
onMessage: console.log(msg);
}
}
Page1.qml
----------------------------------------------
import QtQuick 1.0
Rectangle {
id: myItem
signal message(string msg)
width: 100; height: 100
MouseArea {
anchors.fill: parent
onClicked: myItem.message("clicked!");
}
}
4、加载与被加载组件中都有相同的事件,那么需要设置Loader的属性focus为true,且设置被加载组件 focus: true才能让事件不被传播到被加载组件。
[css] view
plaincopy
main.qml
-------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
source: "Page2.qml"
focus: true
}
Keys.onPressed: {
console.log("Captured: ", event.text);
event.accepted = true;
}
}
Page2.qml
---------------------------------
import QtQuick 1.0
Rectangle {
width: 100
height: 62
Text {
anchors.centerIn: parent
text: "Page2 Test"
}
focus: true
Keys.onPressed: {
console.log("Loaded item captured: ", event.text);
event.accepted = true;
}
}
如果在Page2.qml中去掉event.accepted = true;那么main.qml和Page2.qml都会接收到按键事件,也就是说按键事件会传播到main.qml中
5、Loader的 onStatusChanged和onLoaded事件
[css] view
plaincopy
main.qml
-------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
source: "Page2.qml"
focus: true
onStatusChanged: console.log(pageLoader.status == Loader.Ready)
onLoaded: console.log("Loaded")
}
MouseArea {
anchors.fill: parent
onClicked: changePage();
}
function changePage() {
if(isFirst) {
pageLoader.source = "Page1.qml"
} else {
pageLoader.source = "Page2.qml"
}
isFirst = !isFirst;
}
Component {
id: rect
Rectangle {
width: 200
height: 50
color: "red"
Text {
text: "Default Page"
anchors.fill: parent
}
}
}
Keys.onPressed: {
console.log("Captured: ", event.text);
event.accepted = true;
}
}
1、
[css] view
plaincopy
main.qml
------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
}
MouseArea {
anchors.fill: parent
onClicked: changePage();
}
function changePage() {
if(isFirst) {
pageLoader.source = "Page1.qml"
} else {
pageLoader.source = "Page2.qml"
}
isFirst = !isFirst;
}
}
Page1.qml
-------------------------------------
import QtQuick 1.0
Rectangle {
width: 100
height: 62
Text {
anchors.centerIn: parent
text: "Page1 Test"
}
}
Page2.qml
---------------------------------------
import QtQuick 1.0
Rectangle {
width: 100
height: 62
Text {
anchors.centerIn: parent
text: "Page1 Test"
}
}
2、上面的代码就能界面在Page1和Page2之间切换了,别忘了还能使用sourceComponent属性:
[css] view
plaincopy
main.qml
--------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
sourceComponent: rect
}
MouseArea {
anchors.fill: parent
onClicked: changePage();
}
function changePage() {
if(isFirst) {
pageLoader.source = "Page1.qml"
} else {
pageLoader.source = "Page2.qml"
}
isFirst = !isFirst;
}
Component {
id: rect
Rectangle {
width: 200
height: 50
color: "red"
Text {
text: "Default Page"
anchors.fill: parent
}
}
}
}
上面的代码实现了默认加载组件功能.
3、接收来自加载的qml发出的信号
使用Connections元素可以接收到任何发送自加载组件的信号。
[css] view
plaincopy
main.qml
---------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
source: "Page1.qml"
}
Connections {
target: pageLoader.item
onMessage: console.log(msg);
}
}
Page1.qml
----------------------------------------------
import QtQuick 1.0
Rectangle {
id: myItem
signal message(string msg)
width: 100; height: 100
MouseArea {
anchors.fill: parent
onClicked: myItem.message("clicked!");
}
}
4、加载与被加载组件中都有相同的事件,那么需要设置Loader的属性focus为true,且设置被加载组件 focus: true才能让事件不被传播到被加载组件。
[css] view
plaincopy
main.qml
-------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
source: "Page2.qml"
focus: true
}
Keys.onPressed: {
console.log("Captured: ", event.text);
event.accepted = true;
}
}
Page2.qml
---------------------------------
import QtQuick 1.0
Rectangle {
width: 100
height: 62
Text {
anchors.centerIn: parent
text: "Page2 Test"
}
focus: true
Keys.onPressed: {
console.log("Loaded item captured: ", event.text);
event.accepted = true;
}
}
如果在Page2.qml中去掉event.accepted = true;那么main.qml和Page2.qml都会接收到按键事件,也就是说按键事件会传播到main.qml中
5、Loader的 onStatusChanged和onLoaded事件
[css] view
plaincopy
main.qml
-------------------------------------
import QtQuick 1.0
Item {
property bool isFirst : false;
width: 200
height: 200
Loader {
id: pageLoader
source: "Page2.qml"
focus: true
onStatusChanged: console.log(pageLoader.status == Loader.Ready)
onLoaded: console.log("Loaded")
}
MouseArea {
anchors.fill: parent
onClicked: changePage();
}
function changePage() {
if(isFirst) {
pageLoader.source = "Page1.qml"
} else {
pageLoader.source = "Page2.qml"
}
isFirst = !isFirst;
}
Component {
id: rect
Rectangle {
width: 200
height: 50
color: "red"
Text {
text: "Default Page"
anchors.fill: parent
}
}
}
Keys.onPressed: {
console.log("Captured: ", event.text);
event.accepted = true;
}
}
相关文章推荐
- child process failed, exited with error number 100
- NSString和block的属性时copy,而不用retain 而容器类使用retain
- 今天在我导入一些安卓项目的源码的时候会出现style.xml 和MAIN.xml 等错误, 网上的方法试了各种clean各种fix等方法都是不能解决。最后去国外论坛得到解决
- Kafka错误3_kafka中 FailedToSendMessageException解决_集群启动问题
- Monitor vs WaitHandle
- AIDL通信原理
- LeetCode -- Contains Duplicate
- iredmail下安装脚本分析(一)---get_all.sh 文件所在目录为PKGS
- Windows下Hadoop报错Failed to locate the winutils
- 【LeetCode从零单刷】Container With Most Water
- adb install 报错INSTALL_FAILED_UPDATE_INCOMPATIBLE 解决方法
- hdu4810Wall Painting dp+异或
- rails项目本地运行
- VIVADO报错解决: logical ports have no user assigned specific location constraint (LOC)
- grails2.3.11第一课
- 音乐播放器-MainFragment分析2
- Codeforces Round #325 (Div. 2)D. Phillip and Trains BFS
- 【停课集训10.13】【#3 training】
- Gmail Api 的解读及例子
- hdu 4770 Lights Against Dudely(二进制枚举情况)