How to set data inside QAbstractTableModel

I need to implement a table with Qt.

I believe that I will file a lawsuit in QAbstractTableModel using QTableView using this model.

I understand that I will need to edit the rowCount (), columnCount () and data () functions of the model.

However, I do not understand how to precisely set the data inside the model so that the data () function can restore it ..

Is setData () provided for this purpose? I saw that as a parameter that I do not need, EditRole is required, since I do not want my table to be editable.

So, how can I “set” data inside the model or get data for the model using the data () function?

Also, what is the data () function called, that is, who calls it, and where will it need to be called?

Please help me with this.

Thanks.

+12
qt qtableview qabstracttablemodel
source share
2 answers

How the actual data is stored in memory, generated or retrieved from the data warehouse, is entirely up to you. If this is static data, you can use Qt's container classes or custom data structures.

You only need to override the setData() method for editable models.

There are 4 methods that you must implement in a non-editable subclass of QAbstractTableModel :

  • int rowCount()
  • int columnCount()
  • QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole )
  • QVariant data(const QModelIndex & index, int role = Qt::DisplayRole)

These methods are called from a view, usually an instance of QTableView . The first two methods should return the size of the table. For example, if rowCount() returns 10 and columnCount() returns 4 , the view will refer to the data() method 40 times (once for each cell), requesting the actual data in your internal model data structures.

As an example, suppose you have implemented a custom retrieveDataFromMarsCuriosity() slot in your model. This slot fills the data structure and connects to the QPushButton instance, so you get the latest data by clicking the button. Now you need to report this when the data is changed so that it can be updated correctly. So you need to emit beginRemoveRows() , endRemoveRows() , beginInsertRows() , endInsertRows() and its columns.

The Qt documentation contains everything you need to know about this.

+18
source share

You do not need to use setData(...) . Instead, you need to subclass QAbstractTableModel so that its rowCount() , columnCount() , data(index) and potentially headerData(section, horizontalOrVertical) return the data you want to display. Here is an example based on PyQt5:

 from PyQt5.QtWidgets import * from PyQt5.QtCore import * headers = ["Scientist name", "Birthdate", "Contribution"] rows = [("Newton", "1643-01-04", "Classical mechanics"), ("Einstein", "1879-03-14", "Relativity"), ("Darwin", "1809-02-12", "Evolution")] class TableModel(QAbstractTableModel): def rowCount(self, parent): # How many rows are there? return len(rows) def columnCount(self, parent): # How many columns? return len(headers) def data(self, index, role): if role != Qt.DisplayRole: return QVariant() # What the value of the cell at the given index? return rows[index.row()][index.column()] def headerData(self, section, orientation, role: if role != Qt.DisplayRole or orientation != Qt.Horizontal: return QVariant() # What the header for the given column? return headers[section] app = QApplication([]) model = TableModel() view = QTableView() view.setModel(model) view.show() app.exec_() 

It is taken from this GitHub repository and displays the following table:

QAbstractTableModel example

0
source share

All Articles