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

Editing data in a tableview

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 976 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.
  • M Offline
    M Offline
    MIckH
    wrote on last edited by
    #1

    Hi,
    I have been learning QT from a book called "Create GUI applications with Python and QT6" by Martin Fitzpatrick. I have been following along on how to create a tableview using a dataframe as data. and I understand the concepts(basically) of the model principle. My question though is this, I cannot edit the data in the cells. I wish to be able at least be able to modify that, I then want to update the dataframe with the changes. I will list the code below.
    Can anyone help me please?

    import sys

    import pandas as pd
    from PyQt6 import QtCore, QtGui, QtWidgets
    from PyQt6.QtCore import Qt

    class TableModel(QtCore.QAbstractTableModel):
    def init(self, data):
    super().init()
    self._data = data

    def data(self, index, role):
        if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole:
            value = self._data.iloc[index.row(), index.column()]
            return str(value)
    
    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 == Qt.ItemDataRole.DisplayRole:
            if orientation == Qt.Orientation.Horizontal:
                return str(self._data.columns[section])
    
            if orientation == Qt.Orientation.Vertical:
                return str(self._data.index[section])
    

    class MainWindow(QtWidgets.QMainWindow):
    def init(self):
    super().init()

        self.table = QtWidgets.QTableView()
    
        data = pd.DataFrame(
            [
                [1, 9, 2],
                [1, 0, -1],
                [3, 5, 2],
                [3, 3, 2],
                [5, 8, 9],
            ],
            columns=["A", "B", "C"],
            index=["Row 1", "Row 2", "Row 3", "Row 4", "Row 5"],
        )
    
        self.model = TableModel(data)
        self.table.setModel(self.model)
    
        self.setCentralWidget(self.table)
        self.setGeometry(600, 100, 400, 200)
    

    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec()

    JonBJ 1 Reply Last reply
    0
    • M MIckH

      Hi,
      I have been learning QT from a book called "Create GUI applications with Python and QT6" by Martin Fitzpatrick. I have been following along on how to create a tableview using a dataframe as data. and I understand the concepts(basically) of the model principle. My question though is this, I cannot edit the data in the cells. I wish to be able at least be able to modify that, I then want to update the dataframe with the changes. I will list the code below.
      Can anyone help me please?

      import sys

      import pandas as pd
      from PyQt6 import QtCore, QtGui, QtWidgets
      from PyQt6.QtCore import Qt

      class TableModel(QtCore.QAbstractTableModel):
      def init(self, data):
      super().init()
      self._data = data

      def data(self, index, role):
          if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole:
              value = self._data.iloc[index.row(), index.column()]
              return str(value)
      
      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 == Qt.ItemDataRole.DisplayRole:
              if orientation == Qt.Orientation.Horizontal:
                  return str(self._data.columns[section])
      
              if orientation == Qt.Orientation.Vertical:
                  return str(self._data.index[section])
      

      class MainWindow(QtWidgets.QMainWindow):
      def init(self):
      super().init()

          self.table = QtWidgets.QTableView()
      
          data = pd.DataFrame(
              [
                  [1, 9, 2],
                  [1, 0, -1],
                  [3, 5, 2],
                  [3, 3, 2],
                  [5, 8, 9],
              ],
              columns=["A", "B", "C"],
              index=["Row 1", "Row 2", "Row 3", "Row 4", "Row 5"],
          )
      
          self.model = TableModel(data)
          self.table.setModel(self.model)
      
          self.setCentralWidget(self.table)
          self.setGeometry(600, 100, 400, 200)
      

      app = QtWidgets.QApplication(sys.argv)
      window = MainWindow()
      window.show()
      app.exec()

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

      @MIckH
      First, please enclose blocks of code inside a line of just ``` above and below, or use the forum's </> Code icon when composing your post.

      You are subclassing QAbstractTableModel. So far you have provided the methods required for displaying what is in the model. Now you must add the methods required for editing So read https://doc.qt.io/qtforpython-6/PySide6/QtCore/QAbstractTableModel.html#subclassing

      Editable models need to implement setData() , and implement flags() to return a value containing ItemIsEditable .

      and also https://doc.qt.io/qtforpython-6/PySide6/QtCore/QAbstractItemModel.html#subclassing. I think https://www.pythonguis.com/faq/editing-pyqt-tableview/ covers this in first 2 code boxes.

      You might also be interested in https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QAbstractItemView.html#PySide6.QtWidgets.PySide6.QtWidgets.QAbstractItemView.setEditTriggers and https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QAbstractItemView.html#PySide6.QtWidgets.PySide6.QtWidgets.QAbstractItemView.editTriggers, which controls what user actions enter editing mode. I think the default is double-click to edit, but you can alter that if you wanted to.

      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