Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Remove row from QTableView
QtWS25 Last Chance

Remove row from QTableView

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 747 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.
  • T Offline
    T Offline
    tilz0R
    wrote on last edited by
    #1

    I have a QTableView and custom QStandardItemModel class implementation. In the application, user is allowed to select the rows and delete them by pressing the button.

    I implemented delete, which seems to work fine, but I am not sure if this is the right and recommended way in Qt.

    Function called when button is pressed. I am interested in rows only. I get list of ModelIndex(), that I pass to custom model for delete operation:

        def delete_selected_item_entries(self):
            selecteditems = self.ui.tableView_items.selectionModel().selectedRows()
            self.model_items.remove_rows_list([item.row() for item in selecteditems])
    

    In my model, I have implemented remove_rows_list. Idea is to first sort the indexes from largest to smallest, and then call begin/endRemoveRows functions for each index. Indexes may not be in a sequence.

    My model data array is in table_data variable.

        # Remove rows
        def remove_rows_list(self, list_of_rows:list[int]):
            # Sort indexes from largest to smallest,
            # to delete array from end to beginning.
            # This is to avoid array data shifting when deleting element
            list_of_rows = sorted(list_of_rows, reverse = True)
    
            # We do not expect big list, so we can call begin/end often...
            for index in list_of_rows:
                self.beginRemoveRows(QModelIndex(), index, index)
                del self.table_data[index]
                self.endRemoveRows()
    

    Is this the correct way, or is there better Qt suggested way?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should only need to re-implement the removeRows method. There's no need to reinvent the wheel here.

      However, why are you using QStandardItemModel as base model since you want to manage your own data structure ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You should only need to re-implement the removeRows method. There's no need to reinvent the wheel here.

        However, why are you using QStandardItemModel as base model since you want to manage your own data structure ?

        T Offline
        T Offline
        tilz0R
        wrote on last edited by tilz0R
        #3

        @SGaist Thanks. Is the removeRows a function view is supposed to call directly, or is this function called through any QTableView API?

        I agree that QAbstractItemModel makes more sense indeed.

        SGaistS 1 Reply Last reply
        0
        • T tilz0R

          @SGaist Thanks. Is the removeRows a function view is supposed to call directly, or is this function called through any QTableView API?

          I agree that QAbstractItemModel makes more sense indeed.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @tilz0R not necessarily no but it's a standard method that can be called from any class manipulating the model.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply
          0
          • SGaistS SGaist

            @tilz0R not necessarily no but it's a standard method that can be called from any class manipulating the model.

            T Offline
            T Offline
            tilz0R
            wrote on last edited by
            #5

            @SGaist ok, your point makes sense. I updated the model and now I call it from view directly.

            Thanks.

            1 Reply Last reply
            0
            • T tilz0R has marked this topic as solved on

            • Login

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