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. QAbstractTableModel columns always zero
Forum Updated to NodeBB v4.3 + New Features

QAbstractTableModel columns always zero

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 2.8k 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.
  • A Offline
    A Offline
    antonio
    wrote on last edited by
    #1

    Hi,

    I'm Using aQAbstractTableModel, my row is a
    @QList< QPair<QString, QString> > listOfPairs;@

    And I set my listOfPairs at:
    @void MyTableModel::setCurrencyMap(const QList< QPair<QString, QString> >&map)
    {
    listOfPairs = map;
    reset();
    }@

    The problem is that at data() method my index.column() always is zero.

    Thanks

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

      Hi,

      Are you sure you reimplemented columnCount correctly ?

      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
      • A Offline
        A Offline
        antonio
        wrote on last edited by
        #3

        Hi SGaist

        This is my columnCount

        @int MyTableModel::columnCount(const QModelIndex &/parent/) const
        {
        return 2;
        }@

        Thanks for your quick response

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          Hi, which view are you using?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antonio
            wrote on last edited by
            #5

            Hi 2,

            I'm exporting my model to QML, I have proved with both GridView and ListView, but both have index.colum() = 0.

            Thanks

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

              Did you try first to validate your model functionality with a QTableView ?

              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
              • A Offline
                A Offline
                antonio
                wrote on last edited by
                #7

                Not Sgaist,

                this model is for use it at QML, I've used with GridView and ListView both with the same result. index.column() = 0

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

                  I understood that, I just proposed that you first ensure that your model works properly from a C++ point of view before going with QML, that would allow you to eliminate some errors

                  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
                  • A Offline
                    A Offline
                    antonio
                    wrote on last edited by
                    #9

                    I'm sorry SGaist...

                    I've wasted a lot of time in that part so I've 'resolved' it asking for roles instead of columns like this:
                    @
                    QVariant MyModel::data(const QModelIndex &index, int role) const
                    {
                    if(!index.isValid())
                    return QVariant();

                    if(index.row() >= listOfPairs.size() || index.row() < 0)
                    return QVariant();

                    if(role == Qt::DisplayRole || role == MyRole1 || role == MyRole2) {
                    QPair<QString, QString> pair = listOfPairs.at(index.row());

                    if(role == MyRole1)
                      return pair.first;
                    else if(role == MyRole2){
                      return pair.second;
                    }
                    

                    }

                    return QVariant();
                    }@

                    thanks

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Look for modeltest. It is a class that you can instantiate with your model, and it will tell you all the things that are wrong with it. Your implementation of columnCount is wrong, for instance.

                      Also note that QML doesn't really deal with columns at all. QML works with lists, not grids and not trees.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        antonio
                        wrote on last edited by
                        #11

                        Thanks Andre, That sounds very useful.

                        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