QStandardItemModel: add columns to row before adding the row to the model?
-
When I use a QSortFilterProxyModel and add rows to my model AFTER assigning the proxy, the filterAcceptsRow method gets executed for every column of my new row.
It puzzled me for a while, but it is actually the result of the way I am creating my rows:
_proxyModel->insertRow(2); qDebug() << "row inserted"; _proxyModel->setData(_proxyModel->index(2, 0), "First column"); qDebug() << "Column 1 set"; _proxyModel->setData(_proxyModel->index(2, 1), "Second column")); qDebug() << "Column 2 set"; _proxyModel->setData(_proxyModel->index(2, 2), "Third column")); qDebug() << "Column 3 set";
This results in QSortFilterProxyModel::filterAcceptsRow beeing called 4 times for that single row.
I first create an empty row, which triggers the method.. and then every time I add a new column it has to trigger the method again.Now my question is:
How do it create my row outside of the model, add my columns and only when my row is complete... add it to my model?
Or should I just do a columnCount() inside filterAcceptsRow to verify if my row is valid? -
@huppeldepup said in QStandardItemModel: add columns to row before adding the row to the model?:
How do it create my row outside of the model, add my columns and only when my row is complete... add it to my model?
I am not aware that you can do this. There isn't a "set data for whole row in one go" call.There is an insert data for whole row as per @ChrisW67's reply below.To prevent the incremental filtering/sorting you are supposed to temporarily set dynamicSortFilter to false:
This property holds whether the proxy model is dynamically sorted and filtered whenever the contents of the source model change
Note that you should not update the source model through the proxy model when dynamicSortFilter is true
-
@huppeldepup said in QStandardItemModel: add columns to row before adding the row to the model?:
How do it create my row outside of the model, add my columns and only when my row is complete... add it to my model?
Create a QList of QStandardItems, one per column, and then call QStandardItemModel::insertRow() with the list. I am not sure how this interacts with the protected filter functions.