如何设置我的按钮组件以打开 window

How to setup my button component to open a window

这是 window 我想在文件 PopUpFreeCoins.qml 中打开的代码:

import QtQuick 2.0
import QtQuick.Controls 2.1
Item {
    property int t
    property int c
    ListModel{
        id:ff
        ListElement {
            name: "ByFollow"
            s: "Images/follow.png"
        }
        ListElement {
            name: "ByLike"
            s: "Images/care.png"
        }
        ListElement {
            name: "ByComment"
            s: "Images/chat.png"
        }
    }
    ListView{
        width:t-t/10
        height: c/5
        layoutDirection:Qt.LeftToRight
        orientation: ListView.Horizontal
        model: ff
        spacing:50
        delegate: Button{
            contentItem:  Image{
            source: s
        }}
    }
}

属性 t 在主文件中设置等于 window width 并且 属性 c 设置为 window height。这是我的代码 Button.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
        }
        onClicked: PopUp{height:100;width:300;PopUpFreeCoins{t:a;c:b;}}
    }

属性 a 是 window 宽度,b 是 window 高度。 这一行 onClicked: PopUp{height:100;width:300;PopUpFreeCoins{t:a;c:b;}} 有一个错误我不知道如何处理! 这是错误:

Cannot assign object type PopUpFreeCoins_QMLTYPE_0 with no default method

您需要以某种方式创建对象。您有多种动态创建对象的方法。一种方法是使用 Component.createObject(parent),这需要您在文件中实例化 Component
在这里您还可以传递一个对象 ({property0 : value, property1:value ... }) 作为第二个参数,以设置要实例化的 Component 的属性。您不应该将父级设置为 null,因为它可能会发生,JS-garbage 收集器再次过于激进。

或者,您可以使用 Loadersource (QML-file) 或 sourceComponent 加载它。在这里你不会遇到垃圾收集器的问题。

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    width: 1024
    height: 800

    visible: true

    Button {
        text: 'create'
        onClicked: test.createObject(this)
    }

    Button {
        x: 200
        text: 'load'
        onClicked: loader.active = !loader.active
    }

    Loader {
        id: loader
        source: 'TestObj.qml'
        active: false
    }

    Component {
        id: test
        TestObj {}
    }
}

TestObj.qml包括要打开的Window

或者您可以从头开始创建 Window,只需将 visible 更改为 truefalse.