Qt6 topics merged with General

beginRemoveRows in QSortFilterProxyModel is not working as expected

  • Hi All,
    I have a derived class of QSortFilterProxyModel as below

    class customModelfilterClass : public QSortFilterProxyModel
       Q_PROPERTY(QLSqlTimeTableModel* sourceModel_m READ sourceModel CONSTANT FINAL)
        customModelfilterClass(QObject *parent = nullptr,QAbstractItemModel * sourceModel = nullptr);
        void setSourceModel(QAbstractItemModel * sourceModel);
        QLSqlTimeTableModel * sourceModel();
         Q_INVOKABLE bool removeRow(int row, const QModelIndex &parent = QModelIndex());

    here i have overrided the "removerow" function as below

    bool customModelfilterClass::removeRow(int row, const QModelIndex &parent)
     QModelIndex id = this->index(row,0) ;
     bool k =removeRows(row,1,QModelIndex());
     qDebug() << k;
    k = sourceModel()->submitAll();
     qDebug() << k;
     return k;

    and in main.c file i have a class derived from QSqlRelationalTableModel is set as the sourcemodel for customModelfilterClass.

    QLSqlTimeTableModel timeTableObj(nullptr,*question_table);
     customModelfilterClass proxyModel;

    Iam setting proxyModel as the model for listview in qml.everything is working as expected but to delete a row in the model iam calling removerow from qml as below

    itemdelg.ListView.view.model.removeRow(index) //here itemdelg delegate for listview

    but whenever i delete the first row,first two rows are vanished from the listview instead of one.
    strange thing is that when i opened sqlite db file manually in linux command line, i can see only one row (as expected correct is deleted !!)was deleted in the table!!.

    i suspect beginRemoveRows is not working as expected,because if i want to delete a single row, input what iam providing is not correct(i mean 'first' and 'last' args) .what iam doing rong here ?? please let me know a solution.

    also i have one more question.
    if i want to trigger the remove,move ,add etc.. transition in listview ,is it mandatory to call beginRemoveRows() and endRemoveRows() in model object??

  • @divaindie
    I could be wrong (has happened before!), but:

     bool k =removeRows(row,1,QModelIndex());
     qDebug() << k;
    k = sourceModel()->submitAll();
     qDebug() << k;

    I don't think you're supposed to submit model changes while you're still inside beginRemoveRows()... endRemoveRows()! They are supposed to bracket your row removal for the purposes of notifying the view correctly about what has happened. Move the submitAll() down to after the endRemoveRows(), any better?

  • bool k =removeRows(row,1,QModelIndex());

    I think this sends the signals. With your own begin... and end... calls you call those signals second time. The state of the model is actually different than what those signals let the view presume.

Log in to reply