Unsolved Help and explanation with QAbstractTableModel
-
Hello
I use the class (PersonModel) that inherits QAbstractTableModel. I have one row in database which contains 4 columns. When i fill QTableView with data from database i call the data() function several times with the following parameters:
Col: 0 Role: 6
Col: 0 Role: 7
Col: 0 Role: 9
Col: 0 Role: 10
Col: 0 Role: 1
Col: 0 Role: 0
Col: 0 Role: 8
Col: 1 Role: 6
Col: 1 Role: 7
Col: 1 Role: 9
Col: 1 Role: 10
Col: 1 Role: 1
Col: 1 Role: 0
Col: 1 Role: 8
Col: 2 Role: 6
Col: 2 Role: 7
Col: 2 Role: 9
Col: 2 Role: 10
Col: 2 Role: 1
Col: 2 Role: 0
Col: 2 Role: 8
Col: 3 Role: 6
Col: 3 Role: 7
Col: 3 Role: 9
Col: 3 Role: 10
Col: 3 Role: 1
Col: 3 Role: 0
Col: 3 Role: 8
Col: 0 Role: 6
Col: 0 Role: 7
Col: 0 Role: 9
Col: 0 Role: 10
Col: 0 Role: 1
Col: 0 Role: 0
Col: 0 Role: 8
Col: 1 Role: 6
Col: 1 Role: 7
Col: 1 Role: 9
Col: 1 Role: 10
Col: 1 Role: 1
Col: 1 Role: 0
Col: 1 Role: 8
Col: 2 Role: 6
Col: 2 Role: 7
Col: 2 Role: 9
Col: 2 Role: 10
Col: 2 Role: 1
Col: 2 Role: 0
Col: 2 Role: 8
Col: 3 Role: 6
Col: 3 Role: 7
Col: 3 Role: 9
Col: 3 Role: 10
Col: 3 Role: 1
Col: 3 Role: 0
Col: 3 Role: 8Could someone explain me why it calls so many time and if i could improve it somehow?
And this is my data() function, and is it well written?:
QVariant PersonModel::data(const QModelIndex &index, int role) const { if(!isIndexValid(index)){ return QVariant(); } const Person& person = *mPerson->at(index.row()); qDebug() << "Col: " << index.column() << "Role: " << role; switch (role) { case Qt::DisplayRole: if(index.column() == 0) return person.getId(); else if(index.column() == 1) return person.getFirstName(); else if(index.column() == 2) return person.getLastName(); else return person.getPesel(); default: return QVariant(); } }
-
@ppitu said in Help and explanation with QAbstractTableModel:
Could someone explain me why it calls so many time
Because every cell must be painted
if i could improve it somehow?
No
-
But i have only for cell in on row.
Or does it not matter? -
As your debug output shows you only the 4 cells are asked.
-
Since you only provide DisplayRole in your data()
you could addif(role != Qt::DisplayRole) return QVariant();
at the start of this function.
-
@Bonnie data() is pure virtual, you have to return QVariant()
-
@Christian-Ehrlicher okay, thanks for the answer
-
@ppitu
Don't kid yourself that this change will make that much of difference, especially compiled optimized, other than suppressing your debug messages. What you have to accept is that the Qt code is going to call yourdata()
on each cell with a fair number of different roles, and that's just how it is! -
Hi,
There's work in progress to improve the internals of that part and make it more efficient which are going to show up for Qt 6.
-
@Christian-Ehrlicher
Oops, didn't notice that, I've changed my code above.
Anyway my point is that there's no need to get aPerson
for every data() call.