QTableWidgetItem: pass role to dataChanged() signal
QAbstractItemModel::dataChanged() gained an optional role parameter with Qt5 which was not filled within QTableWidgetItem setData() function Task-number: QTBUG-48295 Change-Id: I82289b6db78eeef09d586da267046032984952da Reviewed-by: Samuel Gaist <samuel.gaist@edeltech.ch> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
parent
5adf18909d
commit
d02e7b46a1
@ -453,17 +453,17 @@ bool QTableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant
|
||||
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
|
||||
QTableWidgetItem *itm = item(index);
|
||||
if (itm) {
|
||||
itm->view = 0; // prohibits item from calling itemChanged()
|
||||
bool changed = false;
|
||||
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());
|
||||
changed = true;
|
||||
rolesVec += it.key();
|
||||
}
|
||||
}
|
||||
itm->view = view;
|
||||
if (changed)
|
||||
itemChanged(itm);
|
||||
if (!rolesVec.isEmpty())
|
||||
itemChanged(itm, rolesVec);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -771,7 +771,7 @@ void QTableModel::clearContents()
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void QTableModel::itemChanged(QTableWidgetItem *item)
|
||||
void QTableModel::itemChanged(QTableWidgetItem *item, const QVector<int> &roles)
|
||||
{
|
||||
if (!item)
|
||||
return;
|
||||
@ -787,7 +787,7 @@ void QTableModel::itemChanged(QTableWidgetItem *item)
|
||||
} else {
|
||||
QModelIndex idx = index(item);
|
||||
if (idx.isValid())
|
||||
emit dataChanged(idx, idx);
|
||||
emit dataChanged(idx, idx, roles);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1386,8 +1386,13 @@ void QTableWidgetItem::setData(int role, const QVariant &value)
|
||||
}
|
||||
if (!found)
|
||||
values.append(QWidgetItemData(role, value));
|
||||
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0))
|
||||
model->itemChanged(this);
|
||||
if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : nullptr))
|
||||
{
|
||||
const QVector<int> roles((role == Qt::DisplayRole) ?
|
||||
QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
|
||||
QVector<int>({role}));
|
||||
model->itemChanged(this, roles);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -302,6 +302,7 @@ Q_SIGNALS:
|
||||
|
||||
void itemActivated(QTableWidgetItem *item);
|
||||
void itemEntered(QTableWidgetItem *item);
|
||||
// ### Qt 6: add changed roles
|
||||
void itemChanged(QTableWidgetItem *item);
|
||||
|
||||
void currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous);
|
||||
|
@ -157,7 +157,7 @@ public:
|
||||
|
||||
void clear();
|
||||
void clearContents();
|
||||
void itemChanged(QTableWidgetItem *item);
|
||||
void itemChanged(QTableWidgetItem *item, const QVector<int> &roles = QVector<int>());
|
||||
|
||||
QTableWidgetItem *createItem() const;
|
||||
const QTableWidgetItem *itemPrototype() const;
|
||||
|
@ -1343,22 +1343,40 @@ void tst_QTableWidget::setItemWithSorting()
|
||||
}
|
||||
}
|
||||
|
||||
class QTableWidgetDataChanged : public QTableWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
using QTableWidget::QTableWidget;
|
||||
|
||||
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) override
|
||||
{
|
||||
QTableWidget::dataChanged(topLeft, bottomRight, roles);
|
||||
currentRoles = roles;
|
||||
}
|
||||
QVector<int> currentRoles;
|
||||
};
|
||||
|
||||
void tst_QTableWidget::itemData()
|
||||
{
|
||||
QTableWidget widget(2, 2);
|
||||
QTableWidgetDataChanged widget(2, 2);
|
||||
widget.setItem(0, 0, new QTableWidgetItem());
|
||||
QTableWidgetItem *item = widget.item(0, 0);
|
||||
QVERIFY(item);
|
||||
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
||||
item->setData(Qt::DisplayRole, QString("0"));
|
||||
QCOMPARE(widget.currentRoles, QVector<int>({Qt::DisplayRole, Qt::EditRole}));
|
||||
item->setData(Qt::CheckStateRole, Qt::PartiallyChecked);
|
||||
item->setData(Qt::UserRole + 0, QString("1"));
|
||||
item->setData(Qt::UserRole + 1, QString("2"));
|
||||
item->setData(Qt::UserRole + 2, QString("3"));
|
||||
item->setData(Qt::UserRole + 3, QString("4"));
|
||||
QCOMPARE(widget.currentRoles, {Qt::CheckStateRole});
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
item->setData(Qt::UserRole + i, QString::number(i + 1));
|
||||
QCOMPARE(widget.currentRoles, {Qt::UserRole + i});
|
||||
}
|
||||
QMap<int, QVariant> flags = widget.model()->itemData(widget.model()->index(0, 0));
|
||||
QCOMPARE(flags.count(), 6);
|
||||
QCOMPARE(flags[(Qt::UserRole + 0)].toString(), QString("1"));
|
||||
for (int i = 0; i < 4; ++i)
|
||||
QCOMPARE(flags[Qt::UserRole + i].toString(), QString::number(i + 1));
|
||||
}
|
||||
|
||||
void tst_QTableWidget::setItemData()
|
||||
|
Loading…
Reference in New Issue
Block a user