QStandardItem: add note about reimplementing data/setData() wrt. flags
Extend the unittests. Drive-by change: add missing include, otherwise static analyzers (clangd) complain. Fixes: QTBUG-105150 Pick-to: 6.5 5.15 Change-Id: I312133d5b35119e2e51002dfefe0e141c0708e3d Reviewed-by: David Faure <david.faure@kdab.com> (cherry picked from commit 85eebedb16da67d9f2caeb14ed45c2dae8f82837) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
72331b450d
commit
49bd0a8190
@ -868,9 +868,15 @@ QStandardItem *QStandardItem::parent() const
|
|||||||
Sets the item's data for the given \a role to the specified \a value.
|
Sets the item's data for the given \a role to the specified \a value.
|
||||||
|
|
||||||
If you subclass QStandardItem and reimplement this function, your
|
If you subclass QStandardItem and reimplement this function, your
|
||||||
reimplementation should call emitDataChanged() if you do not call
|
reimplementation should:
|
||||||
the base implementation of setData(). This will ensure that e.g.
|
\list
|
||||||
views using the model are notified of the changes.
|
\li call emitDataChanged() if you do not call the base implementation of
|
||||||
|
setData(). This will ensure that e.g. views using the model are notified
|
||||||
|
of the changes
|
||||||
|
\li call the base implementation for roles you don't handle, otherwise
|
||||||
|
setting flags, e.g. by calling setFlags(), setCheckable(), setEditable()
|
||||||
|
etc., will not work.
|
||||||
|
\endlist
|
||||||
|
|
||||||
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
|
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
|
||||||
as referring to the same data.
|
as referring to the same data.
|
||||||
@ -924,6 +930,11 @@ void QStandardItem::clearData()
|
|||||||
Returns the item's data for the given \a role, or an invalid
|
Returns the item's data for the given \a role, or an invalid
|
||||||
QVariant if there is no data for the role.
|
QVariant if there is no data for the role.
|
||||||
|
|
||||||
|
If you reimplement this function, your reimplementation should call
|
||||||
|
the base implementation for roles you don't handle, otherwise getting
|
||||||
|
flags, e.g. by calling flags(), isCheckable(), isEditable() etc.,
|
||||||
|
will not work.
|
||||||
|
|
||||||
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
|
\note The default implementation treats Qt::EditRole and Qt::DisplayRole
|
||||||
as referring to the same data.
|
as referring to the same data.
|
||||||
*/
|
*/
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
// We mean it.
|
// We mean it.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <QtGui/qstandarditemmodel.h>
|
||||||
|
|
||||||
#include <QtGui/private/qtguiglobal_p.h>
|
#include <QtGui/private/qtguiglobal_p.h>
|
||||||
#include "private/qabstractitemmodel_p.h"
|
#include "private/qabstractitemmodel_p.h"
|
||||||
|
|
||||||
|
@ -1006,6 +1006,32 @@ public:
|
|||||||
|
|
||||||
using QStandardItem::clone;
|
using QStandardItem::clone;
|
||||||
using QStandardItem::emitDataChanged;
|
using QStandardItem::emitDataChanged;
|
||||||
|
|
||||||
|
void setData(const QVariant &value, int role) override
|
||||||
|
{
|
||||||
|
switch (role) {
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
QStandardItem::setData(value, role);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// setFlags() uses "UserRole - 1" to store the flags, which is an
|
||||||
|
// implementation detail not exposed in the docs.
|
||||||
|
QStandardItem::setData(value, role);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant data(int role) const override
|
||||||
|
{
|
||||||
|
switch (role) {
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
return QStandardItem::data(role);
|
||||||
|
default:
|
||||||
|
// flags() uses "UserRole - 1" to get the flags, which is an implementation
|
||||||
|
// detail not exposed in the docs.
|
||||||
|
return QStandardItem::data(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QStandardItem*)
|
Q_DECLARE_METATYPE(QStandardItem*)
|
||||||
@ -1041,6 +1067,12 @@ void tst_QStandardItem::subclassing()
|
|||||||
QCOMPARE(item->child(0), child2);
|
QCOMPARE(item->child(0), child2);
|
||||||
QCOMPARE(item->child(1), child0);
|
QCOMPARE(item->child(1), child0);
|
||||||
QCOMPARE(item->child(2), child1);
|
QCOMPARE(item->child(2), child1);
|
||||||
|
|
||||||
|
item->setFlags(Qt::ItemFlags{0});
|
||||||
|
QCOMPARE(item->flags(), Qt::ItemFlags{0});
|
||||||
|
|
||||||
|
item->setFlags(Qt::ItemFlags{Qt::ItemIsEditable | Qt::ItemIsSelectable});
|
||||||
|
QCOMPARE(item->flags(), Qt::ItemFlags{Qt::ItemIsEditable | Qt::ItemIsSelectable});
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QStandardItem::lessThan()
|
void tst_QStandardItem::lessThan()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user