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. Help and explanation with QAbstractTableModel
Forum Updated to NodeBB v4.3 + New Features

Help and explanation with QAbstractTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 705 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.
  • P Offline
    P Offline
    ppitu
    wrote on last edited by
    #1

    Hello

    I use the class (PersonModel) that inherits QAbstractTableModel. I have one row in database which contains 4 columns. When i fill QTableView with data from database i call the data() function several times with the following parameters:

    Col: 0 Role: 6
    Col: 0 Role: 7
    Col: 0 Role: 9
    Col: 0 Role: 10
    Col: 0 Role: 1
    Col: 0 Role: 0
    Col: 0 Role: 8
    Col: 1 Role: 6
    Col: 1 Role: 7
    Col: 1 Role: 9
    Col: 1 Role: 10
    Col: 1 Role: 1
    Col: 1 Role: 0
    Col: 1 Role: 8
    Col: 2 Role: 6
    Col: 2 Role: 7
    Col: 2 Role: 9
    Col: 2 Role: 10
    Col: 2 Role: 1
    Col: 2 Role: 0
    Col: 2 Role: 8
    Col: 3 Role: 6
    Col: 3 Role: 7
    Col: 3 Role: 9
    Col: 3 Role: 10
    Col: 3 Role: 1
    Col: 3 Role: 0
    Col: 3 Role: 8
    Col: 0 Role: 6
    Col: 0 Role: 7
    Col: 0 Role: 9
    Col: 0 Role: 10
    Col: 0 Role: 1
    Col: 0 Role: 0
    Col: 0 Role: 8
    Col: 1 Role: 6
    Col: 1 Role: 7
    Col: 1 Role: 9
    Col: 1 Role: 10
    Col: 1 Role: 1
    Col: 1 Role: 0
    Col: 1 Role: 8
    Col: 2 Role: 6
    Col: 2 Role: 7
    Col: 2 Role: 9
    Col: 2 Role: 10
    Col: 2 Role: 1
    Col: 2 Role: 0
    Col: 2 Role: 8
    Col: 3 Role: 6
    Col: 3 Role: 7
    Col: 3 Role: 9
    Col: 3 Role: 10
    Col: 3 Role: 1
    Col: 3 Role: 0
    Col: 3 Role: 8

    Could someone explain me why it calls so many time and if i could improve it somehow?

    And this is my data() function, and is it well written?:

    QVariant PersonModel::data(const QModelIndex &index, int role) const
    {
        if(!isIndexValid(index)){
            return QVariant();
        }
    
        const Person& person = *mPerson->at(index.row());
    
        qDebug() << "Col: " << index.column() << "Role: " << role;
    
        switch (role) {
        case Qt::DisplayRole:
            if(index.column() == 0)
                return person.getId();
            else if(index.column() == 1)
                return person.getFirstName();
            else if(index.column() == 2)
                return person.getLastName();
            else
                return person.getPesel();
    
        default:
            return QVariant();
        }
    
    
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @ppitu said in Help and explanation with QAbstractTableModel:

      Could someone explain me why it calls so many time

      Because every cell must be painted

      if i could improve it somehow?

      No

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • P Offline
        P Offline
        ppitu
        wrote on last edited by ppitu
        #3

        But i have only for cell in on row.
        qt_cells.png
        Or does it not matter?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          As your debug output shows you only the 4 cells are asked.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P 1 Reply Last reply
          2
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            Since you only provide DisplayRole in your data()
            you could add

            if(role != Qt::DisplayRole)
                return QVariant();
            

            at the start of this function.

            Christian EhrlicherC 1 Reply Last reply
            0
            • B Bonnie

              Since you only provide DisplayRole in your data()
              you could add

              if(role != Qt::DisplayRole)
                  return QVariant();
              

              at the start of this function.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Bonnie data() is pure virtual, you have to return QVariant()

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              B 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                As your debug output shows you only the 4 cells are asked.

                P Offline
                P Offline
                ppitu
                wrote on last edited by
                #7

                @Christian-Ehrlicher okay, thanks for the answer

                JonBJ 1 Reply Last reply
                0
                • P ppitu

                  @Christian-Ehrlicher okay, thanks for the answer

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @ppitu
                  Don't kid yourself that this change will make that much of difference, especially compiled optimized, other than suppressing your debug messages. What you have to accept is that the Qt code is going to call yourdata() on each cell with a fair number of different roles, and that's just how it is!

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #9

                    Hi,

                    There's work in progress to improve the internals of that part and make it more efficient which are going to show up for Qt 6.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Bonnie data() is pure virtual, you have to return QVariant()

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher
                      Oops, didn't notice that, I've changed my code above.
                      Anyway my point is that there's no need to get a Person for every data() call.

                      1 Reply Last reply
                      0

                      • Login

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