ListView problem with c++ model
Hi all,
I have a problem with ListView with a custom c++ model created following qt guide (i.e. extending QAbstractListModel).
In my application I have a ListView that can show only 10 rows, while in my model I load 17 items.
The problem is that when I start toying around with the list (flicking, clicking on elements, moving highlight with my personal focus management), at a certain point, the ListView stops to work!
Flicking is blocked or, when it is enabled, the elements that should appear are not visible.. or again currentItem still blocked in wrong item like this (http://tinypic.com/view.php?pic=5d3p05&s=5).
I found a sort of similar problem at this link: http://stackoverflow.com/questions/1...ws-are-visible but the answer does not help me.
Notes:
1) ListView is a my custom ListView that has some extension to manage focus changing; I tried with a standard ListView but the problem still remains.
2) With static ListModel added in qml file everything works fine, so the problem seems to be my QAbstractListModel implementation.
3) In my focus management I print some debug strings and I noticed that, when the problem occurs, ListView.view.count becomes always 0.
Can anyone help me?
Thanks in advance.
Re: ListView problem with c++ model
Can you show us your model implementation?
Re: ListView problem with c++ model
Code:
FieldListModel::FieldListModel(IndexedFieldContainer * fieldContainer)
{
_fieldContainer = fieldContainer;
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[ValueRole] = "value";
roles[DescriptionRole] = "description";
setRoleNames(roles);
IndexedFieldContainer::Iterator it = _fieldContainer->begin();
for ( ; it != _fieldContainer->end(); ++it )
{
QObject::connect((*it
),
SIGNAL(fieldValueChanged
(int)),
this, SLOT(fieldCurrentValueChanged(int)));
}
}
{
if ( !index.isValid() )
if (index.row() < 0 || index.row() > _fieldContainer->count())
{
}
DataModelBasicField * field = _fieldContainer->at(index.row());
switch ( role )
{
case NameRole:
return field->getName();
break;
case ValueRole:
return field->toString();
break;
case DescriptionRole:
return field->getDescription();
break;
default:
}
}
int FieldListModel
::rowCount(const QModelIndex & /* parent*/ ) const {
return _fieldContainer->count();
}
void FieldListModel::fieldCurrentValueChanged(int listIndex)
{
dataChanged(index,index);
}
Do you need other things?
edit:
Code:
typedef QHash<QString, DataModelBasicField*> FieldContainer;
typedef QVector<DataModelBasicField*> IndexedFieldContainer;
Re: ListView problem with c++ model
What about setData(), insertRows(), etc.?
Re: ListView problem with c++ model
The model is not modified so I don't think is necessary to reimplement it
Added after 40 minutes:
Update:
I don't know what .. but something must be wrong.
If I run program (compiled in debug) without debugger: changing of a property value and working with ListView works with a small number of operation before "crash".
If I run program (compiled in debug) with debugger: changing of a property value and ListView do not work immediately!
Over all the really strange thing is that, with debugger, i break on emit of the signal "fieldValueChanged" but the relative slot is not called!
I checked also that is connected the right QObject!
This stinks of dirty memory.. but I also tried to use valgrind without find any invalid read related to the data model.
How is it possibile?
Re: ListView problem with c++ model
It seems your model is modifyable since you are emitting dataChanged() signals at some point.
Re: ListView problem with c++ model
edit: mmm ..maybe setData is needed
Re: ListView problem with c++ model
A couple of things that might not be related to the observed problem.
in data() the check for row's range is wrong. row == index.row() > _fieldContainer->count() passes through but is not a valid index in the list
Should be
Code:
if (index.row() < 0 || index.row() >= _fieldContainer->count())
in fieldCurrentValueChanged(int), the given listIndex is passed for row and column. column should be 0 in a list model.
The method should also call index() since that is one of the methods implemented by QAbstractListModel. It likely does just call createIndex() but this is not guaranteed.
Cheers,
_