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. Refresh data in view
Forum Updated to NodeBB v4.3 + New Features

Refresh data in view

Scheduled Pinned Locked Moved Solved Qt for Python
9 Posts 5 Posters 1.1k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Nightmaster
    wrote on last edited by
    #1

    Hello

    I am using QAbstractTableModel with QTableView. On one of my views a user is able to delete a row and this works fine. However the view does not update unless you close the window and go back in so looking at the documentation it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing.

    Please help.

    This is the code for my view

    def delete_appointment(self):
    
        self.index = self.table.selectionModel().currentIndex()
        
    
        self.s_date = self.index.siblingAtColumn(0).data()
        self.e_date = self.index.siblingAtColumn(1).data()
        self.location = self.index.siblingAtColumn(2).data()
    
        print(self.s_date, self.e_date, self.location) # for testing
    
        self.get_appt_id =main.selected_admin_delete_appt_id(self.s_date,self.e_date,self.location) # call sql query to get appointment id
    
        for id  in self.get_appt_id:
            self.get_appt_id1 = id[0]
    
           
        self.table.dataChanged( self.index,self.index,[QtCore.Qt.DisplayRole])
    
        main.admin_delete_appt(self.get_appt_id1)
    

    This is my data method in from my QAbstractTableModel subclass

    def data(self, QModelIndex, role= QtCore.Qt.DisplayRole):
    
        if role == QtCore.Qt.DisplayRole: # if role is string
    
            try:
    
                return  self.data[QModelIndex.row()][QModelIndex.column()]
    
    JonBJ 1 Reply Last reply
    0
    • N Nightmaster

      Hello

      I am using QAbstractTableModel with QTableView. On one of my views a user is able to delete a row and this works fine. However the view does not update unless you close the window and go back in so looking at the documentation it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing.

      Please help.

      This is the code for my view

      def delete_appointment(self):
      
          self.index = self.table.selectionModel().currentIndex()
          
      
          self.s_date = self.index.siblingAtColumn(0).data()
          self.e_date = self.index.siblingAtColumn(1).data()
          self.location = self.index.siblingAtColumn(2).data()
      
          print(self.s_date, self.e_date, self.location) # for testing
      
          self.get_appt_id =main.selected_admin_delete_appt_id(self.s_date,self.e_date,self.location) # call sql query to get appointment id
      
          for id  in self.get_appt_id:
              self.get_appt_id1 = id[0]
      
             
          self.table.dataChanged( self.index,self.index,[QtCore.Qt.DisplayRole])
      
          main.admin_delete_appt(self.get_appt_id1)
      

      This is my data method in from my QAbstractTableModel subclass

      def data(self, QModelIndex, role= QtCore.Qt.DisplayRole):
      
          if role == QtCore.Qt.DisplayRole: # if role is string
      
              try:
      
                  return  self.data[QModelIndex.row()][QModelIndex.column()]
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Nightmaster said in Refresh data in view:

      it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing

      dataChanged() is for altering data in existing rows, only. For deleting a row to be noticed by QTableView and update correctly you must implement removeRows() to call begin/endRemoveRows(), and similarly for inserting rows, in your QAbstractTableModel-derived class as per https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing (oh, you are Python, so https://doc.qt.io/qtforpython-6.5/PySide6/QtCore/QAbstractTableModel.html#subclassing).

      N 1 Reply Last reply
      3
      • F Offline
        F Offline
        friedemannkleint
        wrote on last edited by
        #3

        See the Editable Tree Model Example .

        JonBJ 1 Reply Last reply
        3
        • F friedemannkleint

          See the Editable Tree Model Example .

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • JonBJ JonB

            @Nightmaster said in Refresh data in view:

            it seems like DataChanged signal would resolve this issue. I have tried it and it does nothing

            dataChanged() is for altering data in existing rows, only. For deleting a row to be noticed by QTableView and update correctly you must implement removeRows() to call begin/endRemoveRows(), and similarly for inserting rows, in your QAbstractTableModel-derived class as per https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing (oh, you are Python, so https://doc.qt.io/qtforpython-6.5/PySide6/QtCore/QAbstractTableModel.html#subclassing).

            N Offline
            N Offline
            Nightmaster
            wrote on last edited by
            #5

            @JonB Hi Jon, thank you for your help with this.

            I have implemented remove rows as the documentation suggests.

            def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs):
            
                QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1)
                self.data.removeAt(p_int)
            
                QAbstractItemModel.endRemoveRows()
            

            I am calling it in my delete appointment method (refer to my og post for that code)

            TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

            This is an excert from another method where i set the datamodel

              def admin_view_appts(self): 
            
                self.view_appts_table = QtWidgets.QWidget()
            
                self.view_appts_table.resize(825, 800)  # set window size
                self.get_all_appts = main.admin_view_appts()
                self.list = []  # initialize list
                self.view_appt_list = []
              
                **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
            

            I am getting the following error,

            " TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'

            i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel

            TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

            I hope this makes sense

            jsulmJ Pl45m4P JonBJ 3 Replies Last reply
            0
            • N Nightmaster

              @JonB Hi Jon, thank you for your help with this.

              I have implemented remove rows as the documentation suggests.

              def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs):
              
                  QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1)
                  self.data.removeAt(p_int)
              
                  QAbstractItemModel.endRemoveRows()
              

              I am calling it in my delete appointment method (refer to my og post for that code)

              TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

              This is an excert from another method where i set the datamodel

                def admin_view_appts(self): 
              
                  self.view_appts_table = QtWidgets.QWidget()
              
                  self.view_appts_table.resize(825, 800)  # set window size
                  self.get_all_appts = main.admin_view_appts()
                  self.list = []  # initialize list
                  self.view_appt_list = []
                
                  **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
              

              I am getting the following error,

              " TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'

              i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel

              TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

              I hope this makes sense

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Nightmaster Just call

              self.beginRemoveRows(QModelIndex(),p_int,p_int_1)
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • N Nightmaster

                @JonB Hi Jon, thank you for your help with this.

                I have implemented remove rows as the documentation suggests.

                def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs):
                
                    QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1)
                    self.data.removeAt(p_int)
                
                    QAbstractItemModel.endRemoveRows()
                

                I am calling it in my delete appointment method (refer to my og post for that code)

                TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

                This is an excert from another method where i set the datamodel

                  def admin_view_appts(self): 
                
                    self.view_appts_table = QtWidgets.QWidget()
                
                    self.view_appts_table.resize(825, 800)  # set window size
                    self.get_all_appts = main.admin_view_appts()
                    self.list = []  # initialize list
                    self.view_appt_list = []
                  
                    **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
                

                I am getting the following error,

                " TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'

                i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel

                TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

                I hope this makes sense

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #7

                @Nightmaster said in Refresh data in view:

                i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel

                Because your self = "this" class, where you want to init your remove procedure is your TableModel and not QAbstractTableModel. Calling the super/base class implementation QAbstractItemModel.beginRemoveRows does not suit your model, since, as the error tells, it expects a generic QAbstractItemModel and not your custom implementation.
                Therefore see @jsulm 's answer.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                1 Reply Last reply
                2
                • N Nightmaster

                  @JonB Hi Jon, thank you for your help with this.

                  I have implemented remove rows as the documentation suggests.

                  def removeRows(self, p_int, p_int_1, parent=None, *args, **kwargs):
                  
                      QAbstractItemModel.beginRemoveRows(QModelIndex(),p_int,p_int_1)
                      self.data.removeAt(p_int)
                  
                      QAbstractItemModel.endRemoveRows()
                  

                  I am calling it in my delete appointment method (refer to my og post for that code)

                  TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

                  This is an excert from another method where i set the datamodel

                    def admin_view_appts(self): 
                  
                      self.view_appts_table = QtWidgets.QWidget()
                  
                      self.view_appts_table.resize(825, 800)  # set window size
                      self.get_all_appts = main.admin_view_appts()
                      self.list = []  # initialize list
                      self.view_appt_list = []
                    
                      **self.appts_data_model = TableModel(self.view_appt_list)** set the model here
                  

                  I am getting the following error,

                  " TypeError: beginRemoveRows(self, QModelIndex, int, int): first argument of unbound method must have type 'QAbstractItemModel'

                  i do not understand because the first arguement of TableModel.removeRow is self.apps_data_model which is of type TableModel which subclasses QAbstractTableModel which inherits from QAbstractItemModel

                  TableModel.removeRow(self.appts_data_model, 1, QModelIndex())

                  I hope this makes sense

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

                  @Nightmaster
                  As the other two have said so far.

                  I am confused by what your TableModel class actually is. Is it a subclass of QAbstractTableModel? It should be. But have you defined it as some other class which encapsulates (i.e. has a member variable rather than being derived from) which is a QAbstractTableModel? You need to be subclassing QAbstractTableModel and overriding the virtual methods in that derived class directly.

                  1 Reply Last reply
                  1
                  • N Offline
                    N Offline
                    Nightmaster
                    wrote on last edited by
                    #9

                    Thanks for your help guys i have solved the issue :).

                    1 Reply Last reply
                    0
                    • N Nightmaster 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