Important: Please read the Qt Code of Conduct -

[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:

    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
    index_source = myBudget->models.cases_and_accesories->sourceModel()->index(row, 4);
    index_this = myBudget->models.cases_and_accesories->mapFromSource(index_source);
    qDebug() << Q_FUNC_INFO << QString("index_this: %1, %2; index_source: %3,%4")


    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.


    The '|^$' did the trick. The rest of the code remains similar to the one I was using above,

    QModelIndex index;
    QModelIndex sourceIndex;
    int row = myBudget->models.presupuestoCompleto->rowCount();
    sourceIndex = myBudget->models.presupuestoCompleto->index(row, 4);
    index = myBudget->models.doors_and_drawers->mapFromSource(sourceIndex);

    Just need to do the index mapping, and it works!

    Thank you for reading and sorry for the noise ;)

Log in to reply