I used GridView to display ListModel. I initially set cellWidth to:
cellWidth = grid.width/3
to create a grid of three columns. then I want to change the number of columns to 2, so I set cellWidth to:
cellWidth = grid.width/2
GridView screen has changed. But when I resize the container desktop window, the cells in the gridview will no longer resize.
What should I do to make it right?
Please review the following code:
import QtQuick 2.1 import QtQuick.Controls 1.0 import QtQuick.Window 2.0 ApplicationWindow { title: qsTr("Hello World") width: 640 height: 480 menuBar: MenuBar { Menu { title: qsTr("File") MenuItem { text: qsTr("2 columns") onTriggered: grid.cellWidth = grid.width/2; } MenuItem { text: qsTr("3 columns") onTriggered: grid.cellWidth = grid.width/3; } } } GridView { id: grid anchors.fill: parent cellWidth: width / 3; cellHeight: 300; model: ListModel { ListElement { name: "Apple" cost: 2.45 } ListElement { name: "Orange" cost: 3.25 } ListElement { name: "Banana" cost: 1.95 } } delegate : Rectangle { //anchors.fill: parent width: grid.cellWidth height: grid.cellHeight border.color: "green" border.width: 2 color: "red" } } }
source share