Solved Updating QAbstractTableModel
-
Hello.
I've implemented a class which inherits from QAbstractTableModel. I'm getting data to a tableview in QML and everything, but I'm struggling updating the table on certain events.
The table data is retrieved from a SQL-Lite database, and I'm simply emitting dataChanged() when QFileSystemWatcher emits fileChanged() on the database-file (after the data is renewed from DB). This works fine, and the table-content is updated.
So there is two issues I have:
1: I want to change the delegate (rectangle) color depending on 5 different states retrieved from the database.
2: There might be fewer or more rows retrieved from the database on an update meaning I have to refresh the table in order to remove rows not relevant any more. I'm fine with updating the entire table since there is not many rows.
How can this be solved?
My TableView in qml:
TableView { id: tableView property var columnWidths: [150, 150, 760, 180] columnWidthProvider: function (column) { return columnWidths[column]; } rowHeightProvider: function (column) { return 40; } rowSpacing: 5 anchors.fill: parent anchors.topMargin: 100 anchors.leftMargin: 20 topMargin: columnsHeader.implicitHeight clip: true flickableDirection: Flickable.VerticalFlick model: alarmmodel delegate: Rectangle { color: Style.current.componentBG2 Text { text: alarmdata anchors.fill: parent anchors.margins: 10 color: Style.current.colorFont font.pixelSize: 15 verticalAlignment: Text.AlignVCenter } } } Row { id: columnsHeader y: tableView.contentY z: 2 Repeater { model: tableView.columns > 0 ? tableView.columns : 1 Label { width: tableView.columnWidthProvider(modelData) height: 35 text: alarmmodel.headerData(modelData, Qt.Horizontal) color: '#888888' font.pixelSize: 15 font.bold: true padding: 10 verticalAlignment: Text.AlignVCenter background: Rectangle { color: Style.current.componentBG } } } } ScrollIndicator.vertical: ScrollIndicator { } }
-
@adaptine said in Updating QAbstractTableModel:
simply emitting dataChanged()
instead call
beginResetModel()
before fetching the new data andendResetModel()
after the update is done -
Bingo!!!