QTableWidget: fix handling of Qt::EditRole/DisplayRole in setItemData()

QTableModel::setItemData() did not treat Qt::EditRole and
Qt::DisplayRole as the same. This lead to inconsistencies between
setItemData() and QTableWidgetItem::setData()

[ChangeLog][QtWidgets][QTableWidget] Fix handling of Qt::EditRole and
Qt::DisplayRole in setItemData().

Change-Id: I456f4c8e654de701dcd579236162b8aaa8ba1e53
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Christian Ehrlicher 2018-05-03 21:11:57 +02:00
parent 432df3f8c8
commit 71a1df5456
2 changed files with 8 additions and 4 deletions

View File

@ -456,9 +456,12 @@ bool QTableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant
itm->view = nullptr; // prohibits item from calling itemChanged()
QVector<int> rolesVec;
for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
if (itm->data(it.key()) != it.value()) {
itm->setData(it.key(), it.value());
rolesVec += it.key();
const int role = (it.key() == Qt::EditRole ? Qt::DisplayRole : it.key());
if (itm->data(role) != it.value()) {
itm->setData(role, it.value());
rolesVec += role;
if (role == Qt::DisplayRole)
rolesVec += Qt::EditRole;
}
}
itm->view = view;

View File

@ -1381,7 +1381,7 @@ void tst_QTableWidget::itemData()
void tst_QTableWidget::setItemData()
{
QTableWidget table(10, 10);
QTableWidgetDataChanged table(10, 10);
table.setSortingEnabled(false);
QSignalSpy dataChangedSpy(table.model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)));
@ -1394,6 +1394,7 @@ void tst_QTableWidget::setItemData()
data.insert(Qt::DisplayRole, QLatin1String("Display"));
data.insert(Qt::ToolTipRole, QLatin1String("ToolTip"));
table.model()->setItemData(idx, data);
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::ToolTipRole).toString(), QLatin1String("ToolTip"));