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. how to update only one row of model in qtableview @ pyside6
Forum Updated to NodeBB v4.3 + New Features

how to update only one row of model in qtableview @ pyside6

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 1.1k 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.
  • A Offline
    A Offline
    Aiden.k
    wrote on last edited by
    #1

    I want to update only 1 row or 1 cell.

    but all cell is updated when datachanged is emitted.

    data() was called hundreds times for each cells and each roles.

    what is wrong with this code ?

    class TestitemTableModel(QAbstractTableModel):

    def __init__(self, data):
        super(TestitemTableModel, self).__init__()
        self._data = data
    
    def data(self, index, role):
    
        if role == QtCore.Qt.DisplayRole:
            value = self._data.iloc[index.row(), index.column()]
            return str(value)
    
        if role == QtCore.Qt.TextAlignmentRole:
            return int(QtCore.Qt.AlignCenter | QtCore.Qt.AlignVCenter)
    
    
    def setData(self, index, value, role):
    
        if not index.isValid():
            return False
    
        if role == QtCore.Qt.DisplayRole:
            self._data.iloc[index.row(), index.column()] = 'WTF'
            
            self.dataChanged.emit(index, index)
    
            return True
    
        return QAbstractTableModel.setData(self,index,value,role)
    
    def rowCount(self, index):
        return self._data.shape[0]
    
    def columnCount(self, index):
        return self._data.shape[1]
    
    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return str(self._data.columns[section])
            
            if orientation == QtCore.Qt.Vertical:
                return str(self._data.index[section])
    
    JonBJ 1 Reply Last reply
    0
    • A Aiden.k

      I want to update only 1 row or 1 cell.

      but all cell is updated when datachanged is emitted.

      data() was called hundreds times for each cells and each roles.

      what is wrong with this code ?

      class TestitemTableModel(QAbstractTableModel):

      def __init__(self, data):
          super(TestitemTableModel, self).__init__()
          self._data = data
      
      def data(self, index, role):
      
          if role == QtCore.Qt.DisplayRole:
              value = self._data.iloc[index.row(), index.column()]
              return str(value)
      
          if role == QtCore.Qt.TextAlignmentRole:
              return int(QtCore.Qt.AlignCenter | QtCore.Qt.AlignVCenter)
      
      
      def setData(self, index, value, role):
      
          if not index.isValid():
              return False
      
          if role == QtCore.Qt.DisplayRole:
              self._data.iloc[index.row(), index.column()] = 'WTF'
              
              self.dataChanged.emit(index, index)
      
              return True
      
          return QAbstractTableModel.setData(self,index,value,role)
      
      def rowCount(self, index):
          return self._data.shape[0]
      
      def columnCount(self, index):
          return self._data.shape[1]
      
      def headerData(self, section, orientation, role):
          if role == QtCore.Qt.DisplayRole:
              if orientation == QtCore.Qt.Horizontal:
                  return str(self._data.columns[section])
              
              if orientation == QtCore.Qt.Vertical:
                  return str(self._data.index[section])
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Aiden-k
      I don't know exactly what your problem is, or what you are trying to do.

      Qt infrastructure will call data() multiple times with different roles.

      Your setData() sets the data on the DisplayRole. This is wrong. setData() is called with EditRole to make an update on the value. Start by getting that right.

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Aiden.k
        wrote on last edited by
        #3

        This situation is occured by below option.

        tableview.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)

        Works fine when I disable that option.

        1 Reply Last reply
        0

        • Login

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