I want to create a custom list using QListView
and so I had to extend QListView
, QItemDelegate
and QAbstractListModel
and then implement the specific methods, along with QAbstractItemModel::data(const QModelIndex & index, int role = Qt::DisplayRole) const
.
It displays correctly on the screen at first glance, but the problem occurs after populating the list model.,The function data(index,role)
is called 4-5 times per item model with different roles (some of them undefined roles/out of range/probably random). And it occurs not only after the initialization of the list model! When I hover a list element, the view calls data(index,role) with the correct index and role but right afterwards it is called again with an unexpected role value.
It seems an awkward behavior. I couldn't find the source of the strange calls. I put logs in every method of my custom classes to be sure that I'm not calling by mistake the data(index,role)
method with wrong values.
Does anyone have some ideas where to look at or why this strange calls occur?
EDIT The source of the "strange" calls is in:
QSize CDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
return QSize(QItemDelegate::sizeHint(option, index));
}
somehow when calling QItemDelegate::sizeHint()
it triggers data()
with different role values.
In my case I have defined role values starting from 0(0,1,2,3). According to @Jens those values are reserved. Changing the starting value of my custom roles solved my problem.