From fa67cd03342ebe72b0438a0383c69105c5e5870b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=A4=C3=A4tt=C3=A4?= Date: Tue, 2 May 2023 12:39:59 +0300 Subject: [PATCH] Use boolean to indicate QTableWidgetItem is header item QTableWidgetItem uses additional enum flag ItemIsHeaderItem which has the same numerical value as ItemFlag ItemNeverHasChildren. This causes conflict since the user can set the latter flag using the setFlags, while the ItemIsHeaderItem is only used internally to mark header items. Remove the additional flag and use boolean instead to fix the conflict. Pick-to: 6.2 6.5 6.5.1 Fixes: QTBUG-113209 Change-Id: Icff549c7e452d9f84575a524361719204817274e Reviewed-by: Volker Hilsheimer --- src/widgets/itemviews/qtablewidget.cpp | 10 +++++----- src/widgets/itemviews/qtablewidget_p.h | 7 ++----- .../qtablewidget/tst_qtablewidget.cpp | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp index 67886be94f..f9d2c8d59c 100644 --- a/src/widgets/itemviews/qtablewidget.cpp +++ b/src/widgets/itemviews/qtablewidget.cpp @@ -248,7 +248,7 @@ void QTableModel::setHorizontalHeaderItem(int section, QTableWidgetItem *item) if (item) { item->view = view; - item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem); + item->d->headerItem = true; } horizontalHeaderItems[section] = item; emit headerDataChanged(Qt::Horizontal, section, section); @@ -270,7 +270,7 @@ void QTableModel::setVerticalHeaderItem(int section, QTableWidgetItem *item) if (item) { item->view = view; - item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem); + item->d->headerItem = true; } verticalHeaderItems[section] = item; emit headerDataChanged(Qt::Vertical, section, section); @@ -283,7 +283,7 @@ QTableWidgetItem *QTableModel::takeHorizontalHeaderItem(int section) QTableWidgetItem *itm = horizontalHeaderItems.at(section); if (itm) { itm->view = nullptr; - itm->itemFlags &= ~ItemIsHeaderItem; + itm->d->headerItem = false; horizontalHeaderItems[section] = 0; } return itm; @@ -296,7 +296,7 @@ QTableWidgetItem *QTableModel::takeVerticalHeaderItem(int section) QTableWidgetItem *itm = verticalHeaderItems.at(section); if (itm) { itm->view = nullptr; - itm->itemFlags &= ~ItemIsHeaderItem; + itm->d->headerItem = false; verticalHeaderItems[section] = 0; } return itm; @@ -761,7 +761,7 @@ void QTableModel::itemChanged(QTableWidgetItem *item, const QList &roles) { if (!item) return; - if (item->flags() & ItemIsHeaderItem) { + if (item->d->headerItem) { int row = verticalHeaderItems.indexOf(item); if (row >= 0) { emit headerDataChanged(Qt::Vertical, row, row); diff --git a/src/widgets/itemviews/qtablewidget_p.h b/src/widgets/itemviews/qtablewidget_p.h index 1d893224c0..6bfa355edb 100644 --- a/src/widgets/itemviews/qtablewidget_p.h +++ b/src/widgets/itemviews/qtablewidget_p.h @@ -53,10 +53,6 @@ class QTableModel : public QAbstractTableModel friend class QTableWidget; public: - enum ItemFlagsExtension { - ItemIsHeaderItem = 128 - }; // we need this to separate header items from other items - QTableModel(int rows, int columns, QTableWidget *parent); ~QTableModel(); @@ -173,9 +169,10 @@ public: class QTableWidgetItemPrivate { public: - QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1) {} + QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1), headerItem(false) {} QTableWidgetItem *q; int id; + bool headerItem; // Qt 7 TODO: inline this stuff in the public class. }; QT_END_NAMESPACE diff --git a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp index 02d93bd356..2892c124d6 100644 --- a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp +++ b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp @@ -63,6 +63,7 @@ private slots: void task219380_removeLastRow(); void task262056_sortDuplicate(); void itemWithHeaderItems(); + void checkHeaderItemFlagsConflict(); void mimeData(); void selectedRowAfterSorting(); void search(); @@ -1669,6 +1670,25 @@ void tst_QTableWidget::itemWithHeaderItems() QCOMPARE(table.item(0, 1), nullptr); } +void tst_QTableWidget::checkHeaderItemFlagsConflict() +{ + // QTBUG-113209 + // Check that setting header item doesn't set Qt::ItemNeverHasChildren + // Chech that header items do not emit itemChanged. + QTableWidget table(1, 1); + QSignalSpy itemChangeSpy(&table, &QTableWidget::itemChanged); + QVERIFY(itemChangeSpy.isValid()); + + QTableWidgetItem *item = new QTableWidgetItem("Initial"); + table.setHorizontalHeaderItem(0, item); + + QVERIFY(!(item->flags() & Qt::ItemNeverHasChildren)); + + item->setData(Qt::DisplayRole, "updated"); + + QCOMPARE(itemChangeSpy.size(), 0); +} + class TestTableWidget : public QTableWidget { Q_OBJECT