QAbstractTableModel: index() vs. createIndex()
Asked Answered
A

2

5

When subclassing QAbstractTableModel, what's the proper way to get a QModelIndex for a given row and column (for example, to provide a convenience method data(int row, int column))?

There's index() and createIndex() - they both seem to do that?

Example:

TableModel::data(int row, int column, int role) const
{
    return data(index(row, column), role);
}
Albuquerque answered 17/11, 2012 at 22:56 Comment(0)
S
6

What you want is index(), createIndex() is what subclasses use to generate indexes. However, QModelIndex has a data() member. So, model->index(row, column).data(role) may be what you want. No need for a helper function.

Shorthorn answered 18/11, 2012 at 3:34 Comment(0)
R
3

I would say index() is the idiomatic way here, your code is then correct.

Generally, createIndex() is used in subclass of QAbstractItemModel, when you need to override index() and parent() methods. Since QModelIndex constructor is private, you have to use createIndex() to create a new index.

Furthermore, a QModelIndex can store some internal data (QModelIndex::internalPointer). createIndex() is also used to pass an internal pointer to the created QModelIndex.

Roselba answered 18/11, 2012 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.