path: root/src/widgets/itemviews/qtreewidget.cpp
diff options
authorGiuseppe D'Angelo <>2017-09-18 00:45:47 +0200
committerGiuseppe D'Angelo <>2017-11-30 13:01:48 +0000
commit259197d8df913e115f9d21c8f08201fb3c9f2dbc (patch)
tree694ad82b06321316317265929d988ef168729618 /src/widgets/itemviews/qtreewidget.cpp
parentbdcbbd7d5ba41ef3aeb24c12ef22ad2fdb7aac90 (diff)
QTreeWidget::setHeaderItem: fix off-by-one in signal emissions
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(<N), then one needs to begin removal from L to N-1 (and not N). Add the -1s needed. Change-Id: Ic669788825a1c480376a08df0d7c9c10f91552ef Reviewed-by: Thorbjørn Lund Martsum <>
Diffstat (limited to 'src/widgets/itemviews/qtreewidget.cpp')
1 files 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())
- else
+ else if (oldCount > item->columnCount())
d->treeModel()->headerDataChanged(Qt::Horizontal, 0, oldCount);