首页 > 解决方案 > 将我自己的 QML 按钮插入 GridLayout

问题描述

我是 Qt 的新手,想在 GridLayout 中有一些按钮(在单独的 .qml 文件中使用 Rectangle 制作......)。不幸的是,它似乎没有将按钮定位到不同的单元格。一切都在第一行/列。是否有必要的 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
    }
 }

}

而这里的 GridLayout 应该在其中放置 Item 几次:

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"
    }

}

稍后我想在按钮上使用不同的颜色/文本并切换不同的灯光。谢谢你的帮助!!

标签: qtqmlgrid-layout

解决方案


您需要设置 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"
    }

}

推荐阅读