[Qt-interest] segfault with beginRemoveRows()

Arnold Krille arnold at arnoldarts.de
Sat Mar 21 11:49:53 CET 2009


On Saturday 21 March 2009 11:13:57 Uwe Drechsel wrote:
> I don't understand why parent() is called several times and finally for
> the rootItem, which obviously has no parent and when trying to
> return createIndex(parentItem->childNumber(), 0, parentItem); I get a

Here it can happen (in your code) that parentItem is 0x0. You should check for 
that before calling parentItem->childNumber(). Because (as you see) calling a 
function on a 0-object gives a segmentation fault...
So if parentIndex is 0x0, you should return and empty (and thus invalid) 
QModelIndex() instead of calling createIndex.

In all the functions returning a QModelIndex I end up adding lots of if's to 
check for the requisites, return a valid index only when all of them are 
fulfilled as needed and have the last line as "return QModelIndex();". That way 
nothing should crash.

And take a look at the ModelTest from Qt Labs, it helps to find errors in the 
models and has good documentation in the code to tell you what you did 
wrong...

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090321/3744b374/attachment.bin 


More information about the Qt-interest-old mailing list