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 can I create a model from a vector of objects so I can use it with a QTableView? it must inherit from what class and must implement what methods?
QtWS25 Last Chance

How can I create a model from a vector of objects so I can use it with a QTableView? it must inherit from what class and must implement what methods?

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

    How can I create a model from a vector of objects so I can use it with a QTableView?

    it must inherit from what class and must implement what methods?

    JonBJ 1 Reply Last reply
    0
    • J jdent

      How can I create a model from a vector of objects so I can use it with a QTableView?

      it must inherit from what class and must implement what methods?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @jdent said in How can I create a model from a vector of objects so I can use it with a QTableView? it must inherit from what class and must implement what methods?:

      it must inherit from what class and must implement what methods?

      Since it's void QTableView::setModel(QAbstractItemModel *model), see https://doc.qt.io/qt-6/qabstractitemmodel.html#subclassing for a QAbstractItemModel, which you could use with a vector data store. See https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing if you want a table. They tell you which methods you must write.

      J 1 Reply Last reply
      1
      • JonBJ JonB

        @jdent said in How can I create a model from a vector of objects so I can use it with a QTableView? it must inherit from what class and must implement what methods?:

        it must inherit from what class and must implement what methods?

        Since it's void QTableView::setModel(QAbstractItemModel *model), see https://doc.qt.io/qt-6/qabstractitemmodel.html#subclassing for a QAbstractItemModel, which you could use with a vector data store. See https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing if you want a table. They tell you which methods you must write.

        J Offline
        J Offline
        jdent
        wrote on last edited by
        #3

        @JonB Is this a valid start? Please help me I am lost!

        class VectorModel  : public QAbstractTableModel
        {
        	Q_OBJECT
        
        
        		std::vector<Claim> rows;
        public:
        	VectorModel(QObject *parent);
        	~VectorModel();
        
        	int rowCount(const QModelIndex& parent) const override
        	{
        		if(parent.isValid())
        			return 0;
        		return rows.size();
        	}
        
        	int columnCount(const QModelIndex& parent) const override
        	{
        		return 14;
        	}
        
        	QVariant data(const QModelIndex& index, int role) const override
        	{
        		Claim claim = rows[index.row()];
        		return claim.id;
        	}
        };
        
        JonBJ 1 Reply Last reply
        0
        • J jdent

          @JonB Is this a valid start? Please help me I am lost!

          class VectorModel  : public QAbstractTableModel
          {
          	Q_OBJECT
          
          
          		std::vector<Claim> rows;
          public:
          	VectorModel(QObject *parent);
          	~VectorModel();
          
          	int rowCount(const QModelIndex& parent) const override
          	{
          		if(parent.isValid())
          			return 0;
          		return rows.size();
          	}
          
          	int columnCount(const QModelIndex& parent) const override
          	{
          		return 14;
          	}
          
          	QVariant data(const QModelIndex& index, int role) const override
          	{
          		Claim claim = rows[index.row()];
          		return claim.id;
          	}
          };
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @jdent
          Yes, good start. Will produce a read-only model. The reference states all the methods you need to override.

          In data() you must test role and only return yours on Display/EditRole. Else an invalid variant, QVariant() or call base methodQAbstractTableModel::data(index, role). And if you are going to have 14 columns you need to provide that data and take index.column() into account in data().

          If you really only want a model of a vector of something then columns needs to return 1. QAbstractListModel Class would then save you implementing most stuff.

          There is some inconsistency: if you want a QTableView not a QListView then presumably your model actual contains more data (columns) than just a vector. Which do you want?

          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