Where did you get that idea? Are you maybe misinterpreting the C++ syntax in the documentation of QAbstractItemModel::columnCount()?The docs for rowCount() and columnCount() say they should return 0 if it is a table.
This means that columnCount() is a pure virtual method that has no implementation in this base class ("= 0") and must be overridden and implemented in a derived class.virtual int columnCount(const QModelIndex &parent = QModelIndex()) const = 0;
A table model is basically a flat (row, column) matrix. For this type of model, if you are asked for the row or column count of an invalid QModelIndex (i.e. the "root" of the model), then you return the actual number of rows or columns in the matrix. For any other valid QModelIndex, you return zero because that implies that you are being asked for the number of child rows or columns of a given cell, which a flat matrix doesn't have. Only tree-structured models can have children within children.
For any method that modifies the table model, -you- must implement the code to modify the data array (list of lists) that underlies the model. You need to look at it this way: your list of lists is -your- representation of the data you want to display and modify. MyTableModel is the "glue" that connects your internal model to Qt's world of table views. So everything that the table view does to request data from or modify data in your list of lists goes through the MyTableModel glue. QAbstractTableModel, the base class, stores absolutely nothing. It is merely the definition of the interface that specifies the rules by which views interact with your model and how the model informs the views that its content is or has changed.But it is not clear to me from the docs whether my implementation of insertRows() MUST do all the actual inserting (is insertRow/s actually inserting a row in the model's modelindex and not the model data?)
So your implementation has to do that. In the first case, you create a new row with the right number of empty column cells (i.e. a one element list containing a list of columns), append your existing list of lists to it, then assign the result back to your list of lists variable. In the second case, you need to create the same one element list of lists, then stick it on the end of the existing list. For a row in between you need to copy the existing list up to just before the indicated row, stick the new list onto that, then copy the rest of your existing list after that. The "insert" semantics imply "insert before" except when the insertion point is greater or equal to the number of rows.The docs also say:
"If row is 0, the rows are prepended to any existing rows in the parent.
If row is rowCount(), the rows are appended to any existing rows in the parent."
beginInsertRows() (or any of the "begin...() methods) emits a signal that the view listens for. When it gets that signal, it suspends any operations on the model, because the model has just told it it is changing and any model indexes the view has could become invalid or point to something else. endInsertRows() (or any end...() method) emits another signal that says, OK, I'm done making changes, you can now update that part of the view that was affected. modelAboutToBeReset() and modelReset() are the "nuclear option" - they tell the view that everything you know is wrong and that when it is all over you'll have to retrieve everything. These are useful when some external calculation updates the whole data set that the model wraps and there isn't really a way to do a partial update.And what does the view actually do when beginInsertRows/endInsertRows are called?
Bookmarks