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. Doubts with QtableModel
Qt 6.11 is out! See what's new in the release blog

Doubts with QtableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 643 Views 2 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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by
    #1

    Hello everybody,

    I am doing this tutorial http://doc.qt.io/archives/qt-4.8/modelview.html#
    And I come up some questions regarding to the following code:

    #include <QAbstractTableModel>
    
    class MyModel : public QAbstractTableModel
    {
        Q_OBJECT
    public:
        MyModel(QObject *parent);
        int rowCount(const QModelIndex &parent = QModelIndex()) const ;
        int columnCount(const QModelIndex &parent = QModelIndex()) const;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    };
    

    When we create the constructor MyModel(QObject *parent);
    why do we pass to MyModel pointer to QObject named parent, is parent any reserved class referring to parent class from QObject?

    Second,

    QVariant MyModel::data(const QModelIndex &index, int role) const
    {
        if (role == Qt::DisplayRole)
        {
           return QString("Row%1, Column%2")
                       .arg(index.row() + 1)
                       .arg(index.column() +1);
        }
        return QVariant();
    }
    

    Are we returning twice 2 different objects? first we return QStringobject and then QVariant. How is possible that a Method returning 2 different objects?

    Thank you very much!

    JKSHJ JonBJ 2 Replies Last reply
    0
    • J jss193

      Hello everybody,

      I am doing this tutorial http://doc.qt.io/archives/qt-4.8/modelview.html#
      And I come up some questions regarding to the following code:

      #include <QAbstractTableModel>
      
      class MyModel : public QAbstractTableModel
      {
          Q_OBJECT
      public:
          MyModel(QObject *parent);
          int rowCount(const QModelIndex &parent = QModelIndex()) const ;
          int columnCount(const QModelIndex &parent = QModelIndex()) const;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
      };
      

      When we create the constructor MyModel(QObject *parent);
      why do we pass to MyModel pointer to QObject named parent, is parent any reserved class referring to parent class from QObject?

      Second,

      QVariant MyModel::data(const QModelIndex &index, int role) const
      {
          if (role == Qt::DisplayRole)
          {
             return QString("Row%1, Column%2")
                         .arg(index.row() + 1)
                         .arg(index.column() +1);
          }
          return QVariant();
      }
      

      Are we returning twice 2 different objects? first we return QStringobject and then QVariant. How is possible that a Method returning 2 different objects?

      Thank you very much!

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @jss193 said in Doubts with QtableModel:

      When we create the constructor MyModel(QObject *parent);
      why do we pass to MyModel pointer to QObject named parent

      All QObjects can form parent-child relationships. A parent object manages the memory of its children.

      Read more:

      • http://doc.qt.io/qt-5/objecttrees.html
      • http://doc.qt.io/qt-5/qobject.html#details
      • http://doc.qt.io/qt-5/qobject.html#QObject
      • http://doc.qt.io/qt-5/qobject.html#children
      QVariant MyModel::data(const QModelIndex &index, int role) const
      {
          if (role == Qt::DisplayRole)
          {
             return QString("Row%1, Column%2")
                         .arg(index.row() + 1)
                         .arg(index.column() +1);
          }
          return QVariant();
      }
      

      Are we returning twice 2 different objects? first we return QStringobject and then QVariant. How is possible that a Method returning 2 different objects?

      Notice the if() statement. It only lets you return one object:

      • If the role is Qt::DisplayRole, the function returns a QString.
      • If the role is not Qt::DisplayRole, the function returns an empty QVariant.

      I am doing this tutorial http://doc.qt.io/archives/qt-4.8/modelview.html#

      Are you using Qt 4? If not, follow the Qt 5 documentation instead:

      http://doc.qt.io/qt-5/modelview.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

        @JKSH said in Doubts with QtableModel:

        If the role is Qt::DisplayRole, the function returns a QString.

        Which is implicitly converted into a QVariant by the compiler :)

        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
        5
        • J jss193

          Hello everybody,

          I am doing this tutorial http://doc.qt.io/archives/qt-4.8/modelview.html#
          And I come up some questions regarding to the following code:

          #include <QAbstractTableModel>
          
          class MyModel : public QAbstractTableModel
          {
              Q_OBJECT
          public:
              MyModel(QObject *parent);
              int rowCount(const QModelIndex &parent = QModelIndex()) const ;
              int columnCount(const QModelIndex &parent = QModelIndex()) const;
              QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
          };
          

          When we create the constructor MyModel(QObject *parent);
          why do we pass to MyModel pointer to QObject named parent, is parent any reserved class referring to parent class from QObject?

          Second,

          QVariant MyModel::data(const QModelIndex &index, int role) const
          {
              if (role == Qt::DisplayRole)
              {
                 return QString("Row%1, Column%2")
                             .arg(index.row() + 1)
                             .arg(index.column() +1);
              }
              return QVariant();
          }
          

          Are we returning twice 2 different objects? first we return QStringobject and then QVariant. How is possible that a Method returning 2 different objects?

          Thank you very much!

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

          @jss193
          @Christian-Ehrlicher's conversion happens without you doing anything because there is an implicit conversion defined from QString to QVariant. Really you are returning a QVariant::QVariant(QString(...)), http://doc.qt.io/qt-5/qvariant.html#QVariant-16, so you are not returning "two different types".

          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