Reputation:
I implemented QAbstractTableModel with the usual:
class PrintIntervalTableModel : public QAbstractTableModel
{
private:
virtual int rowCount (const QModelIndex & parent = QModelIndex()) const;
virtual int columnCount (const QModelIndex & parent = QModelIndex()) const;
virtual QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const;
virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
virtual bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
virtual Qt::ItemFlags flags (const QModelIndex & index) const;
virtual bool insertRows (int position, int rows, const QModelIndex & parent = QModelIndex());
virtual bool removeRows (int position, int rows, const QModelIndex & parent = QModelIndex());
Here is my insert rows, which is pretty simple:
bool PrintIntervalTableModel::insertRows(int position, int rows, const QModelIndex & parent)
{
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
{
std::deque<moment_value_pair_type>::iterator it = printIntervalPairs.begin() + position;
printIntervalPairs.insert(it, moment_value_pair_type());
}
endInsertRows();
return true;
}
Now I wonder why I actually did this? Do views (or other components) automatically call this method?
I would like to have a button on the form that, once clicked, inserts a row underneath the user's current selection. Do I basically create a slot in the table (connected to button clicked()) that figures out where to insert the row, and then the slot would call table->insertRows()? Is this the type of purpose the insertRows() override is used for?
Upvotes: 5
Views: 12210
Reputation: 5466
A bit late, but anyway:
Now I wonder why I actually did this? Do views (or other components) automatically call this method?
Yes, insertRows()
is called by the default implementation of QAbstractItemModel::dropMimeData()
, which gets called when items from another Qt view are dropped there.
Likewise, removeRows()
is called by the default implementation of QAbstractItemView::startDrag()
.
I couldn't find other usages in the source.
Upvotes: 4
Reputation: 4076
Do I basically create a slot in the table (connected to button clicked()) that figures out where to insert the row, and then the slot would call table->insertRows()? Is this the type of purpose the insertRows() override is used for?
Yes (Intented to be used by the view through QAbstractItemModel interface). You can look at the source code of QTableWidget as example.
void QTableWidget::insertRow(int row)
{
Q_D(QTableWidget);
d->tableModel()->insertRows(row);
}
You will notice (h file) insertRow here is a slot (to notify it typically by means of a selection model signal). It then calls insertRows (the function that you've overridden). A typical implementation can be found in the sources:
bool QTableModel::insertRows(int row, int count, const QModelIndex &)
{
if (count < 1 || row < 0 || row > verticalHeaderItems.count())
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
int rc = verticalHeaderItems.count();
int cc = horizontalHeaderItems.count();
verticalHeaderItems.insert(row, count, 0);
if (rc == 0)
tableItems.resize(cc * count);
else
tableItems.insert(tableIndex(row, 0), cc * count, 0);
endInsertRows();
return true;
}
Upvotes: 3