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. Can't set the model data in tableview

Can't set the model data in tableview

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 892 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 13 Jul 2023, 08:10 last edited by
    #1

    Hi All,

    I have a confusing error occurring on this line below self.tableView.setModel(self.model)
    the error is:
    TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'QStandardItemModel'

    now the code works in another form listed below this one. The differences being the broken one has most of the gui code in Ui_MainWindow. Im trying to keep the ui code separate so I can easily update it using QT designer. I just don't understand why the model code doesn't work anymore, I hope you can help.

    cheers MickH

    Broken code

    class Budget(QMainWindow, Ui_MainWindow):
    def init(self):
    super().init()

        self.setupUi(self)
        self.show()
    
        # You can still override values from your UI file within your code,
        # but if possible, set them in Qt Creator. See the properties panel.
        f = self.label.font()
        f.setPointSize(25)
        self.label.setAlignment(
            Qt.AlignmentFlag.AlignHCenter
            | Qt.AlignmentFlag.AlignVCenter
        )
        self.label.setFont(f)
    
        # Signals from UI widgets can be connected as normal.
        self.pushButton.pressed.connect(self.update_label)
    
        # self.model= QItemSelectionModel()
        # Add the column headers to the model
        headers = list(dpTest.columns)
        headers.append("tick")
    
        self.model = QStandardItemModel()
        self.model.setHorizontalHeaderLabels(headers)
    
        # Loop through each row in the pandas dataframe and add the corresponding data to the QStandardItemModel
        for i in range(dpTest.shape[0]):
            row = []
            for j in range(dpTest.shape[1]):
                item = QStandardItem(str(dpTest.iloc[i, j]))
                row.append(item)
            tick_box = QStandardItem()
            tick_box.setCheckable(True)
            row.append(tick_box)
            self.model.appendRow(row)
    
        # Set the model for the table view
        self.tableView.setModel(self.model)
        self.tableView.setEditTriggers(QTableView.NoEditTriggers)
        self.tableView.setSelectionBehavior(QTableView.SelectRows)
        self.tableView.setSelectionMode(QTableView.SingleSelection)
        self.tableView.verticalHeader().setVisible(False)
        self.tableView.setHorizontalScrollMode(QTableView.ScrollPerPixel)
        self.tableView.setVerticalScrollMode(QTableView.ScrollPerPixel)
        self.tableView.setShowGrid(False)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
    
    # Set the resize mode for the last column
    
        self.tableView.horizontalHeader().setSectionResizeMode(
            dpTest.shape[1], QHeaderView.ResizeToContents)
    
    # adjust the size to your preference
        self.tableView.setColumnWidth(dpTest.shape[1], 30)
        self.tableView.move(550, 150)
        self.tableView.setFixedSize(500, 250)
    

    working code###

    class MainWindow(QWidget):

    def __init__(self):
        super().__init__()
    
        table_view = QTableView()
        model = QStandardItemModel()
    
        # Add the column headers to the model
        headers = list(dpTest.columns)
        headers.append("tick")
        model.setHorizontalHeaderLabels(headers)
    
        # Loop through each row in the pandas dataframe and add the corresponding data to the QStandardItemModel
        for i in range(dpTest.shape[0]):
            row = []
            for j in range(dpTest.shape[1]):
                item = QStandardItem(str(dpTest.iloc[i, j]))
                row.append(item)
            tick_box = QStandardItem()
            tick_box.setCheckable(True)
            row.append(tick_box)
            model.appendRow(row)
    
        # Set the model for the table view
        table_view.setModel(model)
        table_view.setEditTriggers(QTableView.NoEditTriggers)
        table_view.setSelectionBehavior(QTableView.SelectRows)
        table_view.setSelectionMode(QTableView.SingleSelection)
        table_view.verticalHeader().setVisible(False)
        table_view.setHorizontalScrollMode(QTableView.ScrollPerPixel)
        table_view.setVerticalScrollMode(QTableView.ScrollPerPixel)
        table_view.setShowGrid(False)
        table_view.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
    
        # Set the resize mode for the last column
    
        table_view.horizontalHeader().setSectionResizeMode(
            dpTest.shape[1], QHeaderView.ResizeToContents)
    
        # adjust the size to your preference
        table_view.setColumnWidth(dpTest.shape[1], 30)
        table_view.move(550, 150)
        table_view.setFixedSize(500, 250)
    
    J 1 Reply Last reply 13 Jul 2023, 09:28
    0
    • M MIckH
      13 Jul 2023, 08:10

      Hi All,

      I have a confusing error occurring on this line below self.tableView.setModel(self.model)
      the error is:
      TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'QStandardItemModel'

      now the code works in another form listed below this one. The differences being the broken one has most of the gui code in Ui_MainWindow. Im trying to keep the ui code separate so I can easily update it using QT designer. I just don't understand why the model code doesn't work anymore, I hope you can help.

      cheers MickH

      Broken code

      class Budget(QMainWindow, Ui_MainWindow):
      def init(self):
      super().init()

          self.setupUi(self)
          self.show()
      
          # You can still override values from your UI file within your code,
          # but if possible, set them in Qt Creator. See the properties panel.
          f = self.label.font()
          f.setPointSize(25)
          self.label.setAlignment(
              Qt.AlignmentFlag.AlignHCenter
              | Qt.AlignmentFlag.AlignVCenter
          )
          self.label.setFont(f)
      
          # Signals from UI widgets can be connected as normal.
          self.pushButton.pressed.connect(self.update_label)
      
          # self.model= QItemSelectionModel()
          # Add the column headers to the model
          headers = list(dpTest.columns)
          headers.append("tick")
      
          self.model = QStandardItemModel()
          self.model.setHorizontalHeaderLabels(headers)
      
          # Loop through each row in the pandas dataframe and add the corresponding data to the QStandardItemModel
          for i in range(dpTest.shape[0]):
              row = []
              for j in range(dpTest.shape[1]):
                  item = QStandardItem(str(dpTest.iloc[i, j]))
                  row.append(item)
              tick_box = QStandardItem()
              tick_box.setCheckable(True)
              row.append(tick_box)
              self.model.appendRow(row)
      
          # Set the model for the table view
          self.tableView.setModel(self.model)
          self.tableView.setEditTriggers(QTableView.NoEditTriggers)
          self.tableView.setSelectionBehavior(QTableView.SelectRows)
          self.tableView.setSelectionMode(QTableView.SingleSelection)
          self.tableView.verticalHeader().setVisible(False)
          self.tableView.setHorizontalScrollMode(QTableView.ScrollPerPixel)
          self.tableView.setVerticalScrollMode(QTableView.ScrollPerPixel)
          self.tableView.setShowGrid(False)
          self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
      
      # Set the resize mode for the last column
      
          self.tableView.horizontalHeader().setSectionResizeMode(
              dpTest.shape[1], QHeaderView.ResizeToContents)
      
      # adjust the size to your preference
          self.tableView.setColumnWidth(dpTest.shape[1], 30)
          self.tableView.move(550, 150)
          self.tableView.setFixedSize(500, 250)
      

      working code###

      class MainWindow(QWidget):

      def __init__(self):
          super().__init__()
      
          table_view = QTableView()
          model = QStandardItemModel()
      
          # Add the column headers to the model
          headers = list(dpTest.columns)
          headers.append("tick")
          model.setHorizontalHeaderLabels(headers)
      
          # Loop through each row in the pandas dataframe and add the corresponding data to the QStandardItemModel
          for i in range(dpTest.shape[0]):
              row = []
              for j in range(dpTest.shape[1]):
                  item = QStandardItem(str(dpTest.iloc[i, j]))
                  row.append(item)
              tick_box = QStandardItem()
              tick_box.setCheckable(True)
              row.append(tick_box)
              model.appendRow(row)
      
          # Set the model for the table view
          table_view.setModel(model)
          table_view.setEditTriggers(QTableView.NoEditTriggers)
          table_view.setSelectionBehavior(QTableView.SelectRows)
          table_view.setSelectionMode(QTableView.SingleSelection)
          table_view.verticalHeader().setVisible(False)
          table_view.setHorizontalScrollMode(QTableView.ScrollPerPixel)
          table_view.setVerticalScrollMode(QTableView.ScrollPerPixel)
          table_view.setShowGrid(False)
          table_view.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
      
          # Set the resize mode for the last column
      
          table_view.horizontalHeader().setSectionResizeMode(
              dpTest.shape[1], QHeaderView.ResizeToContents)
      
          # adjust the size to your preference
          table_view.setColumnWidth(dpTest.shape[1], 30)
          table_view.move(550, 150)
          table_view.setFixedSize(500, 250)
      
      J Offline
      J Offline
      JonB
      wrote on 13 Jul 2023, 09:28 last edited by
      #2

      @MIckH said in Can't set the model data in tableview:

      QStandardItemModel

      In the one which is "broken" do you have line from PySide6.QtGui import QStandardItemModel or equivalent at the top of the file?

      M 1 Reply Last reply 13 Jul 2023, 10:14
      0
      • J JonB
        13 Jul 2023, 09:28

        @MIckH said in Can't set the model data in tableview:

        QStandardItemModel

        In the one which is "broken" do you have line from PySide6.QtGui import QStandardItemModel or equivalent at the top of the file?

        M Offline
        M Offline
        MIckH
        wrote on 13 Jul 2023, 10:14 last edited by
        #3

        @JonB
        this is the top of the file:

        import random
        import sys

        from PyQt6.QtCore import Qt, QItemSelectionModel

        from PyQt6.QtWidgets import QApplication, QMainWindow

        from PyQt6.QtCore import Qt, QItemSelectionModel
        from PyQt6.QtGui import QStandardItemModel, QFont, QBrush
        from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTableView, QHeaderView, QPushButton, QSizePolicy, QSpacerItem
        from PyQt5.QtGui import QStandardItem
        from PyQt5.QtGui import *

        import pandas as pd

        from test import Ui_MainWindow

        dpTest = pd.read_csv('TableViewTest.csv')

        J 2 Replies Last reply 13 Jul 2023, 11:12
        0
        • M MIckH
          13 Jul 2023, 10:14

          @JonB
          this is the top of the file:

          import random
          import sys

          from PyQt6.QtCore import Qt, QItemSelectionModel

          from PyQt6.QtWidgets import QApplication, QMainWindow

          from PyQt6.QtCore import Qt, QItemSelectionModel
          from PyQt6.QtGui import QStandardItemModel, QFont, QBrush
          from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTableView, QHeaderView, QPushButton, QSizePolicy, QSpacerItem
          from PyQt5.QtGui import QStandardItem
          from PyQt5.QtGui import *

          import pandas as pd

          from test import Ui_MainWindow

          dpTest = pd.read_csv('TableViewTest.csv')

          J Offline
          J Offline
          JonB
          wrote on 13 Jul 2023, 11:12 last edited by
          #4

          @MIckH
          Please use the forum's Code tags (</> icon) on all code you paste.

          Then I cannot spot what is wrong. Reduce all of the code in "broken" case, you don't need any more than e.g.

          self.model = QStandardItemModel()
          self.tableView.setModel(self.model)
          

          to test.

          1 Reply Last reply
          1
          • J JonB referenced this topic on 15 Jul 2023, 06:51
          • M MIckH
            13 Jul 2023, 10:14

            @JonB
            this is the top of the file:

            import random
            import sys

            from PyQt6.QtCore import Qt, QItemSelectionModel

            from PyQt6.QtWidgets import QApplication, QMainWindow

            from PyQt6.QtCore import Qt, QItemSelectionModel
            from PyQt6.QtGui import QStandardItemModel, QFont, QBrush
            from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QTableView, QHeaderView, QPushButton, QSizePolicy, QSpacerItem
            from PyQt5.QtGui import QStandardItem
            from PyQt5.QtGui import *

            import pandas as pd

            from test import Ui_MainWindow

            dpTest = pd.read_csv('TableViewTest.csv')

            J Offline
            J Offline
            JonB
            wrote on 15 Jul 2023, 07:25 last edited by JonB
            #5

            @MIckH
            Now I can indeed spot what is wrong in your code! See your duplicate thread and my answer in https://forum.qt.io/topic/146638/a-problem-with-displaying-data-in-a-tableview/5. Please do not continue this thread as well as that one.....

            1 Reply Last reply
            2

            1/5

            13 Jul 2023, 08:10

            • Login

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