qt5qcomboboxqabstractitemmodelqmodelindex

Where and when QModelIndex created


Class QAbstractItemModel have a method QAbstractItemModel::createIndex() that creates QModelIndex.

When this object created for each QComboBox item for example?

In documentation we can find:

Note: Model indexes should be used immediately and then discarded. You should not rely on indexes to remain valid after calling model functions that change the structure of the model or delete items. If you need to keep a model index over time use a QPersistentModelIndex.

So it seems temp object, but who create it? Is it model, but i did not find any sign that model itself create index in source code, or it View (Widget) creates index before it reads data from model?

How QComboBox store values and indexes that readed from Model, or it reads it from every time it need to update data?

How QComboBox actually read data, is it just loop through from 0 to rowCount() and creates new index for each value then use data( const QModelIndex &index, int role ) to read?


Solution

  • Ok, after i wrote my own widget with model i have an answer - QModelIndex created by widget itself.

    As example you can use my project on github:

    https://github.com/vasiliyaltunin/articles.blog.altuninvv.ru/tree/master/qt5/Models/LampModel

    When status of lamp updated

    https://github.com/vasiliyaltunin/articles.blog.altuninvv.ru/blob/1068fdd88466b0e2dcf818af1e305e7d41896b19/qt5/Models/LampModel/qlampwidget.cpp#L344

    I use model method data() to get status from model

    this->setStatus(model->data(QModelIndex(),Qt::DisplayRole).toInt());

    Because i have only one "cell" in model, i pass empty QModelIndex() to model.

    So it widget itself create indexes based on row and column count returned by model.