Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. beginRemoveRows in QSortFilterProxyModel is not working as expected

beginRemoveRows in QSortFilterProxyModel is not working as expected

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 785 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    divaindie
    wrote on last edited by
    #1

    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??

    JonBJ 1 Reply Last reply
    0
    • D divaindie

      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??

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      1
      • E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3
        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.

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved