Solved The model inherited from QAbstractTableModel throws errors
-
@Mikeeeeee said in The model inherited from QAbstractTableModel throws errors:
What is the bug and how to fix it?
Christian Ehrlicher 18 minutes ago
And you should only return values for Qt::DisplayRole
-
I fixed the code, there is no more error. But the column index is always zero. I don't understand how to fix this. Debag returns:
0 0
0 0
0 0
Model size of -3 is less than 0
0 1
0 1
0 1
0 2
0 2
0 2 -
-
@Mikeeeeee said in The model inherited from QAbstractTableModel throws errors:
I fixed the code
Please update your code in your example so we can see what you're doing.
-
This post is deleted! -
@Christian-Ehrlicher I updated the code an hour ago. I didn't add anything after that.
-
Maybe the problem is in QML?
import QtQuick 2.13 import QtQuick.Controls 1.4 Item { width: 380 height: 680 TableView { anchors.fill: parent TableViewColumn { role: "role1" title: "Column1" width: 100 } TableViewColumn { role: "role2" title: "Column2" width: 100 } TableViewColumn { role: "role3" title: "Column3" width: 100 } model: myModel } }
-
Then you still did not read my comment about the Qt::DisplayRole. So stay on your own.
-
I don't understand what you mean. Please answer in more detail and clearly.
-
Its work
QVariant MyModel::data(const QModelIndex & index, int role) const { if (index.row() < 0 || index.row() >= list1.count()) return QVariant(); if(role == 0){ return QVariant(list1[index.row()]); } if(role == 1){ return QVariant(list2[index.row()]); } if(role == 2){ return QVariant(list3[index.row()]); } return QVariant(); }