首页 文章

如何在GridLayout中移动单元格 - QML?

提问于
浏览
0

考虑到documentation of GridLayout,这是我尝试过的:


import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1

Window
{
    visible: true

    MainForm
    {
        GridLayout {
            id: gridLayout
            columns: 3

            height: 100
            width: 100
            property int oneRow: 0
            property int oneCol: 0

            Rectangle { id: one; Layout.row :gridLayout.oneRow; Layout.column: gridLayout.oneCol;
                height: 50; width: 50; color: "brown"}

            Rectangle { height: 50; width: 50; color: "red" }
            Rectangle { height: 50; width: 50; color: "blue"}
            Rectangle { height: 50; width: 50; color: "green"}
            Rectangle { height: 50; width: 50; color: "yellow"}
        }

        Component.onCompleted:
        {
            gridLayout.oneRow = 2
            gridLayout.oneCol = 2
        }
    }
}

如果我从 Component.onCompleted 注释掉这段代码,

gridLayout.oneRow = 2
gridLayout.oneCol = 2

我明白了:

enter image description here

Whereas I want that brown square to "move to" the second row's last column.
所以,我写道:

gridLayout.oneRow = 1
gridLayout.oneCol = 2

Component.onCompleted .

但后来我得到了以下内容:

enter image description here

which is NOT what I wanted.

请帮忙 .

1 回答

  • 1

    如果要更改 GridLayout 中某个项目的单元格编号,则需要将初始row number and column number分配给所有元素 yourself ,然后动态更改所需项目的位置,如下所示:

    import QtQuick 2.4
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.1
    
    Window
    {
        visible: true
    
        MainForm
        {
            GridLayout {
                id: gridLayout
    
                height: 100
                width: 100
    
                property int oneRow: 0
                property int oneCol: 0
                Rectangle { id: one;
                    Layout.row :gridLayout.oneRow; Layout.column: gridLayout.oneCol;
                    height: 50; width: 50; color: "brown"}
    
                property int twoRow: 0
                property int twoCol: 1
                Rectangle { id: two;
                    Layout.row :gridLayout.twoRow; Layout.column: gridLayout.twoCol;
                    height: 50; width: 50; color: "red" }
    
                property int threeRow: 0
                property int threeCol: 2
                Rectangle { id: three;
                    Layout.row :gridLayout.threeRow; Layout.column: gridLayout.threeCol;
                    height: 50; width: 50; color: "blue"}
    
                property int fourRow: 1
                property int fourCol: 0
                Rectangle { id: four;
                    Layout.row :gridLayout.fourRow; Layout.column: gridLayout.fourCol;
                    height: 50; width: 50; color: "green"}
    
                property int fiveRow: 1
                property int fiveCol: 1
                Rectangle { id: five;
                    Layout.row :gridLayout.fiveRow; Layout.column: gridLayout.fiveCol;
                    height: 50; width: 50; color: "yellow"}
            }
    
            Component.onCompleted:
            {
                gridLayout.oneRow = 1
                gridLayout.oneCol = 2
            }
        }
    }
    

    现在,正如您所看到的, Component.onCompleted 中的以下代码将棕色矩形移动到第2行的第3列 .

    Component.onCompleted:
      {
          gridLayout.oneRow = 1
          gridLayout.oneCol = 2
      }
    

    enter image description here

相关问题