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 to add C++ model to Tableview
QtWS25 Last Chance

how to add C++ model to Tableview

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 882 Views
  • 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.
  • M Offline
    M Offline
    milan
    wrote on last edited by
    #1

    I am following a book on Qt. It has following example.

    // mulmodel.cpp
    #include "mulmodel.h"
    
    MulModel::MulModel(int rows, int columns, QObject *parent)
        : QAbstractTableModel(parent)
    {
        m_rows = rows;
        m_columns = columns;
    }
    
    Qt::ItemFlags MulModel::flags(const QModelIndex &index) const
    {
        if(!index.isValid())
            return Qt::ItemIsEnabled;
    
        return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    }
    
    QVariant MulModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if( role != Qt::DisplayRole )
            return QVariant();
    
        return section+1;
    }
    
    int MulModel::rowCount(const QModelIndex &parent) const
    {
        if (parent.isValid())
            return 0;
        return m_rows;
    }
    
    int MulModel::columnCount(const QModelIndex &parent) const
    {
        if (parent.isValid())
            return 0;
        return m_columns;
    }
    
    QVariant MulModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        switch(role) {
            case Qt::DisplayRole:
                return (index.row()+1) * (index.column()+1);
            case Qt::ToolTipRole:
                return QString("%1 x %2").arg(index.row()+1).arg(index.column()+1);
            default:
                return QVariant();
        }
    }
    
    #ifndef MULMODEL_H
    #define MULMODEL_H
    
    #include <QAbstractTableModel>
    
    class MulModel : public QAbstractTableModel
    {
        Q_OBJECT
    
    public:
        explicit MulModel(int rows, int columns, QObject *parent = nullptr);
    
        Qt::ItemFlags flags( const QModelIndex &index ) const;
    
        // Header:
        QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    
        // Basic functionality:
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    
    private:
        int m_rows, m_columns;
    };
    
    #endif // MULMODEL_H
    

    Main cpp as

    //Main.cpp
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine* engine = new QQmlApplicationEngine();
    MulModel mulmodel(12, 12);
    engine->rootContext()->setContextProperty("MulModel", &mulmodel);
    engine->load(QUrl(QStringLiteral("qrc:/ui/main.qml")));
    
    TableView {
            width: 600
            alternatingRowColors: true
            model: MulModel
        }
    

    I have no idea, why the code is not working. Can somebody help on this.

    Gojir4G 1 Reply Last reply
    0
    • M milan

      I am following a book on Qt. It has following example.

      // mulmodel.cpp
      #include "mulmodel.h"
      
      MulModel::MulModel(int rows, int columns, QObject *parent)
          : QAbstractTableModel(parent)
      {
          m_rows = rows;
          m_columns = columns;
      }
      
      Qt::ItemFlags MulModel::flags(const QModelIndex &index) const
      {
          if(!index.isValid())
              return Qt::ItemIsEnabled;
      
          return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
      }
      
      QVariant MulModel::headerData(int section, Qt::Orientation orientation, int role) const
      {
          if( role != Qt::DisplayRole )
              return QVariant();
      
          return section+1;
      }
      
      int MulModel::rowCount(const QModelIndex &parent) const
      {
          if (parent.isValid())
              return 0;
          return m_rows;
      }
      
      int MulModel::columnCount(const QModelIndex &parent) const
      {
          if (parent.isValid())
              return 0;
          return m_columns;
      }
      
      QVariant MulModel::data(const QModelIndex &index, int role) const
      {
          if (!index.isValid())
              return QVariant();
      
          switch(role) {
              case Qt::DisplayRole:
                  return (index.row()+1) * (index.column()+1);
              case Qt::ToolTipRole:
                  return QString("%1 x %2").arg(index.row()+1).arg(index.column()+1);
              default:
                  return QVariant();
          }
      }
      
      #ifndef MULMODEL_H
      #define MULMODEL_H
      
      #include <QAbstractTableModel>
      
      class MulModel : public QAbstractTableModel
      {
          Q_OBJECT
      
      public:
          explicit MulModel(int rows, int columns, QObject *parent = nullptr);
      
          Qt::ItemFlags flags( const QModelIndex &index ) const;
      
          // Header:
          QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
      
          // Basic functionality:
          int rowCount(const QModelIndex &parent = QModelIndex()) const override;
          int columnCount(const QModelIndex &parent = QModelIndex()) const override;
      
      private:
          int m_rows, m_columns;
      };
      
      #endif // MULMODEL_H
      

      Main cpp as

      //Main.cpp
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      QGuiApplication app(argc, argv);
      QQmlApplicationEngine* engine = new QQmlApplicationEngine();
      MulModel mulmodel(12, 12);
      engine->rootContext()->setContextProperty("MulModel", &mulmodel);
      engine->load(QUrl(QStringLiteral("qrc:/ui/main.qml")));
      
      TableView {
              width: 600
              alternatingRowColors: true
              model: MulModel
          }
      

      I have no idea, why the code is not working. Can somebody help on this.

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

      @milan What do you mean by the code is not working ? Does the application start ? Do you see a "blank" window ?

      I see two mistake in the code:

      • In main.cpp, return app.exec(); is missing
      • There is no height defined for TableView, I assume this is the root element of your main.qml file.
      1 Reply Last reply
      1
      • M Offline
        M Offline
        milan
        wrote on last edited by milan
        #3

        @Gojir4 .The main cpp does have app.exec(). I just posted code snippet for main.cpp. The tableview, even if height is defined, it does not work. And yes, the window does open, only Table is populated with mulmodel data. The table can also be seen, it is blank though.

        Gojir4G 2 Replies Last reply
        0
        • M milan

          @Gojir4 .The main cpp does have app.exec(). I just posted code snippet for main.cpp. The tableview, even if height is defined, it does not work. And yes, the window does open, only Table is populated with mulmodel data. The table can also be seen, it is blank though.

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

          @milan I guess you have to add some TableViewColumn in your TableView, as suggested from the doc

          TableView {
              TableViewColumn {
                  role: "display"
                  title: "Display"
                  width: 100
              }
              TableViewColumn {
                  role: "tooltip"
                  title: "ToolTip"
                  width: 200
              }
              model: libraryModel
          }
          

          You will also probably have to implement QAbstractItemModel::roleNames() method to provide role names for QML (I don't thinkif you can use display and tooltip directly from QML)

          1 Reply Last reply
          0
          • M milan

            @Gojir4 .The main cpp does have app.exec(). I just posted code snippet for main.cpp. The tableview, even if height is defined, it does not work. And yes, the window does open, only Table is populated with mulmodel data. The table can also be seen, it is blank though.

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

            @milan Don't you have the corrected version of your code as it is coming from a book ??

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TobbY
              wrote on last edited by
              #6

              Hi,
              Please refer this page for your reference. this will surely help your problem. You need to have your own custom role and modification in your qml file.
              https://qiita.com/illness072/items/a6f2ce9f7a1bfff44049

              M 1 Reply Last reply
              1
              • T TobbY

                Hi,
                Please refer this page for your reference. this will surely help your problem. You need to have your own custom role and modification in your qml file.
                https://qiita.com/illness072/items/a6f2ce9f7a1bfff44049

                M Offline
                M Offline
                milan
                wrote on last edited by
                #7

                @TobbY . Thank you very much this solves my question. I have a different question (maybe I should open new topic). How can I add tooltip role to table row's title in tooltip, maybe table cells' value in its tooltip.

                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