A problem with displaying data in a tableView
-
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 :)
-
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 :)
@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
, theQStandardItemModel
and thesetModel()
. Now in your current "test2" there is noQStandardItemModel
and nosetModel()
, so it says nothing about your situation.UPDATE
OMG, HANG ON!!!! In your code for both cases your imports have a mixture offrom PyQt6 import ...
andfrom 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!
-
J JonB referenced this topic on
-
@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
, theQStandardItemModel
and thesetModel()
. Now in your current "test2" there is noQStandardItemModel
and nosetModel()
, so it says nothing about your situation.UPDATE
OMG, HANG ON!!!! In your code for both cases your imports have a mixture offrom PyQt6 import ...
andfrom 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!
-
@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