Can't set the model data in tableview
-
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)
-
@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')
-
@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.
-
J JonB referenced this topic on
-