Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How does QModelIndex know what is my data structure??



  • I am a QT beginner.When I met the QT Model/View for first time,I have some confusion about it.
    Just think about the code below(.h file):

    class GALLERYCORESHARED_EXPORT AlbumModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
        enum {
            IdRole = Qt::UserRole + 1,
            NameRole
        };
    ...
    private:
        DataBaseManager& mDb;
        std::unique_ptr<std::vector<std::unique_ptr<Album>>> mAlbums;
    

    for me,the data structure is the unique_ptr mAlbums,but now,How does QT Model know what or which is my data strcuture?
    I've never use function such like setDataStructureToModel(dsPointer),but they did connect.What did qt do?
    And,how do the QModelIndex and my pointer connect?
    I want to know what's going on here?


  • Moderators

    @Skipper said in How does QModelIndex know what is my data structure??:

    And,how do the QModelIndex and my pointer connect?

    you need to implement the data() method in your model accordingly.
    Qt requests data via the data item roles and you need to return the corresponding QVariant data in your data() method for them.

    Maybe you are looking for QSqlTableModel



  • Hi @Skipper,

    It doesn't know. You have to implemented at least QVariant QAbstractItemModel::data(const QModelIndex &index, int role = Qt::DisplayRole) const and int QAbstractItemModel::rowCount(const QModelIndex &parent = QModelIndex()) const

    edit: I was too slow :)



  • @raven-worx @Gojir4 Thinks a lot.
    But I still have a question.How does the index relationship between QModelIndex and data structure's index be established? i.e why index.row() equals a specifitc i in mAlbums[i]?
    code:

    QVariant AlbumModel::data(const QModelIndex& index,int role) const {
        if(!isIndexValid(index)) {
            return QVariant();
        }
        const Album& album = *mAlbums->at((int) index.row());
        switch(role) {
            case IdRole:
                return album.id();;
            case NameRole:
            case Qt::DisplayRole:
                return album.name();
            default:
                return QVariant();
        }
    }
    

    I can't understand this line:

        const Album& album = *mAlbums->at((int) index.row());
    

  • Moderators

    @Skipper
    when you reimplement your model's rowCount() method like that:

    int AlbumModel::rowCount(const QModelIndex & parent = QModelIndex()) const
    {
          return mAlbums->count();
    }
    int AlbumModel::columnCount() const
    {
          return ???; // number of properties you want to show of the item
    }
    

    Then Qt (the item view) creates a QModelIndex for each cell (row/column) and requests the data with it.

    If your list items represents the rows and the columns the items's data, you can use the index' row for the list item and the column index for the corresponding data of the item.



  • @raven-worx I think I got it.So can you remcommend me some articals ,books or documents that can give me a deeper understanding or more details on this question? Because I really want to know the inner mechanism about it.


  • Moderators

    @Skipper
    there is no better document than the source code ;)

    No i am not aware of an appropriate document, other than the docs and the item-view examples and some good ol' trial and error.


  • Qt Champions 2017

    There is one simple example of this ModelView at my GIT. You can check & modify & experiment to understand.



  • Yes,I've got it finally.The QT Docs is awesome!


  • Lifetime Qt Champion

    @Skipper So please close this topic as SOLVED. Thanks!


Log in to reply