PDA

View Full Version : A top level QStandardItem parent() doesn't return the invisible root it returns null



cale1
14th January 2013, 19:19
Recently I switched over from QT 4.8 to QT 5.0.

I'm using the QStandardItemModel to create a tree, and I found that the QStandardItem's parent() function has slightly changed. If the QStandardItem is a top level item then instead of parent() returning its parent (A.K.A. the invisible root item), it returns null. The QT 5.0 Documentation for the QStandardItem (https://qt-project.org/doc/qt-5.0/qtgui/qstandarditem.html#parent) has a note that documents this unusual behavior, but this is not the same functionality that was in QT 4.8 (see QT 4.8 Documentation for the QStandardItem (https://qt-project.org/doc/qt-4.8/qstandarditem.html#parent)).

Why was this changed from how it functioned in QT 4.8, which had a top level QStandardItem parent() return the invisible root item?

Now I seem to be forced to add an if statement checking if the parent is null and then making the assumption that if it is null then the parent must be the invisible root item. And I'm not confident that this assumption is always true.



if(item->parent() != nullptr)
item->parent()->removeRow(item->row());
else
mItemModel->invisibleRootItem()->removeRow(item->row());


When searching for comments on this issue on the web, I found a bug report (https://bugreports.qt-project.org/browse/QTBUG-18785) dealing with this same issue addressed to QT 4.7.2. It is marked as resolved for QT 5.0, but if you ask me, it was properly fixed in QT 4.8, and now QT 5.0 took a step backwards to the same funcationality as QT 4.7.2 and merely documented the strange behavior addressed in the bug report (https://bugreports.qt-project.org/browse/QTBUG-18785).

In short, a QStandardItem shouldn't function differently depending on where it is in the tree.