InsertRow/SetRow
Haiyang-Bian opened this issue · comments
When I use 'TableViewColumn' in the section of 'TableView', I got the following error: "TableViewColumn is not a type". How can I settle this promblem without changing the way to building a table?
My code is as follows:
import QtQuick
import QtQuick.Window
import QtQuick.Controls
import Qt.labs.qmlmodels
Window {
visible: true
width: 400
height: 300
title: "Table"
ListModel {
id: tableModel
ListElement { name: "Alice"; age: 20; gender: "Female" }
ListElement { name: "Bob"; age: 22; gender: "Male" }
ListElement { name: "Charlie"; age: 21; gender: "Male" }
ListElement { name: "David"; age: 23; gender: "Male" }
ListElement { name: "Eve"; age: 19; gender: "Female" }
}
TableView {
anchors.fill: parent
model: tableModel
selectionMode: SelectionMode.SingleSelection
selectionBehavior: SelectionBehavior.SelectRows
TableViewColumn {
role: "name"
title: "Name"
width: 100
}
TableViewColumn {
role: "age"
title: "Age"
width: 100
}
TableViewColumn {
role: "gender"
title: "Gender"
width: 100
}
itemDelegate: Item {
Text {
text: styleData.value
anchors.fill: parent
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
Rectangle {
anchors.fill: parent
color: styleData.selected ? "blue" : "white"
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log("You clicked row " + styleData.row)
console.log("Name: " + model.name)
console.log("Age: " + model.age)
console.log("Gender: " + model.gender)
}
}
}
}
}
How can I use the "insertRow" and "setRow" when I using a model in my component with qml? I've tried jsonObject and there dosen't have this method, I've tried Vector and got "DimensionMismatch: array could not be broadcast to match destination".
My code is as follows:
import QtQuick 2.0
import QtQuick.Controls 2.15
import QtQuick.Window
Window {
id: window
width: 700
height: 700
visible: true
ComboBox {
model: tList
textRole: "name"
valueRole: "type"
width: 200
height: 50
currentIndex: 0
onCurrentIndexChanged: {
tList.removeRow(currentIndex)
tList.insertRow(currentIndex,{
name: "New Item",
type: "New Type"
})
}
delegate: ItemDelegate {
width: parent.width
height: 40
contentItem: Row {
spacing: 10
Text {
text: name
font.pixelSize: 20
verticalAlignment: Text.AlignVCenter
}
}
}
}
}
Hi, regarding TableViewColumn
: this was a QtQuick.Controls version 1 component and it is removed in Qt 6.
Inserting and removing rows with the Julia item model is shown in these examples:
- tableview.jl
- dynamiclist.jl (uses a
ListView
)
There is also the tableview test that shows the full functionality, but it's a little less readable:
Thanks, the second example of tableview
helps me a lot!