QTreeWidgetItem: pass role to dataChanged() signal

QAbstractItemModel::dataChanged() gained an optional role parameter
with Qt5 which was not filled within QTreeWidgetItem setData() function

Change-Id: Ife87e9e046621b464ad340bd9eb451355730e93c
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Christian Ehrlicher 2018-02-02 20:19:25 +01:00
parent f9fa3e2553
commit 85f6dd8325
4 changed files with 32 additions and 13 deletions

View File

@ -777,7 +777,7 @@ bool QTreeModel::isChanging() const
if column is -1 then all columns have changed if column is -1 then all columns have changed
*/ */
void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column) void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column, const QVector<int> &roles)
{ {
if (signalsBlocked()) if (signalsBlocked())
return; return;
@ -800,7 +800,7 @@ void QTreeModel::emitDataChanged(QTreeWidgetItem *item, int column)
topLeft = index(item, column); topLeft = index(item, column);
bottomRight = topLeft; bottomRight = topLeft;
} }
emit dataChanged(topLeft, bottomRight); emit dataChanged(topLeft, bottomRight, roles);
} }
void QTreeModel::beginInsertItems(QTreeWidgetItem *parent, int row, int count) void QTreeModel::beginInsertItems(QTreeWidgetItem *parent, int row, int count)
@ -1766,11 +1766,14 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
} }
if (model) { if (model) {
model->emitDataChanged(this, column); const QVector<int> roles((role == Qt::DisplayRole || role == Qt::EditRole) ?
QVector<int>({Qt::DisplayRole, Qt::EditRole}) :
QVector<int>({role}));
model->emitDataChanged(this, column, roles);
if (role == Qt::CheckStateRole) { if (role == Qt::CheckStateRole) {
QTreeWidgetItem *p; QTreeWidgetItem *p;
for (p = par; p && (p->itemFlags & Qt::ItemIsAutoTristate); p = p->par) for (p = par; p && (p->itemFlags & Qt::ItemIsAutoTristate); p = p->par)
model->emitDataChanged(p, column); model->emitDataChanged(p, column, roles);
} }
} }
} }

View File

@ -339,6 +339,7 @@ Q_SIGNALS:
void itemDoubleClicked(QTreeWidgetItem *item, int column); void itemDoubleClicked(QTreeWidgetItem *item, int column);
void itemActivated(QTreeWidgetItem *item, int column); void itemActivated(QTreeWidgetItem *item, int column);
void itemEntered(QTreeWidgetItem *item, int column); void itemEntered(QTreeWidgetItem *item, int column);
// ### Qt 6: add changed roles
void itemChanged(QTreeWidgetItem *item, int column); void itemChanged(QTreeWidgetItem *item, int column);
void itemExpanded(QTreeWidgetItem *item); void itemExpanded(QTreeWidgetItem *item);
void itemCollapsed(QTreeWidgetItem *item); void itemCollapsed(QTreeWidgetItem *item);

View File

@ -139,7 +139,7 @@ public:
protected: protected:
QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = 0); QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = 0);
void emitDataChanged(QTreeWidgetItem *item, int column); void emitDataChanged(QTreeWidgetItem *item, int column, const QVector<int> &roles);
void beginInsertItems(QTreeWidgetItem *parent, int row, int count); void beginInsertItems(QTreeWidgetItem *parent, int row, int count);
void endInsertItems(); void endInsertItems();
void beginRemoveItems(QTreeWidgetItem *parent, int row, int count); void beginRemoveItems(QTreeWidgetItem *parent, int row, int count);

View File

@ -1938,23 +1938,38 @@ void tst_QTreeWidget::setData()
} }
} }
class QTreeWidgetDataChanged : public QTreeWidget
{
Q_OBJECT
public:
using QTreeWidget::QTreeWidget;
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles) override
{
QTreeWidget::dataChanged(topLeft, bottomRight, roles);
currentRoles = roles;
}
QVector<int> currentRoles;
};
void tst_QTreeWidget::itemData() void tst_QTreeWidget::itemData()
{ {
QTreeWidget widget; QTreeWidgetDataChanged widget;
QTreeWidgetItem item(&widget); QTreeWidgetItem item(&widget);
widget.setColumnCount(2); widget.setColumnCount(2);
item.setFlags(item.flags() | Qt::ItemIsEditable); item.setFlags(item.flags() | Qt::ItemIsEditable);
item.setData(0, Qt::DisplayRole, QString("0")); item.setData(0, Qt::DisplayRole, QString("0"));
QCOMPARE(widget.currentRoles, QVector<int>({Qt::DisplayRole, Qt::EditRole}));
item.setData(0, Qt::CheckStateRole, Qt::PartiallyChecked); item.setData(0, Qt::CheckStateRole, Qt::PartiallyChecked);
item.setData(0, Qt::UserRole + 0, QString("1")); QCOMPARE(widget.currentRoles, {Qt::CheckStateRole});
item.setData(0, Qt::UserRole + 1, QString("2")); for (int i = 0; i < 4; ++i) {
item.setData(0, Qt::UserRole + 2, QString("3")); item.setData(0, Qt::UserRole + i, QString::number(i + 1));
item.setData(0, Qt::UserRole + 3, QString("4")); QCOMPARE(widget.currentRoles, {Qt::UserRole + i});
}
QMap<int, QVariant> flags = widget.model()->itemData(widget.model()->index(0, 0)); QMap<int, QVariant> flags = widget.model()->itemData(widget.model()->index(0, 0));
QCOMPARE(flags.count(), 6); 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));
flags = widget.model()->itemData(widget.model()->index(0, 1)); flags = widget.model()->itemData(widget.model()->index(0, 1));
QCOMPARE(flags.count(), 0); QCOMPARE(flags.count(), 0);
} }