import QtQuick 2.15 import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 400 height: 300 GridView { width: parent.width height: parent.height cellWidth: 100 cellHeight: 30 columnCount: 2 delegate: Text { text: model.name + " - " + model.age width: 100 height: 30 } model: ListModel { ListElement { name: "John"; age: 25 } ListElement { name: "Alice"; age: 30 } ListElement { name: "Bob"; age: 35 } } } } 运行显示 Cannot assign to non-existent property "columnCount"
时间: 2024-02-14 09:25:53 浏览: 137
很抱歉,我之前给出的代码存在一些问题。在QML中,GridView没有columnCount属性来指定列数。相反,GridView会根据提供的model自动计算列数。以下是修正后的代码示例:
```qml
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
width: 400
height: 300
GridView {
width: parent.width
height: parent.height
cellWidth: 100
cellHeight: 30
delegate: Text {
text: model.name + " - " + model.age
width: 100
height: 30
}
model: ListModel {
ListElement { name: "John"; age: 25 }
ListElement { name: "Alice"; age: 30 }
ListElement { name: "Bob"; age: 35 }
}
}
}
```
这样,你可以使用GridView来创建一个简单的表格,并在每个单元格中显示姓名和年龄的信息。
阅读全文