[SOLVED] QSqlTableModel::insertRow() from QSortFilterProxyModel
-
Hello.
I am trying to get a new row inserted into a QSqlTableModel from a proxy model.
I am aware of the map{To,From}Source methods, but I am not having much luck. Probably because I don't get the logic behind this.
Right now I am trying this:
[code]
QModelIndex index_this;
QModelIndex index_source;// get row count from the source model int row = myBudget->models.cases_and_accesories->sourceModel()->rowCount(); // this, unfortunately, does nothing in the proxy model, which is what I'd like myBudget->models.cases_and_accesories->sourceModel()->insertRow(row); index_source = myBudget->models.cases_and_accesories->sourceModel()->index(row, 4); index_this = myBudget->models.cases_and_accesories->mapFromSource(index_source); ui->tableView_cascosYAccesorios->setCurrentIndex(index_this); qDebug() << Q_FUNC_INFO << QString("index_this: %1, %2; index_source: %3,%4") .arg(index_this.row()).arg(index_this.column()) .arg(index_source.row()).arg(index_source.column()); ui->tableView_cascosYAccesorios->edit(index_this);
[/code]
qDebug() reports that index_source has sane and adequate values, but index_this is -1,-1, so, unsurprisingly, I can't edit anything in the proxy model (there's no new row either, so no surprise there).
I am sure there must be something simple I am missing...
Can anyone help me on this one? I have looked at the examples, the addressbook particularly, and they seem to pick the row from the source model, just as I do. But surely there's some other bit that I am not getting.
Thank you for any help.
-
I'll answer myself.
The problem has, in fact, nothing to do with the proxy (well, it has, but not in the direct way that I thought).
After losing a few valuable hours of work, I thought of commenting out the filters, and it started working. I just had to add to the setFilterRegExp() expression the empty string.
[code]
models.cases_and_accesories->setFilterRegExp(QRegExp(".AL..|.BA..|.BT..|.COL.|.SEN.|.SEB.|^$"));
[/code]The '|^$' did the trick. The rest of the code remains similar to the one I was using above,
[code]
QModelIndex index;
QModelIndex sourceIndex;
int row = myBudget->models.presupuestoCompleto->rowCount();
myBudget->models.presupuestoCompleto->insertRow(row);
sourceIndex = myBudget->models.presupuestoCompleto->index(row, 4);
index = myBudget->models.doors_and_drawers->mapFromSource(sourceIndex);
ui->tableView_PuertasYCajones->edit(index);
[/code]Just need to do the index mapping, and it works!
Thank you for reading and sorry for the noise ;)