diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2019-06-25 20:47:13 +0200 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2019-11-22 22:02:15 +0100 |
commit | 96c27eb710a37780e79e09df2ebce1d5e4922c9d (patch) | |
tree | 2b5a92fd4a65c8e79ef1660f425b7d2e5f3b4165 | |
parent | 2c871dfd38d89d6415c2c25d47b4a0e9c8b2ef11 (diff) | |
download | qtbase-96c27eb710a37780e79e09df2ebce1d5e4922c9d.tar.gz |
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 <richard.gustavsen@qt.io>
-rw-r--r-- | src/widgets/itemviews/qlistwidget.cpp | 9 | ||||
-rw-r--r-- | src/widgets/itemviews/qlistwidget.h | 6 | ||||
-rw-r--r-- | src/widgets/itemviews/qtablewidget.cpp | 8 | ||||
-rw-r--r-- | src/widgets/itemviews/qtablewidget.h | 6 | ||||
-rw-r--r-- | src/widgets/itemviews/qtreewidget.cpp | 8 | ||||
-rw-r--r-- | src/widgets/itemviews/qtreewidget.h | 6 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp | 14 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp | 25 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp | 14 |
9 files changed, 80 insertions, 16 deletions
diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index e7dcfac403..1931360dbc 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -987,8 +987,9 @@ QDataStream &operator>>(QDataStream &in, QListWidgetItem &item) \fn void QListWidgetItem::setSizeHint(const QSize &size) \since 4.1 - Sets the size hint for the list item to be \a size. If no size hint is set, - the item delegate will compute the size hint based on the item data. + Sets the size hint for the list item to be \a size. + If no size hint is set or \a size is invalid, the item + delegate will compute the size hint based on the item data. */ /*! @@ -1119,6 +1120,8 @@ void QListWidgetItem::setFlags(Qt::ItemFlags aflags) \since 4.2 Sets the background brush of the list item to the given \a brush. + Setting a default-constructed brush will let the view use the + default color from the style. \sa background(), setForeground() */ @@ -1137,6 +1140,8 @@ void QListWidgetItem::setFlags(Qt::ItemFlags aflags) \since 4.2 Sets the foreground brush of the list item to the given \a brush. + Setting a default-constructed brush will let the view use the + default color from the style. \sa foreground(), setBackground() */ diff --git a/src/widgets/itemviews/qlistwidget.h b/src/widgets/itemviews/qlistwidget.h index c102b144df..1319d658ab 100644 --- a/src/widgets/itemviews/qlistwidget.h +++ b/src/widgets/itemviews/qlistwidget.h @@ -127,7 +127,7 @@ public: inline QBrush background() const { return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); } inline void setBackground(const QBrush &brush) - { setData(Qt::BackgroundRole, brush); } + { setData(Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } #if QT_DEPRECATED_SINCE(5, 13) QT_DEPRECATED_X ("Use QListWidgetItem::foreground() instead") @@ -141,7 +141,7 @@ public: inline QBrush foreground() const { return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); } inline void setForeground(const QBrush &brush) - { setData(Qt::ForegroundRole, brush); } + { setData(Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } inline Qt::CheckState checkState() const { return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); } @@ -151,7 +151,7 @@ public: inline QSize sizeHint() const { return qvariant_cast<QSize>(data(Qt::SizeHintRole)); } inline void setSizeHint(const QSize &size) - { setData(Qt::SizeHintRole, size); } + { setData(Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); } virtual QVariant data(int role) const; virtual void setData(int role, const QVariant &value); diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp index b1dbafa997..91860341ee 100644 --- a/src/widgets/itemviews/qtablewidget.cpp +++ b/src/widgets/itemviews/qtablewidget.cpp @@ -1064,8 +1064,8 @@ QTableWidgetSelectionRange::~QTableWidgetSelectionRange() \since 4.1 Sets the size hint for the table item to be \a size. - If no size hint is set, the item delegate will compute the - size hint based on the item data. + If no size hint is set or \a size is invalid, the item + delegate will compute the size hint based on the item data. */ /*! @@ -1279,6 +1279,8 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags) \since 4.2 Sets the item's background brush to the specified \a brush. + Setting a default-constructed brush will let the view use the + default color from the style. \sa setForeground() */ @@ -1313,6 +1315,8 @@ void QTableWidgetItem::setFlags(Qt::ItemFlags aflags) \since 4.2 Sets the item's foreground brush to the specified \a brush. + Setting a default-constructed brush will let the view use the + default color from the style. \sa setBackground() */ diff --git a/src/widgets/itemviews/qtablewidget.h b/src/widgets/itemviews/qtablewidget.h index 0d93a0a075..70e2046400 100644 --- a/src/widgets/itemviews/qtablewidget.h +++ b/src/widgets/itemviews/qtablewidget.h @@ -146,7 +146,7 @@ public: inline QBrush background() const { return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); } inline void setBackground(const QBrush &brush) - { setData(Qt::BackgroundRole, brush); } + { setData(Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } #if QT_DEPRECATED_SINCE(5, 13) QT_DEPRECATED_X ("Use QTableWidgetItem::foreground() instead") @@ -160,7 +160,7 @@ public: inline QBrush foreground() const { return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); } inline void setForeground(const QBrush &brush) - { setData(Qt::ForegroundRole, brush); } + { setData(Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } inline Qt::CheckState checkState() const { return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); } @@ -170,7 +170,7 @@ public: inline QSize sizeHint() const { return qvariant_cast<QSize>(data(Qt::SizeHintRole)); } inline void setSizeHint(const QSize &size) - { setData(Qt::SizeHintRole, size); } + { setData(Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); } virtual QVariant data(int role) const; virtual void setData(int role, const QVariant &value); diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp index 6d0909108b..cce0773fec 100644 --- a/src/widgets/itemviews/qtreewidget.cpp +++ b/src/widgets/itemviews/qtreewidget.cpp @@ -1285,6 +1285,8 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const Sets the background brush of the label in the given \a column to the specified \a brush. + Setting a default-constructed brush will let the view use the + default color from the style. \note If \l{Qt Style Sheets} are used on the same widget as setBackground(), style sheets will take precedence if the settings conflict. @@ -1314,6 +1316,8 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const Returns the brush used to render the foreground (e.g. text) of the specified \a column. + Setting a default-constructed brush will let the view use the + default color from the style. \sa background() */ @@ -1357,8 +1361,8 @@ bool QTreeWidgetItem::isFirstColumnSpanned() const \since 4.1 Sets the size hint for the tree item in the given \a column to be \a size. - If no size hint is set, the item delegate will compute the size hint based - on the item data. + If no size hint is set or \a size is invalid, the item + delegate will compute the size hint based on the item data. */ /*! diff --git a/src/widgets/itemviews/qtreewidget.h b/src/widgets/itemviews/qtreewidget.h index bed77b336d..b9543fb954 100644 --- a/src/widgets/itemviews/qtreewidget.h +++ b/src/widgets/itemviews/qtreewidget.h @@ -146,7 +146,7 @@ public: inline QBrush background(int column) const { return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); } inline void setBackground(int column, const QBrush &brush) - { setData(column, Qt::BackgroundRole, brush); } + { setData(column, Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } #if QT_DEPRECATED_SINCE(5, 13) QT_DEPRECATED_X ("Use QTreeWidgetItem::foreground() instead") @@ -160,7 +160,7 @@ public: inline QBrush foreground(int column) const { return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); } inline void setForeground(int column, const QBrush &brush) - { setData(column, Qt::ForegroundRole, brush); } + { setData(column, Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); } inline Qt::CheckState checkState(int column) const { return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt()); } @@ -170,7 +170,7 @@ public: inline QSize sizeHint(int column) const { return qvariant_cast<QSize>(data(column, Qt::SizeHintRole)); } inline void setSizeHint(int column, const QSize &size) - { setData(column, Qt::SizeHintRole, size); } + { setData(column, Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); } virtual QVariant data(int column, int role) const; virtual void setData(int column, int role, const QVariant &value); 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<int>({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<QModelIndex>(dataChangedSpy.takeFirst().at(0))); + QCOMPARE(idx, qvariant_cast<QModelIndex>(dataChangedSpy.first().at(0))); + QCOMPARE(idx, qvariant_cast<QModelIndex>(dataChangedSpy.first().at(1))); + const auto roles = qvariant_cast<QVector<int>>(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<int>({Qt::DisplayRole, Qt::EditRole}), qvariant_cast<QVector<int>>(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() |