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. A problem with displaying data in a tableView
Forum Update on Monday, May 27th 2025

A problem with displaying data in a tableView

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 545 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'll repost my issue so its better understood and hope fully I can get a fix.
    In the following code, I am trying to display data in the tableView. The tableView was built in QT designer and the ui is stored in test2.py (I'll list that too). So I am trying to have all the gui widget declarations contained in the test2.py which is generated solely by QT designer.

    But Im getting the following error:
    TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'QStandardItemModel'

    This occuring on the 'self.tableView.setModel(model)' line

    (Note that I have had this code working when contained in file)
    Here is the main code, which is breaking:

    import random
    import sys
    
    from PyQt6.QtCore import Qt
    from PyQt5.QtGui import QStandardItemModel, QFont, QBrush, QStandardItem
    from PyQt6.QtWidgets import QApplication, QMainWindow, QStyleFactory
    import pandas as pd
    
    # from MainWindow import Ui_MainWindow
    from test2 import Ui_MainWindow
    
    dpTest = pd.read_csv('TableViewTest.csv')
    
    # class MainWindow(QMainWindow, Ui_MainWindow):
    
    
    class MainWindow(QMainWindow, Ui_MainWindow):
        def __init__(self):
            super().__init__()
    
            self.setupUi(self)
            self.show()
    
            self.model = QStandardItemModel()
    
            # Add the column headers to the model
            headers = list(dpTest.columns)
            headers.append("tick")
            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)
    
            self.tableView.setModel(self.model)
    
        def update_label(self):
            n = random.randint(1, 6)
            self.label.setText("%d" % n)
    
    
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    w = MainWindow()
    app.exec()
    

    and here is the code in test2:

    from PyQt6 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTableView, QHeaderView, QPushButton, QSizePolicy, QSpacerItem
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(1006, 619)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.tableView = QtWidgets.QTableView(self.centralwidget)
            self.tableView.setGeometry(QtCore.QRect(30, 30, 911, 192))
            self.tableView.setStyleSheet("td {\n"
                                         "                position: relative;\n"
                                         "                font-family: Arial;\n"
                                         "                font-size: 14px;\n"
                                         "            }\n"
                                         "            td:before {\n"
                                         "                content: \'\';\n"
                                         "                position: absolute;\n"
                                         "                top: 0;\n"
                                         "                bottom: 0;\n"
                                         "                left: -10px;\n"
                                         "                width: 10px;\n"
                                         "                background-color: #eee;\n"
                                         "            }")
            self.tableView.setObjectName("tableView")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec())
    

    Any help is most welcome. Its driving me nuts :)

    JonBJ 1 Reply Last reply
    0
    • M MIckH

      Hi,
      I'll repost my issue so its better understood and hope fully I can get a fix.
      In the following code, I am trying to display data in the tableView. The tableView was built in QT designer and the ui is stored in test2.py (I'll list that too). So I am trying to have all the gui widget declarations contained in the test2.py which is generated solely by QT designer.

      But Im getting the following error:
      TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'QStandardItemModel'

      This occuring on the 'self.tableView.setModel(model)' line

      (Note that I have had this code working when contained in file)
      Here is the main code, which is breaking:

      import random
      import sys
      
      from PyQt6.QtCore import Qt
      from PyQt5.QtGui import QStandardItemModel, QFont, QBrush, QStandardItem
      from PyQt6.QtWidgets import QApplication, QMainWindow, QStyleFactory
      import pandas as pd
      
      # from MainWindow import Ui_MainWindow
      from test2 import Ui_MainWindow
      
      dpTest = pd.read_csv('TableViewTest.csv')
      
      # class MainWindow(QMainWindow, Ui_MainWindow):
      
      
      class MainWindow(QMainWindow, Ui_MainWindow):
          def __init__(self):
              super().__init__()
      
              self.setupUi(self)
              self.show()
      
              self.model = QStandardItemModel()
      
              # Add the column headers to the model
              headers = list(dpTest.columns)
              headers.append("tick")
              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)
      
              self.tableView.setModel(self.model)
      
          def update_label(self):
              n = random.randint(1, 6)
              self.label.setText("%d" % n)
      
      
      app = QApplication(sys.argv)
      app.setStyle('Fusion')
      w = MainWindow()
      app.exec()
      

      and here is the code in test2:

      from PyQt6 import QtCore, QtGui, QtWidgets
      from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTableView, QHeaderView, QPushButton, QSizePolicy, QSpacerItem
      
      
      class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
              MainWindow.setObjectName("MainWindow")
              MainWindow.resize(1006, 619)
              self.centralwidget = QtWidgets.QWidget(MainWindow)
              self.centralwidget.setObjectName("centralwidget")
              self.tableView = QtWidgets.QTableView(self.centralwidget)
              self.tableView.setGeometry(QtCore.QRect(30, 30, 911, 192))
              self.tableView.setStyleSheet("td {\n"
                                           "                position: relative;\n"
                                           "                font-family: Arial;\n"
                                           "                font-size: 14px;\n"
                                           "            }\n"
                                           "            td:before {\n"
                                           "                content: \'\';\n"
                                           "                position: absolute;\n"
                                           "                top: 0;\n"
                                           "                bottom: 0;\n"
                                           "                left: -10px;\n"
                                           "                width: 10px;\n"
                                           "                background-color: #eee;\n"
                                           "            }")
              self.tableView.setObjectName("tableView")
              MainWindow.setCentralWidget(self.centralwidget)
              self.menubar = QtWidgets.QMenuBar(MainWindow)
              self.menubar.setGeometry(QtCore.QRect(0, 0, 1006, 21))
              self.menubar.setObjectName("menubar")
              MainWindow.setMenuBar(self.menubar)
              self.statusbar = QtWidgets.QStatusBar(MainWindow)
              self.statusbar.setObjectName("statusbar")
              MainWindow.setStatusBar(self.statusbar)
      
              self.retranslateUi(MainWindow)
              QtCore.QMetaObject.connectSlotsByName(MainWindow)
      
          def retranslateUi(self, MainWindow):
              _translate = QtCore.QCoreApplication.translate
              MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
      
      
      if __name__ == "__main__":
          import sys
          app = QtWidgets.QApplication(sys.argv)
          MainWindow = QtWidgets.QMainWindow()
          ui = Ui_MainWindow()
          ui.setupUi(MainWindow)
          MainWindow.show()
          sys.exit(app.exec())
      

      Any help is most welcome. Its driving me nuts :)

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

      @MIckH
      Please don't repost the same as your https://forum.qt.io/topic/146576/can-t-set-the-model-data-in-tableview. It doesn't help anyone to make responders duplicate posts.

      I already replied there and asked you to reduce it to

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

      to test. Did you do that? If that works then you can build back up towards your code. If that does not then you have a problem, and you can remove all the rest of the irrelevant code till resolved. If you say in the other post that it works in one case and errors in the other, please reduce to about 10 lines for each and show so we can figure what the difference in the situation is.

      Meanwhile, in your other post at least both working and non-working showed the creation of the QTableView, the QStandardItemModel and the setModel(). Now in your current "test2" there is no QStandardItemModel and no setModel(), so it says nothing about your situation.

      UPDATE
      OMG, HANG ON!!!! In your code for both cases your imports have a mixture of from PyQt6 import ... and from PyQt5 import .... How could you write that, and not notice? No wonder you get errors! :)

      If you are going to stick with PyQt6 you might like to uninstall PyQt5 to stop this happening again!

      M 1 Reply Last reply
      2
      • JonBJ JonB referenced this topic on
      • JonBJ JonB

        @MIckH
        Please don't repost the same as your https://forum.qt.io/topic/146576/can-t-set-the-model-data-in-tableview. It doesn't help anyone to make responders duplicate posts.

        I already replied there and asked you to reduce it to

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

        to test. Did you do that? If that works then you can build back up towards your code. If that does not then you have a problem, and you can remove all the rest of the irrelevant code till resolved. If you say in the other post that it works in one case and errors in the other, please reduce to about 10 lines for each and show so we can figure what the difference in the situation is.

        Meanwhile, in your other post at least both working and non-working showed the creation of the QTableView, the QStandardItemModel and the setModel(). Now in your current "test2" there is no QStandardItemModel and no setModel(), so it says nothing about your situation.

        UPDATE
        OMG, HANG ON!!!! In your code for both cases your imports have a mixture of from PyQt6 import ... and from PyQt5 import .... How could you write that, and not notice? No wonder you get errors! :)

        If you are going to stick with PyQt6 you might like to uninstall PyQt5 to stop this happening again!

        M Offline
        M Offline
        MIckH
        wrote on last edited by
        #3

        @JonB yes thanks saw that myself. That's the problem with changing from 5 to 6. Got caught by it. I was able to make some strong strives forward using chatgbpt. Thanks for your help but it wasn't a OMG moment

        JonBJ 1 Reply Last reply
        0
        • M MIckH

          @JonB yes thanks saw that myself. That's the problem with changing from 5 to 6. Got caught by it. I was able to make some strong strives forward using chatgbpt. Thanks for your help but it wasn't a OMG moment

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

          @MIckH It was only an "OMG" moment for me because I had not spotted the PyQt5/PyQt6 mixture earlier :)

          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