From 4f388c383e39b598d997e21bd9a4f16d89bd0625 Mon Sep 17 00:00:00 2001 From: Markku Heikkila Date: Fri, 27 Jan 2012 13:53:09 +0200 Subject: Fix crash in QTreeWidgetItem::sortChildren when adding new item. 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: I48af98b03c0291de8da90d1581bc55cc89846b8c Reviewed-by: Stephen Kelly --- src/gui/itemviews/qtreewidget.cpp | 1 + tests/auto/qtreewidget/tst_qtreewidget.cpp | 32 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/gui/itemviews/qtreewidget.cpp b/src/gui/itemviews/qtreewidget.cpp index f851688..2816966 100644 --- a/src/gui/itemviews/qtreewidget.cpp +++ b/src/gui/itemviews/qtreewidget.cpp @@ -2108,6 +2108,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(); diff --git a/tests/auto/qtreewidget/tst_qtreewidget.cpp b/tests/auto/qtreewidget/tst_qtreewidget.cpp index e8a1de3..4957e3c 100644 --- a/tests/auto/qtreewidget/tst_qtreewidget.cpp +++ b/tests/auto/qtreewidget/tst_qtreewidget.cpp @@ -172,6 +172,7 @@ private slots: void taskQTBUG2844_visualItemRect(); void setChildIndicatorPolicy(); + void task20345_sortChildren(); public slots: void itemSelectionChanged(); @@ -3366,7 +3367,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) #include "tst_qtreewidget.moc" -- cgit v0.12