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>bb10
parent
cf20c06756
commit
cf52c268b0
|
|
@ -2079,6 +2079,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.
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Reference in New Issue