QStandardItem: store Qt::UserRole-1 in a static constexpr var

Change-Id: I809135aed1883130891863e7c8650d0b3e7f3a35
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Ahmad Samir 2023-11-14 18:24:10 +02:00
parent 85eebedb16
commit 6e8563fb2d
2 changed files with 9 additions and 5 deletions

View File

@ -19,6 +19,11 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
// Used internally to store the flags
namespace {
constexpr auto DataFlagsRole = Qt::ItemDataRole(Qt::UserRole - 1);
}
static inline QString qStandardItemModelDataListMimeType() static inline QString qStandardItemModelDataListMimeType()
{ {
return QStringLiteral("application/x-qstandarditemmodeldatalist"); return QStringLiteral("application/x-qstandarditemmodeldatalist");
@ -279,8 +284,7 @@ QMap<int, QVariant> QStandardItemPrivate::itemData() const
{ {
QMap<int, QVariant> result; QMap<int, QVariant> result;
for (const auto &data : values) { for (const auto &data : values) {
// Qt::UserRole - 1 is used internally to store the flags if (data.role != DataFlagsRole)
if (data.role != Qt::UserRole - 1)
result.insert(data.role, data.value); result.insert(data.role, data.value);
} }
return result; return result;
@ -994,7 +998,7 @@ void QStandardItem::emitDataChanged()
*/ */
void QStandardItem::setFlags(Qt::ItemFlags flags) void QStandardItem::setFlags(Qt::ItemFlags flags)
{ {
setData((int)flags, Qt::UserRole - 1); setData((int)flags, DataFlagsRole);
} }
/*! /*!
@ -1009,7 +1013,7 @@ void QStandardItem::setFlags(Qt::ItemFlags flags)
*/ */
Qt::ItemFlags QStandardItem::flags() const Qt::ItemFlags QStandardItem::flags() const
{ {
QVariant v = data(Qt::UserRole - 1); QVariant v = data(DataFlagsRole);
if (!v.isValid()) if (!v.isValid())
return (Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable return (Qt::ItemIsSelectable|Qt::ItemIsEnabled|Qt::ItemIsEditable
|Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled); |Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);

View File

@ -728,7 +728,7 @@ void tst_QStandardItemModel::data()
const QMap<int, QVariant> itmData = m_model->itemData(m_model->index(0, 0)); const QMap<int, QVariant> itmData = m_model->itemData(m_model->index(0, 0));
QCOMPARE(itmData.value(Qt::DisplayRole), QLatin1String("initialitem")); QCOMPARE(itmData.value(Qt::DisplayRole), QLatin1String("initialitem"));
QCOMPARE(itmData.value(Qt::ToolTipRole), QLatin1String("tooltip")); QCOMPARE(itmData.value(Qt::ToolTipRole), QLatin1String("tooltip"));
QVERIFY(!itmData.contains(Qt::UserRole - 1)); QVERIFY(!itmData.contains(Qt::UserRole - 1)); // Qt::UserRole - 1 is used to store flags
QVERIFY(m_model->itemData(QModelIndex()).isEmpty()); QVERIFY(m_model->itemData(QModelIndex()).isEmpty());
} }