Search code examples
qt4model-view

What is the difference between QModelIndex.model().data and QModelIndex.data?


Going through the documentation on QModelIndex, I noticed that it has a data() method to get the data from the model. All the tutorials I've seen use index.model().data(index, role) to get the data at the index. I thought that was strange, since index.data() seems to be much easier to use.

I searched around a bit, but I couldn't find any code using index.data() or anything comparing the two methods.

Are these these two methods functionally equivalent, or is there a reason beyond personal preference to use one over the other?


Solution

  • They are equivalent, QModelIndex::data(role) being a shortcut for QAbstractItemModel::data(index, role). It is more convenient if you already have an index object, but the implementation of data() is located in the model.