Add a method to clear the data to QStandardItemModel
After the behavior of setItemData has been changed following QTBUG-45114, QStandardItemModel was lacking an interface to clear all the data from a single index. Task-number: QTBUG-69616 Change-Id: Ide0b5bb6358439fc42c474df8b044fbace6def8d Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: David Faure <david.faure@kdab.com>bb10
parent
67352c9276
commit
d0069ff8c9
|
|
@ -1866,6 +1866,23 @@ bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value
|
|||
return false;
|
||||
}
|
||||
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||
/*!
|
||||
\since 6.0
|
||||
Removes the data stored in all the roles for the given \a index.
|
||||
Returns \c{true} if successful; otherwise returns \c{false}.
|
||||
The dataChanged() signal should be emitted if the data was successfully
|
||||
removed.
|
||||
The base class implementation returns \c{false}
|
||||
\sa data(), itemData(), setData(), setItemData()
|
||||
*/
|
||||
bool QAbstractItemModel::clearItemData(const QModelIndex &index)
|
||||
{
|
||||
Q_UNUSED(index);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*!
|
||||
\fn QVariant QAbstractItemModel::data(const QModelIndex &index, int role) const = 0
|
||||
|
||||
|
|
|
|||
|
|
@ -198,6 +198,9 @@ public:
|
|||
|
||||
virtual QMap<int, QVariant> itemData(const QModelIndex &index) const;
|
||||
virtual bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||
virtual bool clearItemData(const QModelIndex &index);
|
||||
#endif
|
||||
|
||||
virtual QStringList mimeTypes() const;
|
||||
virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
|
||||
|
|
|
|||
|
|
@ -930,6 +930,21 @@ void QStandardItem::setData(const QVariant &value, int role)
|
|||
d->model->d_func()->itemChanged(this, roles);
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.12
|
||||
Removes all the data from all roles previously set.
|
||||
\sa data(), setData()
|
||||
*/
|
||||
void QStandardItem::clearData()
|
||||
{
|
||||
Q_D(QStandardItem);
|
||||
if (d->values.isEmpty())
|
||||
return;
|
||||
d->values.clear();
|
||||
if (d->model)
|
||||
d->model->d_func()->itemChanged(this, QVector<int>{});
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the item's data for the given \a role, or an invalid
|
||||
QVariant if there is no data for the role.
|
||||
|
|
@ -2990,6 +3005,23 @@ bool QStandardItemModel::setData(const QModelIndex &index, const QVariant &value
|
|||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.12
|
||||
Removes the data stored in all the roles for the given \a index.
|
||||
\sa setData(), data()
|
||||
*/
|
||||
bool QStandardItemModel::clearItemData(const QModelIndex &index)
|
||||
{
|
||||
if (!checkIndex(index, CheckIndexOption::IndexIsValid))
|
||||
return false;
|
||||
Q_D(QStandardItemModel);
|
||||
QStandardItem *item = d->itemFromIndex(index);
|
||||
if (!item)
|
||||
return false;
|
||||
item->clearData();
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
\reimp
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ public:
|
|||
|
||||
virtual QVariant data(int role = Qt::UserRole + 1) const;
|
||||
virtual void setData(const QVariant &value, int role = Qt::UserRole + 1);
|
||||
void clearData();
|
||||
|
||||
inline QString text() const {
|
||||
return qvariant_cast<QString>(data(Qt::DisplayRole));
|
||||
|
|
@ -343,6 +344,8 @@ public:
|
|||
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
// Qt 6: add override keyword
|
||||
bool clearItemData(const QModelIndex &index);
|
||||
|
||||
QVariant headerData(int section, Qt::Orientation orientation,
|
||||
int role = Qt::DisplayRole) const override;
|
||||
|
|
|
|||
|
|
@ -69,8 +69,20 @@ private slots:
|
|||
void sortChildren();
|
||||
void subclassing();
|
||||
void lessThan();
|
||||
void clearData();
|
||||
};
|
||||
|
||||
void tst_QStandardItem::clearData()
|
||||
{
|
||||
QStandardItem item;
|
||||
item.setData(QStringLiteral("Test"), Qt::EditRole);
|
||||
item.setData(5, Qt::UserRole);
|
||||
item.clearData();
|
||||
QCOMPARE(item.data(Qt::EditRole), QVariant());
|
||||
QCOMPARE(item.data(Qt::UserRole), QVariant());
|
||||
QCOMPARE(item.data(Qt::DisplayRole), QVariant());
|
||||
}
|
||||
|
||||
void tst_QStandardItem::ctor()
|
||||
{
|
||||
QStandardItem item;
|
||||
|
|
|
|||
|
|
@ -98,6 +98,7 @@ private slots:
|
|||
void checkChildren();
|
||||
void data();
|
||||
void clear();
|
||||
void clearItemData();
|
||||
void sort_data();
|
||||
void sort();
|
||||
void sortRole_data();
|
||||
|
|
@ -752,6 +753,27 @@ void tst_QStandardItemModel::data()
|
|||
|
||||
}
|
||||
|
||||
void tst_QStandardItemModel::clearItemData()
|
||||
{
|
||||
currentRoles.clear();
|
||||
QVERIFY(!m_model->clearItemData(QModelIndex()));
|
||||
QCOMPARE(currentRoles, {});
|
||||
const QModelIndex idx = m_model->index(0, 0);
|
||||
const QMap<int, QVariant> oldData = m_model->itemData(idx);
|
||||
m_model->setData(idx, QLatin1String("initialitem"), Qt::DisplayRole);
|
||||
m_model->setData(idx, QLatin1String("tooltip"), Qt::ToolTipRole);
|
||||
m_model->setData(idx, 5, Qt::UserRole);
|
||||
currentRoles.clear();
|
||||
QVERIFY(m_model->clearItemData(idx));
|
||||
QCOMPARE(idx.data(Qt::UserRole), QVariant());
|
||||
QCOMPARE(idx.data(Qt::ToolTipRole), QVariant());
|
||||
QCOMPARE(idx.data(Qt::DisplayRole), QVariant());
|
||||
QCOMPARE(idx.data(Qt::EditRole), QVariant());
|
||||
QCOMPARE(currentRoles, {});
|
||||
m_model->setItemData(idx, oldData);
|
||||
currentRoles.clear();
|
||||
}
|
||||
|
||||
void tst_QStandardItemModel::clear()
|
||||
{
|
||||
QStandardItemModel model;
|
||||
|
|
|
|||
Loading…
Reference in New Issue