Component
组件,结合已有元素封装自己需要的控件。
progress : real
status : enumeration
url : url
completed() //组件构建完成
destruction() //销毁组件
object createObject(QtObject parent, object properties)
string errorString()
object incubateObject(Item parent, object properties, enumeration mode)
Loader
动态加载组件,继承自Item,通过Loader加载组件后如果需要对组件属性进行修改,可通过item加载组件中元素,然后可对其属性进行修改。
active : bool
asynchronous : bool
item : object //Loader加载的顶级元素
progress : real
source : url
sourceComponent : Component
status : enumeration
loaded()
object setSource(url source, object properties)
实操:
Component{
id:com
Rectangle{
id: rect
width: 100
height: 100
color: "lightblue"
Component.onCompleted: {
console.log("onCompleted width: ", width, "height: ", height)
}
Component.onDestruction: {
// console.log("onDestruction width: ", width, "height: ", height)
}
}
}
Loader{
id: loader
// source: "/2.jpg" //不可加载图片,status会报error
// source: "rectangle.qml" //可加载qml文件
asynchronous: true //异步加载,status从loading到ready
sourceComponent: com
onStatusChanged: {
console.log("status:", status)
}
}
Button{
width: 50
height: 50
x: loader.item.width
onPressed: { //按下的时候不会加载的组件
loader.sourceComponent = null
}
onReleased: { //松开的时候会重新加载组件
loader.sourceComponent = com
loader.item.width = 80
loader.item.height = 80
loader.item.color = "blue"
}
}
Component{
id: com
// Image {
// id: img
// source: "/2.jpg"
//// source: "/th.gif" //不可加载动画
// width:200
// height:200
// }
AnimatedImage { //动图
id: img
// source: "/2.jpg"
source: "/th.gif" //加载动画与静态图
speed: 2 //修改动图播放速度
width:200
height:200
}
}
Loader{
id: loader
// source: "/2.jpg" //不可加载图片,status会报error
// source: "rectangle.qml" //可加载qml文件
asynchronous: true //异步加载,status从loading到ready
sourceComponent: com
onStatusChanged: {
console.log("status:", status)
}
}
Button{
width: 50
height: 50
x: loader.item.width
onPressed: { //暂停动图播放
loader.item.paused = true
}
onReleased: { //开始动图播放
loader.item.paused = false
}
}