PDA

View Full Version : What actually calls QAbstractionTableModel::insertRows() ?



jmalicke
4th July 2014, 19:45
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) 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 manually calls table->insertRows()?

ChrisW67
4th July 2014, 21:49
Anything that needs to add a row into a model will use insertRows() directly or indirectly. Your code might do that in response to a direct user command (as in the case of your Add Row button). It might happen less directly as the result a paste or drag and drop operation. Your code may use this function while you are loading the initial data into a model (like inserting an entry into a combo box does with the underlying model). If you use the model interface in any sort of internal computation you might insert a result row using this function ( no human or view required)