QML我可以在GridView上保留列吗?

时间:2014-07-13 05:49:21

标签: qt qml qt5

我想在GridView上获得像Gird.columns一样的固定列。

有可能吗?

我试过了 verticalLayoutDirection, 流, 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
    }
}

1 个答案:

答案 0 :(得分:-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)
    ...
}