Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

beginRemoveRows in QSortFilterProxyModel is not working as expected



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

    class customModelfilterClass : public QSortFilterProxyModel
    {
       Q_OBJECT
       Q_PROPERTY(QLSqlTimeTableModel* sourceModel_m READ sourceModel CONSTANT FINAL)
    
        public:
        customModelfilterClass(QObject *parent = nullptr,QAbstractItemModel * sourceModel = nullptr);
        ~customModelfilterClass();
        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) ;
     beginRemoveRows(id.parent(),row,row);
     bool k =removeRows(row,1,QModelIndex());
     qDebug() << k;
    k = sourceModel()->submitAll();
     qDebug() << k;
      endRemoveRows();
     return k;
    }
    

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

    //main.c
    QLSqlTimeTableModel timeTableObj(nullptr,*question_table);
     timeTableObj.setTable("MON");
    timeTableObj.setEditStrategy(QSqlTableModel::OnManualSubmit);
     timeTableObj.select();
     timeTableObj.generateRoleNames();
     customModelfilterClass proxyModel;
     proxyModel.setSourceModel(&timeTableObj);
    

    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:

     beginRemoveRows(id.parent(),row,row);
     bool k =removeRows(row,1,QModelIndex());
     qDebug() << k;
    k = sourceModel()->submitAll();
     qDebug() << k;
      endRemoveRows();
    

    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