Make the roleNames a virtual accessor.

This is consistent with the rest of the API of QAbstractItemModel
(which is virtual) and removes the need for code like this
in the constructor (where it doesn't belong):

QHash<int, QByteArray> myRoleNames = roleNames();
myRoleNames.insert(Qt::UserRole + 1, "myCustomRole");
setRoleNames(myRoleNames);

in favor of

MyModel::roleNames() const {
  QHash<int, QByteArray> myRoleNames = QAbstractItemModel::roleNames();
  myRoleNames.insert(Qt::UserRole + 1, "myCustomRole");
  return myRoleNames;
}

which is consistent with all other QAIM API (eg, flags()).

This is a source compatible change.

Change-Id: I7e1ce17f8dab2292c4c7b6dbd3c09ec71b5c793b
Reviewed-by: David Faure <faure@kde.org>
Reviewed-by: Marius Bugge Monsen <marius@cutehacks.com>
This commit is contained in:
Stephen Kelly 2011-12-27 17:02:33 +01:00 committed by Qt by Nokia
parent 1e10e4deb9
commit c3ad8c1c06
3 changed files with 44 additions and 4 deletions

View File

@ -2132,6 +2132,7 @@ QSize QAbstractItemModel::span(const QModelIndex &) const
/*! /*!
\since 4.6 \since 4.6
\obsolete
Sets the model's role names to \a roleNames. Sets the model's role names to \a roleNames.
@ -2142,7 +2143,11 @@ QSize QAbstractItemModel::span(const QModelIndex &) const
\sa roleNames() \sa roleNames()
*/ */
void QAbstractItemModel::setRoleNames(const QHash<int,QByteArray> &roleNames)
/*!
\internal
*/
void QAbstractItemModel::doSetRoleNames(const QHash<int,QByteArray> &roleNames)
{ {
Q_D(QAbstractItemModel); Q_D(QAbstractItemModel);
d->roleNames = roleNames; d->roleNames = roleNames;
@ -2155,7 +2160,7 @@ void QAbstractItemModel::setRoleNames(const QHash<int,QByteArray> &roleNames)
\sa setRoleNames() \sa setRoleNames()
*/ */
const QHash<int,QByteArray> &QAbstractItemModel::roleNames() const QHash<int,QByteArray> QAbstractItemModel::roleNames() const
{ {
Q_D(const QAbstractItemModel); Q_D(const QAbstractItemModel);
return d->roleNames; return d->roleNames;

View File

@ -222,7 +222,7 @@ public:
Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const; Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const;
virtual QSize span(const QModelIndex &index) const; virtual QSize span(const QModelIndex &index) const;
const QHash<int,QByteArray> &roleNames() const; virtual QHash<int,QByteArray> roleNames() const;
#ifdef Q_NO_USING_KEYWORD #ifdef Q_NO_USING_KEYWORD
inline QObject *parent() const { return QObject::parent(); } inline QObject *parent() const { return QObject::parent(); }
@ -302,9 +302,16 @@ protected:
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to); void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to);
QModelIndexList persistentIndexList() const; QModelIndexList persistentIndexList() const;
void setRoleNames(const QHash<int,QByteArray> &roleNames); #if QT_DEPRECATED_SINCE(5,0)
QT_DEPRECATED void setRoleNames(const QHash<int,QByteArray> &roleNames)
{
doSetRoleNames(roleNames);
}
#endif
private: private:
void doSetRoleNames(const QHash<int,QByteArray> &roleNames);
Q_DECLARE_PRIVATE(QAbstractItemModel) Q_DECLARE_PRIVATE(QAbstractItemModel)
Q_DISABLE_COPY(QAbstractItemModel) Q_DISABLE_COPY(QAbstractItemModel)
}; };

View File

@ -112,6 +112,8 @@ private slots:
void testChildrenLayoutsChanged(); void testChildrenLayoutsChanged();
void testRoleNames();
private: private:
DynamicTreeModel *m_model; DynamicTreeModel *m_model;
}; };
@ -1976,5 +1978,31 @@ void tst_QAbstractItemModel::testChildrenLayoutsChanged()
} }
} }
class OverrideRoleNames : public QStringListModel
{
Q_OBJECT
public:
OverrideRoleNames(QObject *parent = 0)
: QStringListModel(parent)
{
}
QHash<int, QByteArray> roleNames() const
{
QHash<int, QByteArray> roles = QStringListModel::roleNames();
roles.insert(Qt::UserRole + 2, "custom");
return roles;
}
};
void tst_QAbstractItemModel::testRoleNames()
{
QAbstractItemModel *model = new OverrideRoleNames(this);
QHash<int, QByteArray> roles = model->roleNames();
QVERIFY(roles.contains(Qt::UserRole + 2));
QVERIFY(roles.value(Qt::UserRole + 2) == "custom");
}
QTEST_MAIN(tst_QAbstractItemModel) QTEST_MAIN(tst_QAbstractItemModel)
#include "tst_qabstractitemmodel.moc" #include "tst_qabstractitemmodel.moc"