diff options
author | Chris Adams <christopher.adams@nokia.com> | 2012-01-30 11:04:07 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-31 12:00:31 +0100 |
commit | cf52c268b036d808901313acc554587465bdd746 (patch) | |
tree | e0efea11753f61faed7fbb3f15ea86a4adb702e9 | |
parent | cf20c06756a461730f35acf7631cbf5e762fcfe1 (diff) | |
download | qtbase-cf52c268b036d808901313acc554587465bdd746.tar.gz |
Add public role names setter to QStandardItemModel
This allows QStandardItemModel to be usable in QML without requiring
clients to subclass it.
Task-number: QTBUG-15067
Change-Id: I867fcd8137132affdf21f37a9fa293e855a09a13
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
-rw-r--r-- | src/widgets/itemviews/qstandarditemmodel.cpp | 9 | ||||
-rw-r--r-- | src/widgets/itemviews/qstandarditemmodel.h | 2 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qstandarditemmodel/tst_qstandarditemmodel.cpp | 20 |
3 files changed, 31 insertions, 0 deletions
diff --git a/src/widgets/itemviews/qstandarditemmodel.cpp b/src/widgets/itemviews/qstandarditemmodel.cpp index 0fa82cbd42..112b533f4f 100644 --- a/src/widgets/itemviews/qstandarditemmodel.cpp +++ b/src/widgets/itemviews/qstandarditemmodel.cpp @@ -2080,6 +2080,15 @@ QStandardItemModel::~QStandardItemModel() } /*! + Sets the item role names to \a roleNames. +*/ +void QStandardItemModel::setItemRoleNames(const QHash<int,QByteArray> &roleNames) +{ + Q_D(QStandardItemModel); + d->roleNames = roleNames; +} + +/*! Removes all items (including header items) from the model and sets the number of rows and columns to zero. diff --git a/src/widgets/itemviews/qstandarditemmodel.h b/src/widgets/itemviews/qstandarditemmodel.h index 10a44ffe39..e374665f20 100644 --- a/src/widgets/itemviews/qstandarditemmodel.h +++ b/src/widgets/itemviews/qstandarditemmodel.h @@ -324,6 +324,8 @@ public: QStandardItemModel(int rows, int columns, QObject *parent = 0); ~QStandardItemModel(); + void setItemRoleNames(const QHash<int,QByteArray> &roleNames); + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; QModelIndex parent(const QModelIndex &child) const; diff --git a/tests/auto/widgets/itemviews/qstandarditemmodel/tst_qstandarditemmodel.cpp b/tests/auto/widgets/itemviews/qstandarditemmodel/tst_qstandarditemmodel.cpp index bcabedf89c..1c661f9060 100644 --- a/tests/auto/widgets/itemviews/qstandarditemmodel/tst_qstandarditemmodel.cpp +++ b/tests/auto/widgets/itemviews/qstandarditemmodel/tst_qstandarditemmodel.cpp @@ -134,6 +134,8 @@ private slots: void treeDragAndDrop(); void removeRowsAndColumns(); + void itemRoleNames(); + private: QAbstractItemModel *m_model; QPersistentModelIndex persistent; @@ -1658,6 +1660,24 @@ void tst_QStandardItemModel::removeRowsAndColumns() VERIFY_MODEL } +void tst_QStandardItemModel::itemRoleNames() +{ + QVector<QString> row_list = QString("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20").split(',').toVector(); + QVector<QString> col_list = row_list; + QStandardItemModel model; + for (int c = 0; c < col_list.count(); c++) + for (int r = 0; r < row_list.count(); r++) + model.setItem(r, c, new QStandardItem(row_list[r] + "x" + col_list[c])); + VERIFY_MODEL + + QHash<int, QByteArray> newRoleNames; + newRoleNames.insert(Qt::DisplayRole, "Name"); + newRoleNames.insert(Qt::DecorationRole, "Avatar"); + model.setItemRoleNames(newRoleNames); + QCOMPARE(model.roleNames(), newRoleNames); + VERIFY_MODEL +} + QTEST_MAIN(tst_QStandardItemModel) #include "tst_qstandarditemmodel.moc" |