[Qt-interest] Slight change in Qt example gives segmentation fault
Stephen Jackson
spjackson42 at gmail.com
Tue Jan 26 23:51:57 CET 2010
2010/1/24 Gunnar :
> Hello there.
>
> I took the source code for the simpletreemodel example that is supplied with
> Qt 4.5 and compiled it, ran it, it worked fine.
>
> It can be found here:
> http://qt.nokia.com/doc/4.5/itemviews-simpletreemodel.html
>
> I thought I understood how it worked, but then I tried to add yet one more
> item to the tree I get a segmentation fault. The only change I've done is to
> add the following two lines to the constructor
>
>
> TreeModel::TreeModel(const QString &data, QObject *parent)
> : QAbstractItemModel(parent)
> {
> QList<QVariant> rootData;
> rootData << "Title" << "Summary";
> rootItem = new TreeItem(rootData);
> setupModelData(data.split(QString("\n")), rootItem);
>
> /* NEW CODE */
> TreeItem* x = new TreeItem(rootData);
> rootItem->appendChild(x);
> /* END NEW CODE */
>
> }
>
The implementation of TreeModel in the example assumes that all items
other than rootItem have a parent. If this is not the case, it crashes
in TreeModel::parent(). So you need to correct your new code as
follows:
TreeItem* x = new TreeItem(rootData, rootItem);
--
Cheers,
Stephen Jackson
More information about the Qt-interest-old
mailing list