Qt 如何设置加载程序

Qt 如何设置加载程序,qt,qml,loader,Qt,Qml,Loader,我想设置一个加载程序来打开一个QML文件;但这似乎不起作用: Button{//Below Right width:profilePicture.width/2 height:profilePicture.width/2 x:profilePicture.x+profilePicture.width y:profilePicture.y+profilePicture.height contentItem: Imag

我想设置一个加载程序来打开一个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文件的属性,但不知道怎么做。 例如,我有属性intA和属性intB,它们是窗口的宽度和高度,我如何在loader中初始化它们,就像在组件中初始化一样

Component{
    PopUP{a:100; b:200}
}

我不确定你所说的“is似乎不工作”是什么意思,因为装载机对我来说工作正常。代码应该是有效的。一个简单的例子:

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
    }
}
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"
        }
    }
}
Testy.qml:

import QtQuick 2.7

Rectangle{
    width: 200
    height: 200
    color: "red"
}
我怀疑您的PopUpFreeCoins.qml没有正确实例化,或者宽度和高度为零

关于第二个问题,请使用
sourceComponent

在我的示例中很容易看出:

修改的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}
}

如果您的组件是一个单独的窗口,则可能需要在加载后显式显示它

也就是说,加载只会使该对象对程序可用,但在显示之前它是不可见的,就像
窗口的
visible
属性确保它被显示一样

至于价值转移,你有两个选择

  • 加载时

    Loader {
        onLoaded: {
            item.a = 123;
            item.b = 456;
        }
    }
    
  • 在这里,这些值将写入加载项的属性中,即这是一个赋值

  • 使用
    绑定
    元素

    Loader {
        id: myLoader
    }
    Binding {
        target: myLoader.item
        property: "a"
        value: 123
    }
    
  • 这里我们有一个属性绑定,即项的属性绑定到值,具有自动更新和所有常见的属性绑定行为