Can't set the model data in tableview
-
wrote on 13 Jul 2023, 08:10 last edited by
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)
-
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)
wrote on 13 Jul 2023, 09:28 last edited by@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? -
@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?wrote on 13 Jul 2023, 10:14 last edited by@JonB
this is the top of the file:import random
import sysfrom 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')
-
@JonB
this is the top of the file:import random
import sysfrom 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')
wrote on 13 Jul 2023, 11:12 last edited by@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.
-
-
@JonB
this is the top of the file:import random
import sysfrom 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')
wrote on 15 Jul 2023, 07:25 last edited by JonB@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/5