PDA

View Full Version : [qt4,win,g++] model/view programming: selection, internal pointer wrong ?!



jh
15th February 2006, 22:02
hi,
i get a selection model from a treeview and connect signal
selectionChanged to the following method:



void
Container_Widget::selectionChanged(const QItemSelection& selected,
const QItemSelection& deselected)
{

printf("<Container_Widget::selection changed()>\n");
fflush(stdout);

QModelIndexList indexlist = selected.indexes();
QListIterator<QModelIndex> it(indexlist);

while(it.hasNext()) {

QModelIndex modelindex = it.next();

if(!modelindex.isValid()) {
printf("Container_Widget::selectionChanged() : model index not valid \n");
fflush(stdout);
continue;
}

printf("Container_Widget::selectionChanged() : row = %d, col = %d \n",
modelindex.row(), modelindex.column() );
fflush(stdout);

Container_ModelItem* item
= static_cast<Container_ModelItem*>(modelindex.internalPointer());

... call some method of item -> segfault !!

}

printf("</Container_Widget::selection changed()>\n");
fflush(stdout);

}


the problem is that the internal pointer of the model index
cannot be of class Container_ModelItem,
since if i call a method of this class the program crashes.
i checke already all methods of the model which return a QModelIndex,
i could'nt find a mistake there.
where is the mistake?

bets regards,
jh

wysota
16th February 2006, 13:05
First of all use dynamic_cast instead of static one. And second of all, it's not possible to help you without seeing the source of the model, at least its index() method.

jh
16th February 2006, 20:09
i already found the problem. i used a sorting proxy model which was
invalid.

thanx.

jh