Fix crash in QTreeWidgetItem::sortChildren when adding new item.

This is a cherry-pick of 4f388c383e39b598d997e21bd9a4f16d89bd0625
from February

Recursive call is caused if user code calls QtreeWidgetItem()::sortChildren and
sorting is enbled in QTreeWidget.

First call is from user code and second is caused by timer.
When timer expires second call is made.

This recursion is prevented with QTreeModel::SkipSorting skipSorting()
in QTreeWidgetItem::sortChildren();

Task-number: QTBUG-20345
Change-Id: Ibf73e69274423f31397a9e391bfba7d5c4103a3c
Reviewed-by: Markku Tapio Heikkilä <markku.heikkila@digia.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Markku Heikkila 2012-06-07 16:30:04 +02:00 committed by Qt by Nokia
parent a95e396a83
commit 9e75354aab
2 changed files with 34 additions and 0 deletions

View File

@ -2111,6 +2111,7 @@ void QTreeWidgetItem::sortChildren(int column, Qt::SortOrder order, bool climb)
return;
if (model->isChanging())
return;
QTreeModel::SkipSorting skipSorting(model);
int oldSortColumn = view->d_func()->explicitSortColumn;
view->d_func()->explicitSortColumn = column;
emit model->layoutAboutToBeChanged();

View File

@ -164,6 +164,7 @@ private slots:
void taskQTBUG2844_visualItemRect();
void setChildIndicatorPolicy();
void task20345_sortChildren();
public slots:
void itemSelectionChanged();
@ -3342,6 +3343,38 @@ void tst_QTreeWidget::setChildIndicatorPolicy()
QTRY_COMPARE(delegate.numPaints, 1);
}
void tst_QTreeWidget::task20345_sortChildren()
{
// This test case is considered successful if it is executed (no crash in sorting)
QTreeWidget tw;
tw.setColumnCount(3);
tw.headerItem()->setText(0, "Col 0");
tw.headerItem()->setText(1, "Col 1");
tw.header()->setSortIndicator(0, Qt::AscendingOrder);
tw.setSortingEnabled(true);
tw.show();
QTreeWidgetItem *rootItem = 0;
QTreeWidgetItem *childItem = 0;
rootItem = new QTreeWidgetItem(&tw, QStringList("a"));
childItem = new QTreeWidgetItem(rootItem);
childItem->setText(1, "3");
childItem = new QTreeWidgetItem(rootItem);
childItem->setText(1, "1");
childItem = new QTreeWidgetItem(rootItem);
childItem->setText(1, "2");
tw.setCurrentItem(tw.topLevelItem(0));
QTreeWidgetItem * curItem = tw.currentItem();
int childCount = curItem->childCount() + 1;
QTreeWidgetItem * newItem = new QTreeWidgetItem(curItem);
newItem->setText(1, QString::number(childCount));
rootItem->sortChildren(1, Qt::AscendingOrder);
QVERIFY(1);
}
QTEST_MAIN(tst_QTreeWidget)