From 259197d8df913e115f9d21c8f08201fb3c9f2dbc Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 18 Sep 2017 00:45:47 +0200 Subject: QTreeWidget::setHeaderItem: fix off-by-one in signal emissions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When setting a header item (that is, the item that provides the QTreeWidget's column) the widget needs to manipulate the underlying tree model and add or remove columns. This requires calling the right QAbstractItemModel APIs for structural model changes. The calculations done resulted in a off-by-one error: * if the model had N columns and needs to grow to M(>N), then one needs to begin insertion from N to M-1 (and not M); * if the model had N columns and needs to shrink to L( --- src/widgets/itemviews/qtreewidget.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp index 334a3e0c75..e5f73407cd 100644 --- a/src/widgets/itemviews/qtreewidget.cpp +++ b/src/widgets/itemviews/qtreewidget.cpp @@ -2729,14 +2729,14 @@ void QTreeWidget::setHeaderItem(QTreeWidgetItem *item) int oldCount = columnCount(); if (oldCount < item->columnCount()) - d->treeModel()->beginInsertColumns(QModelIndex(), oldCount, item->columnCount()); - else - d->treeModel()->beginRemoveColumns(QModelIndex(), item->columnCount(), oldCount); + d->treeModel()->beginInsertColumns(QModelIndex(), oldCount, item->columnCount() - 1); + else if (oldCount > item->columnCount()) + d->treeModel()->beginRemoveColumns(QModelIndex(), item->columnCount(), oldCount - 1); delete d->treeModel()->headerItem; d->treeModel()->headerItem = item; if (oldCount < item->columnCount()) d->treeModel()->endInsertColumns(); - else + else if (oldCount > item->columnCount()) d->treeModel()->endRemoveColumns(); d->treeModel()->headerDataChanged(Qt::Horizontal, 0, oldCount); } -- cgit v1.2.3