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. After removing rows in QTableView, gridlines still show.

After removing rows in QTableView, gridlines still show.

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.3k 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.
  • J Offline
    J Offline
    johnby
    wrote on last edited by johnby
    #1

    I am using the removeRows() function below for removing rows in a QTableView.

    It works, however, the problem is that although the function completes successfully and quickly, it might take a few seconds for the actual rows to be removed visually. Also, after the rows do visually disappear, the grid lines in the tableview still show for the exact number of items in the tableview. And when I add new rows to the table, the grid lines disappear as if it's being redrawn and looks correct.

    Here is my table model.

    class MyModel : public QAbstractTableModel
    {   
        QList<MyItem> items;
    
        public:
            MyModel(QObject *parent = nullptr);
        
            int rowCount(const QModelIndex &index = QModelIndex()) const override;
            int columnCount(const QModelIndex &index = QModelIndex()) const override; 
            QVariant data(const QModelIndex &index, int role) const override; 
            QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
            void append(const MyItem &item);
        
            bool removeRow(int row, const QModelIndex &parent = QModelIndex());
            bool removeRows(int row, int count, 
            const QModelIndex &parent = QModelIndex());
    };
    
    bool MyModel::removeRows(int row, int count, const QModelIndex &parent)
    {
        beginRemoveRows(parent, row, count);
    
        QList<MyItems>::iterator it;  
    
        for (it = items.begin(); it != items.end(); ++it)
            items.erase(it);
    
        endRemoveRows();
    
        return true;
    }
    

    Is there some sort of update function that I should be calling to clear the QTableView after removing?

    Update:
    Found reset(), which I call after removeRows() and the view now clears fast, but gridlines still appear. Also tried update() to no avail.

    Ketan__Patel__0011K 1 Reply Last reply
    0
    • J johnby

      I am using the removeRows() function below for removing rows in a QTableView.

      It works, however, the problem is that although the function completes successfully and quickly, it might take a few seconds for the actual rows to be removed visually. Also, after the rows do visually disappear, the grid lines in the tableview still show for the exact number of items in the tableview. And when I add new rows to the table, the grid lines disappear as if it's being redrawn and looks correct.

      Here is my table model.

      class MyModel : public QAbstractTableModel
      {   
          QList<MyItem> items;
      
          public:
              MyModel(QObject *parent = nullptr);
          
              int rowCount(const QModelIndex &index = QModelIndex()) const override;
              int columnCount(const QModelIndex &index = QModelIndex()) const override; 
              QVariant data(const QModelIndex &index, int role) const override; 
              QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
              void append(const MyItem &item);
          
              bool removeRow(int row, const QModelIndex &parent = QModelIndex());
              bool removeRows(int row, int count, 
              const QModelIndex &parent = QModelIndex());
      };
      
      bool MyModel::removeRows(int row, int count, const QModelIndex &parent)
      {
          beginRemoveRows(parent, row, count);
      
          QList<MyItems>::iterator it;  
      
          for (it = items.begin(); it != items.end(); ++it)
              items.erase(it);
      
          endRemoveRows();
      
          return true;
      }
      

      Is there some sort of update function that I should be calling to clear the QTableView after removing?

      Update:
      Found reset(), which I call after removeRows() and the view now clears fast, but gridlines still appear. Also tried update() to no avail.

      Ketan__Patel__0011K Offline
      Ketan__Patel__0011K Offline
      Ketan__Patel__0011
      wrote on last edited by
      #2

      @johnby

      if You want to display Rows itemwise Or Data Are The Itembase Then use QTableWidget instead of QTableView

      QTableView is ModelBased Widget And QTableWidget is ItemBased Widget

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @johnby said in After removing rows in QTableView, gridlines still show.:

        beginRemoveRows(parent, row, count);

        This is wrong. It's not count but endRow.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        JonBJ J 2 Replies Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @johnby said in After removing rows in QTableView, gridlines still show.:

          beginRemoveRows(parent, row, count);

          This is wrong. It's not count but endRow.

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

          @Christian-Ehrlicher
          Damn! Good spot! :)

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @johnby said in After removing rows in QTableView, gridlines still show.:

            beginRemoveRows(parent, row, count);

            This is wrong. It's not count but endRow.

            J Offline
            J Offline
            johnby
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in After removing rows in QTableView, gridlines still show.:

            @johnby said in After removing rows in QTableView, gridlines still show.:

            beginRemoveRows(parent, row, count);

            This is wrong. It's not count but endRow.

            Sorry, guess I'm not understanding this one. I see no endRow. Also, if count is the last row, that seems to be what beginREmoveRows is asking for in the last arg. No?

            Christian EhrlicherC JonBJ 2 Replies Last reply
            0
            • J johnby

              @Christian-Ehrlicher said in After removing rows in QTableView, gridlines still show.:

              @johnby said in After removing rows in QTableView, gridlines still show.:

              beginRemoveRows(parent, row, count);

              This is wrong. It's not count but endRow.

              Sorry, guess I'm not understanding this one. I see no endRow. Also, if count is the last row, that seems to be what beginREmoveRows is asking for in the last arg. No?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @johnby said in After removing rows in QTableView, gridlines still show.:

              I see no endRow

              Read the documentation in the link I gave you: https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveColumns

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • J johnby

                @Christian-Ehrlicher said in After removing rows in QTableView, gridlines still show.:

                @johnby said in After removing rows in QTableView, gridlines still show.:

                beginRemoveRows(parent, row, count);

                This is wrong. It's not count but endRow.

                Sorry, guess I'm not understanding this one. I see no endRow. Also, if count is the last row, that seems to be what beginREmoveRows is asking for in the last arg. No?

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

                @johnby
                Apart from whether it's endRow/last/count, you're not actually obeying it (nor row) in your code for what you erase.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  johnby
                  wrote on last edited by
                  #8

                  Ok, I'm assuming you meant to send me the link to https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows

                  Anyways, I think I see what you were referring to, however, I was just trying to delete all rows using iters and ignoring the first and last, but I changed it using first and last.

                  bool MyModel::removeRows(int first, int last, const QModelIndex &parent)
                  {
                      QList<MyItems>::iterator it;
                  
                      beginRemoveRows(parent, first, last);
                  
                      for (int row = last - 1; row >= 0; --row)
                          items.removeAt(row);
                  
                      endRemoveRows();
                  }
                  

                  So even though this does remove all items in the list, I still have the issue of gridlines staying in the view. Thoughts?

                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #9

                    @johnby said in After removing rows in QTableView, gridlines still show.:

                    Thoughts?

                    I would guess first is not 0.
                    If you want to remove all rows use begin/endResetModel() and clear your complete container - it's faster.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • J johnby

                      Ok, I'm assuming you meant to send me the link to https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows

                      Anyways, I think I see what you were referring to, however, I was just trying to delete all rows using iters and ignoring the first and last, but I changed it using first and last.

                      bool MyModel::removeRows(int first, int last, const QModelIndex &parent)
                      {
                          QList<MyItems>::iterator it;
                      
                          beginRemoveRows(parent, first, last);
                      
                          for (int row = last - 1; row >= 0; --row)
                              items.removeAt(row);
                      
                          endRemoveRows();
                      }
                      

                      So even though this does remove all items in the list, I still have the issue of gridlines staying in the view. Thoughts?

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

                      @johnby said in After removing rows in QTableView, gridlines still show.:

                      for (int row = last - 1; row >= 0; --row)

                      Again, you cannot/should not do this. You must respect the parameters passed in, both first & last. Otherwise what you remove here does not correspond to the beginRemoveRows() call you make, and that will upset any view attached to the model.

                      1 Reply Last reply
                      1

                      • Login

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