将我自己的QML按钮插入GridLayout

我是Qt的新手,并且想要在GridLayout中使用一些Button(在带有Rectangle的单独.qml文件中制成)。不幸的是,似乎没有将Button定位到不同的单元格。一切都在第一行/列中。是否有必要为ID提供变量,例如数组?还是QML动态对象? 这里的按钮:

Item {
property string textOnButton: "Test"
property int w: 130
property int h: 100

Rectangle{
    id: emptyRectangle
    width: parent.w
    height: parent.h
    Text {
        text: parent.parent.textOnButton + parent.index
        anchors.horizontalCenter: parent.parent.horizontalCenter
        topPadding: 6
        font.pixelSize: 30
    }
 }

}

这里是几次放置Item的GridLayout:

GridLayout{
    id:grid1
    rows: 3
    Layout.margins: 20
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.verticalCenter: parent.verticalCenter
    columnSpacing: 10
    rowSpacing: 10
    EmptyButton{
        id: eins
        textOnButton: "HELP"
    }
    EmptyButton{
        id: zwei
        textOnButton: "HELP"
    }

}

稍后,我想在按钮上使用不同的颜色/文字并例如切换不同的灯。 谢谢您的帮助!

yangwei521 回答:将我自己的QML按钮插入GridLayout

您需要设置GridLayout和所有其他项目的宽度和高度

GridLayout{
    width: 500
    height: 500
    id:grid1
    rows: 3
    Layout.margins: 20
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.verticalCenter: parent.verticalCenter
    columnSpacing: 10
    rowSpacing: 10
    EmptyButton{
        id: eins
        textOnButton: "HELP"
    }
    EmptyButton{
        id: zwei
        textOnButton: "HELP"
    }

}
本文链接:https://www.f2er.com/2839087.html

大家都在问