Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How does QModelIndex know what is my data structure??
Forum Updated to NodeBB v4.3 + New Features

How does QModelIndex know what is my data structure??

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skipper
    wrote on last edited by Skipper
    #1

    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?

    raven-worxR Gojir4G 2 Replies Last reply
    0
    • S Skipper

      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?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      6
      • S Skipper

        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?

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by Gojir4
        #3

        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 :)

        1 Reply Last reply
        3
        • S Offline
          S Offline
          Skipper
          wrote on last edited by
          #4

          @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());
          
          raven-worxR 1 Reply Last reply
          0
          • S Skipper

            @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());
            
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @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.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            3
            • S Offline
              S Offline
              Skipper
              wrote on last edited by Skipper
              #6

              @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.

              raven-worxR 1 Reply Last reply
              0
              • S Skipper

                @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.

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

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

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    Skipper
                    wrote on last edited by
                    #9

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

                    aha_1980A 1 Reply Last reply
                    1
                    • S Skipper

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

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved