[solved]QTreeView : Child-indicator is not updated after inserting children
-
Ok, I found a solution, but I think this is some kind of bug...
I inserted the child as follows:
@beginInsertRows(index, pItem->childrenCount(), pItem->childrenCount()); //index is the index of pItem, which is the parent I'm inserting to
new CTreeItem(pData, pItem);
endInsertRows();@
This should insert the new Item after the last child of pItem, and it does so when there are already children, if childrenCount() is 0 nothing happens. This is a bug in my opinion, because according to the documentation of QAbstractItemModel::beginInsertRows() the first item has index 0 and the parameter first specifies the new child-index my item will have (which should be 0 when it is the first item)Please correct me if I'm wrong... (Or tell me that I'm right ;)
Anyway, I fortunately use a QSortFilterProxyModel, so I do not have to care about the child-order in my model... now I'm just calling beginInsertRows(index, 1, 1) no matter if index already has children or not, and it works...
-
If you use
@
beginInsertRows(index, pItem->childrenCount(), pItem->childrenCount());
@then, with a parent item pItem without children, this literally translates to
@
beginInsertRows(index, 0, 0);
@so, actually you're telling your model that you are going to insert zero items. I would call no update on the parent item as the correct reaction of the model to that :-)
-
But then the documentation on that is a bit confusing... because it says the two ints are the positions of the new items...
From QAbstractItemModel::beginInsertRows()-documentation:
@The parent index corresponds to the parent into which the new rows are inserted; first and last are the row numbers that the new rows will have after they have been inserted.@...and if there aren't already items, the new one has of course position 0.
Which for my understanding means that beginInsertRows(index, 0, 0); should insert the new item at position 0, and because the last position is also 0, there's only one inserted.
If this is interpreted as a count, it's a "bug" in the doc ;)btw:
When I call beginInsertRows(index, 1, 1); it does work, but I get the debug message "QSortFilterProxyModel: invalid inserted rows reported by source model", so there has to be a better solution... -
Oh, you're right, of course. I didn't read the docs and stand corrected here.
It looks a bit weird, though. I don't have problems with that in my own models, it works as expected.
Do methods hasChildren() and rowCount() on the parent index return the correct values afterwards?
Maybe it can help to set the parent expanded, using slot expand().
-
I already tried expanding the parent, without any effect.
When I query hasChildren() and rowCount() on the index used as parent in beginInsertRows immediately after endInsertRows rowCount() returns 1, and hasChildren() returns true...edit:
it still returns 1 and true when i call the non-updating version beginInsertRows(index, pItem->childrenCount(), pItem->childrenCount())... (and no debug message in this case) -
[quote author="Volker" date="1343338405"]Can you create a small, self contained test case? It's hard to help further without the "working" failure.[/quote]
Lol... I already tried to create a small test-project containing the error for posting it here, but I didn't get the error again :D
I'll give it another try this weekend...But in the meantime we found out that the error is somehow related to the SortFilterProxyModel I use:
if I do not use this ProxyModel, everything works fine (without sorting and filtering of course)
In this ProxyModel I overwrite lessThan() and filterAcceptsRow(), and call setDynamicSortFilter(true) in the constructor... thats all it does.That's the code of my ProxyModel (shortened):
@
MySortModel::MySortModel(QObject *pParent)
: QSortFilterProxyModel(pParent)
{
setDynamicSortFilter(true);
c_pDataSource = NULL;
}//Needed for comparisons in lessThan and filterAcceptsRow
void MySortModel::setUserData(MyDataManagingClass *pDataSource)
{
c_pDataSource = pDataSource;
}bool MySortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
MyTreeItem *pLeftItem;
MyTreeItem *pRightItem;pLeftItem = static_cast<MyTreeItem*>(left.internalPointer());
pRightItem = static_cast<MyTreeItem*>(right.internalPointer());if (!pLeftItem || !pRightItem)
{
return false;
}//Do some comparisons and return true or false...
}bool MySortModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
MyTreeItem *pItem;
int iColumn;
QRegExp qrFilter;iColumn = filterKeyColumn();
qrFilter = filterRegExp();if (qrFilter.isEmpty() || !qrFilter.isValid())
{
//No filter, show all
return true;
}//Get Item to filter
if (!source_parent.isValid())
{
//pItem is Top-Level
pItem = static_cast<MyTreeItem*>((static_cast<MyModel*>(sourceModel()))->getBufferRoot()->getChild(source_row));
}
else
{
//pItem is Child
pItem = (static_cast<MyTreeItem*>(source_parent.child(source_row, 0).internalPointer()));
}if (!pItem)
{
return true;
}//Do some comparisons to determine if pItem is displayed and return true or false...
}
@in the class creating the QTreeView I do the following (again shortened):
@
MyWidget::MyWidget()
{
c_pSortModel = new MySortModel(this);
c_pSortModel->setUserData(pDataSource);
c_pModel = new MyModel(pDataSource); //c_pModel: Inherited from QAbstractItemModel
c_pSortModel->setSourceModel(c_pModel);c_pTreeView = new QTreeView(this); //Do some initializing on c_pTreeView and add it to this widgets layout c_pTreeView->setSortingEnabled(true); c_pTreeView->setModel(c_pSortModel); //call c_pTreeView->setModel(c_pModel) to avoid Bug
}
@and that's how the new Item is inserted:
@
//In a slot connected to a changed-signal emitted by my DataSource-class:
if (pItem) //previously determined; everything works fine to this point
{
beginInsertRows(index, pItem->childrenCount(), pItem->childrenCount()); //in the error-case childrenCount() returns 0 (correct, new Item is first child), so the new Item should become item 0 (the first), so everything should be fine...
new MyTreeItem(pData, pItem); //pItem is set as parent, the tree-structure is set up correctly
endInsertRows();
//calling reset() afterwards would help, but I don't want to do that for performance reasons
}
@So I hope I suceed in building a demo-project containing exact the same error, but as I already tried, I can't promise...
But perhaps the code I posted already helps... -
I wasn't able to reproduce the error in a small demo-project...
but I found another workaround:
emit layoutAboutToBeChanged() before and emit layoutChanged() after inserting the row helps.
And seems to be better than inserting as Item 1, 1 as that caused an assertion in qsortfilterproxymodel.cpp in some cases. The layoutChanged-version does not (and doesn't give me the invalid rows debug message)... -
If somebody is still interested in this:
The problem was the columnCount given by my model: QAbstractItemModel::columnCount() didn't return the same value for every index, and QTreeView doesn't seem to like that ;)
Now it always returns the same number and everything works fine.