如何设置装载机
how to setup a loader
我想设置一个加载程序来打开 QML 文件;但似乎不起作用:
Button{//Below Right
width:profilePicture.width/2
height:profilePicture.width/2
x:profilePicture.x+profilePicture.width
y:profilePicture.y+profilePicture.height
contentItem: Image {
source: "Images/freecoins.png"
anchors.fill: parent
Rectangle{
anchors.fill:parent
radius: this.width
border.color: "yellow"
border.width: 2
color: "transparent"
}
}
onClicked: popUpFreeCoinsloader.active = true
Loader{
id: popUpFreeCoinsloader
source: "PopUpFreeCoins.qml"
active: false
focus: true
}
}
我也想设置那个 QML 文件的属性,但不知道该怎么做。
例如,我有 属性 int a 和 属性 int b,它们是 window 宽度和高度,我如何在加载器中初始化它们,就像在这样的组件中初始化一样?:
Component{
PopUP{a:100; b:200}
}
我不确定你所说的 "is seems doesn't work" 是什么意思,因为加载程序对我来说工作正常。该代码应该可以工作。一个简单的例子:
Main.qml:
Window {
visible: true
width: 300
height: 300
Button{
text: "button"
onClicked: loader.active = true
}
Loader{
id: loader
active: false
source: "Testy.qml"
focus: true
}
}
Testy.qml:
import QtQuick 2.7
Rectangle{
width: 200
height: 200
color: "red"
}
我怀疑你的 PopUpFreeCoins.qml 没有正确实例化或者宽度和高度为零。
关于你的第二个问题,使用sourceComponent
(link)
在我的例子中很容易看出:
修改后Main.qml:
Window {
visible: true
width: 300
height: 300
Button{
text: "button"
onClicked: loader.active = true
}
Loader{
id: loader
active: false
// load customized component
sourceComponent: rect
focus: true
}
//customized component
Component {
id: rect
Testy {
//customized values
width: 50
height: 50
color: "blue"
}
}
}
或者在你的例子中:
Loader{
id: popUpFreeCoinsloader
sourceComponent: popUp
active: false
focus: true
}
Component{
id: popUp
PopUP{a:100; b:200}
}
如果您的组件是一个单独的 window,那么它可能需要在加载后显式显示。
即loading 只会使对象对程序可用,但在显示之前它是不可见的,就像 Window
的 visible
属性 确保显示它一样。
至于价值转移,你有两种选择
加载时
Loader {
onLoaded: {
item.a = 123;
item.b = 456;
}
}
这里的值将被写入加载项的属性,即这是一个赋值。
有一个Binding
元素
Loader {
id: myLoader
}
Binding {
target: myLoader.item
property: "a"
value: 123
}
这里我们有一个 属性 绑定,即项目的 属性 绑定到值,具有自动更新和所有常见的 属性 绑定行为
我想设置一个加载程序来打开 QML 文件;但似乎不起作用:
Button{//Below Right
width:profilePicture.width/2
height:profilePicture.width/2
x:profilePicture.x+profilePicture.width
y:profilePicture.y+profilePicture.height
contentItem: Image {
source: "Images/freecoins.png"
anchors.fill: parent
Rectangle{
anchors.fill:parent
radius: this.width
border.color: "yellow"
border.width: 2
color: "transparent"
}
}
onClicked: popUpFreeCoinsloader.active = true
Loader{
id: popUpFreeCoinsloader
source: "PopUpFreeCoins.qml"
active: false
focus: true
}
}
我也想设置那个 QML 文件的属性,但不知道该怎么做。 例如,我有 属性 int a 和 属性 int b,它们是 window 宽度和高度,我如何在加载器中初始化它们,就像在这样的组件中初始化一样?:
Component{
PopUP{a:100; b:200}
}
我不确定你所说的 "is seems doesn't work" 是什么意思,因为加载程序对我来说工作正常。该代码应该可以工作。一个简单的例子:
Main.qml:
Window {
visible: true
width: 300
height: 300
Button{
text: "button"
onClicked: loader.active = true
}
Loader{
id: loader
active: false
source: "Testy.qml"
focus: true
}
}
Testy.qml:
import QtQuick 2.7
Rectangle{
width: 200
height: 200
color: "red"
}
我怀疑你的 PopUpFreeCoins.qml 没有正确实例化或者宽度和高度为零。
关于你的第二个问题,使用sourceComponent
(link)
在我的例子中很容易看出:
修改后Main.qml:
Window {
visible: true
width: 300
height: 300
Button{
text: "button"
onClicked: loader.active = true
}
Loader{
id: loader
active: false
// load customized component
sourceComponent: rect
focus: true
}
//customized component
Component {
id: rect
Testy {
//customized values
width: 50
height: 50
color: "blue"
}
}
}
或者在你的例子中:
Loader{
id: popUpFreeCoinsloader
sourceComponent: popUp
active: false
focus: true
}
Component{
id: popUp
PopUP{a:100; b:200}
}
如果您的组件是一个单独的 window,那么它可能需要在加载后显式显示。
即loading 只会使对象对程序可用,但在显示之前它是不可见的,就像 Window
的 visible
属性 确保显示它一样。
至于价值转移,你有两种选择
加载时
Loader { onLoaded: { item.a = 123; item.b = 456; } }
这里的值将被写入加载项的属性,即这是一个赋值。
有一个
Binding
元素Loader { id: myLoader } Binding { target: myLoader.item property: "a" value: 123 }
这里我们有一个 属性 绑定,即项目的 属性 绑定到值,具有自动更新和所有常见的 属性 绑定行为