QAbstractItemModel::dataChanged what would you prefer?
-
Flavour question about
QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
:Say you have a model that changes, at the same time, the
Qt::DisplayRole
of theindex(0,0)
andQt::FontRole
of theindex(1,0)
Would you prefer that the model emitted
dataChanged(index(0,0),index(1,0),{Qt::DisplayRole,Qt::FontRole});
or would you rather havedataChanged(index(0,0),index(0,0),{Qt::DisplayRole}); dataChanged(index(1,0),index(1,0),{Qt::FontRole});
? -
@kshegunov said in QAbstractItemModel::dataChanged what would you prefer?:
if Qt::UserRole isn't anything that should be updated in the GUI.
You are right, that aspect is important, changed the question so that both roles matter for the UI
-
Hi,
Also number two as it might optimise the number of calls to data.
-
I'd generally prefer flavour 2 too. It's more "correct", since
index(0, 0)
hasn't changed its FontRole andindex(1, 0)
hasn't changed its DisplayRole.In the end though, I'd also consider the code complexity and impact. If this was a custom model within my own app, I'd just choose the option that requires simpler code (which I think is Flavour 1).
-
When you look at the default implementation you will see that the second one is better:
https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qabstractitemview.cpp.html#_ZN17QAbstractItemView11dataChangedERK11QModelIndexS2_RK7QVectorIiE -
I would go with the 2nd option, simply for the fact that it's much clearer, what is supposed to happen. Or rather what just happend.
-
@JKSH said in QAbstractItemModel::dataChanged what would you prefer?:
I'd just choose the option that requires simpler code (which I think is Flavour 1)
it's actually flavour 2 in my case as I would have to check that the two indexes are one next to the other while I don't care using flavour 2
@Christian-Ehrlicher said in QAbstractItemModel::dataChanged what would you prefer?:
When you look at the default implementation you will see that the second one is better:
Ok, this is a show stopper. I did not realise sending
topLeft!=bottomRight
(often) triggers a repaint of the entire view. This makes flavour 2 strictly betterThanks everyone for your insight