From 96c27eb710a37780e79e09df2ebce1d5e4922c9d Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Tue, 25 Jun 2019 20:47:13 +0200 Subject: QList/Table/TreeWidgetItem: Allow reseting values by passing the default value The convenience functions setBackground(), setForeground() and setSizeHint() a default constructed value as 'reset'. This means a default constructed QBrush or QSize is returned in the data() function which leads to an unexpected background or forground color or size hint. Therefore check if the passed value is a default constructed value and set an empty QVariant instead which. [ChangeLog][QtWidgets][ItemViews] The convenience views QList/Table/TreeWidgetItem now treat a default constructed QBrush or QSize as an empty QVariant which allows to reset the values set to it's default values. Task-number: QTBUG-76423 Change-Id: I840570bbad3e5fd8c5b4b58903b4fd0066dbdeb7 Reviewed-by: Richard Moe Gustavsen --- .../itemviews/qlistwidget/tst_qlistwidget.cpp | 14 ++++++++++++ .../itemviews/qtablewidget/tst_qtablewidget.cpp | 25 +++++++++++++++++++++- .../itemviews/qtreewidget/tst_qtreewidget.cpp | 14 ++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp b/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp index cb083fdcbe..dcb932de66 100644 --- a/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp +++ b/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp @@ -1512,6 +1512,20 @@ void tst_QListWidget::itemData() QCOMPARE(flags.count(), 6); for (int i = 0; i < 4; ++i) QCOMPARE(flags[Qt::UserRole + i].toString(), QString::number(i + 1)); + + item.setBackground(QBrush(Qt::red)); + item.setForeground(QBrush(Qt::red)); + item.setSizeHint(QSize(10, 10)); + QCOMPARE(item.data(Qt::BackgroundRole), QVariant(QBrush(Qt::red))); + QCOMPARE(item.data(Qt::ForegroundRole), QVariant(QBrush(Qt::red))); + QCOMPARE(item.data(Qt::SizeHintRole), QVariant(QSize(10, 10))); + // an empty brush should result in a QVariant() + item.setBackground(QBrush()); + item.setForeground(QBrush()); + item.setSizeHint(QSize()); + QCOMPARE(item.data(Qt::BackgroundRole), QVariant()); + QCOMPARE(item.data(Qt::ForegroundRole), QVariant()); + QCOMPARE(item.data(Qt::SizeHintRole), QVariant()); } void tst_QListWidget::changeDataWithSorting() diff --git a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp index f640996690..38dae7743f 100644 --- a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp +++ b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp @@ -1404,9 +1404,17 @@ void tst_QTableWidget::setItemData() QCOMPARE(table.currentRoles, QVector({Qt::DisplayRole, Qt::EditRole, Qt::ToolTipRole})); QCOMPARE(table.model()->data(idx, Qt::DisplayRole).toString(), QLatin1String("Display")); + QCOMPARE(table.model()->data(idx, Qt::EditRole).toString(), QLatin1String("Display")); QCOMPARE(table.model()->data(idx, Qt::ToolTipRole).toString(), QLatin1String("ToolTip")); QCOMPARE(dataChangedSpy.count(), 1); - QCOMPARE(idx, qvariant_cast(dataChangedSpy.takeFirst().at(0))); + QCOMPARE(idx, qvariant_cast(dataChangedSpy.first().at(0))); + QCOMPARE(idx, qvariant_cast(dataChangedSpy.first().at(1))); + const auto roles = qvariant_cast>(dataChangedSpy.first().at(2)); + QCOMPARE(roles.size(), 3); + QVERIFY(roles.contains(Qt::DisplayRole)); + QVERIFY(roles.contains(Qt::EditRole)); + QVERIFY(roles.contains(Qt::ToolTipRole)); + dataChangedSpy.clear(); table.model()->setItemData(idx, data); QCOMPARE(dataChangedSpy.count(), 0); @@ -1416,6 +1424,21 @@ void tst_QTableWidget::setItemData() table.model()->setItemData(idx, data); QCOMPARE(table.model()->data(idx, Qt::DisplayRole).toString(), QLatin1String("dizplaye")); QCOMPARE(dataChangedSpy.count(), 1); + QCOMPARE(QVector({Qt::DisplayRole, Qt::EditRole}), qvariant_cast>(dataChangedSpy.first().at(2))); + + item->setBackground(QBrush(Qt::red)); + item->setForeground(QBrush(Qt::green)); + item->setSizeHint(QSize(10, 10)); + QCOMPARE(item->data(Qt::BackgroundRole), QVariant(QBrush(Qt::red))); + QCOMPARE(item->data(Qt::ForegroundRole), QVariant(QBrush(Qt::green))); + QCOMPARE(item->data(Qt::SizeHintRole), QVariant(QSize(10, 10))); + // an empty brush should result in a QVariant() + item->setBackground(QBrush()); + item->setForeground(QBrush()); + item->setSizeHint(QSize()); + QCOMPARE(item->data(Qt::BackgroundRole), QVariant()); + QCOMPARE(item->data(Qt::ForegroundRole), QVariant()); + QCOMPARE(item->data(Qt::SizeHintRole), QVariant()); } void tst_QTableWidget::cellWidget() diff --git a/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp b/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp index 2118bb5a29..7da56ab797 100644 --- a/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp +++ b/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp @@ -1994,6 +1994,20 @@ void tst_QTreeWidget::itemData() QCOMPARE(flags[Qt::UserRole + i].toString(), QString::number(i + 1)); flags = widget.model()->itemData(widget.model()->index(0, 1)); QCOMPARE(flags.count(), 0); + + item.setBackground(0, QBrush(Qt::red)); + item.setForeground(0, QBrush(Qt::green)); + item.setSizeHint(0, QSize(10, 10)); + QCOMPARE(item.data(0, Qt::BackgroundRole), QVariant(QBrush(Qt::red))); + QCOMPARE(item.data(0, Qt::ForegroundRole), QVariant(QBrush(Qt::green))); + QCOMPARE(item.data(0, Qt::SizeHintRole), QVariant(QSize(10, 10))); + // an empty brush should result in a QVariant() + item.setBackground(0, QBrush()); + item.setForeground(0, QBrush()); + item.setSizeHint(0, QSize()); + QCOMPARE(item.data(0, Qt::BackgroundRole), QVariant()); + QCOMPARE(item.data(0, Qt::ForegroundRole), QVariant()); + QCOMPARE(item.data(0, Qt::SizeHintRole), QVariant()); } void tst_QTreeWidget::enableDisable() -- cgit v1.2.3