Qt QML我可以在GridView上保存列吗?

Qt QML我可以在GridView上保存列吗?,qt,qml,qt5,Qt,Qml,Qt5,我想在GridView上获得固定的列,比如Gird.columns 可能吗 我试过了 垂直向外, 流 GridView上的snapMode。 但它在调整窗口大小时被更改 比如说,, listmodel上有5项。 我只要1列,只要调整窗口大小。 但当窗口高度大小小于项目的总高度时,它会超过1列 Rectangle { width: 300; height: 200 MouseArea { anchors.fill: parent hoverEnabled:

我想在GridView上获得固定的列,比如Gird.columns

可能吗

我试过了 垂直向外, 流 GridView上的snapMode。 但它在调整窗口大小时被更改

比如说,, listmodel上有5项。 我只要1列,只要调整窗口大小。 但当窗口高度大小小于项目的总高度时,它会超过1列

Rectangle {
width: 300; height: 200

    MouseArea {
        anchors.fill: parent
        hoverEnabled: true
        //onClicked: grid.currentIndex = -1
        //onEntered: grid.currentIndex = -1
    }


    Component {
    //Grid {
        id: contactDelegate
        //Grid {
        //columns: 1
        Item {
            width: grid.cellWidth; height: grid.cellHeight
            //columns: 1

            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onClicked: grid.currentIndex = index
                onEntered: grid.currentIndex = index
            }

            Column {
                anchors.fill: parent
                Image { source: portrait; anchors.horizontalCenter: parent.horizontalCenter }
                Text { text: name; anchors.horizontalCenter: parent.horizontalCenter }
            }
        }
    }


    GridView {
        id: grid
        anchors.fill: parent
        cellWidth: 80; cellHeight: 80
        //verticalLayoutDirection: GridView.TopToBottom
        //flow: GridView.FlowTopToBottom
        //snapMode: GridView.SnapToRow
        //columns: 1

        model: ContactModel {}
        delegate: contactDelegate
        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        focus: true

        flickableChildren: MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            //onClicked: grid.currentIndex = -1
            //onEntered: grid.currentIndex = -1
        }
        Component.onCompleted: currentIndex = -1
    }
}

根据GridView的方向,将cellWidth或cellHeight绑定到GridView的宽度或高度。大概是这样的:

GridView {
    ...
    cellWidth: grid.width/2       // if you want 2 columns for example
    cellHeight: grid.height/2     // OR, if you want only 2 rows (for horizontal flow)
    ...
}