Access QTableView method from child class
-
I'm using Pyside2, python 3.8.
For design, and other project specific purposes, I'm writing a class that does inherits from my MainWindow class, the class gets initialized with a excel file, I'm aiming with this class to parse my excel file and add data to MyTableView (which is connected to a proxy model).
I'm trying to access the MyTableView methods from the child class (AddFromFile), which seems to work, but in MyTableView, there's no rows added.
Below is my (minimal version) code. Hope it's clear enough.
Thanks,
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): ProxyModel = ProxyModel() TableModel = TableModel() ProxyModel.setSourceModel(TableModel) MyTableView.setModel(ProxyModel) class AddFromFile(MainWindow): def __init__(self,File): super().__init__() self.MyTableView.model().sourceModel().addRow(['test data', 'column 1','column 2','column 3','column 4' ]) class TableModel(QtCore.QAbstractTableModel): def __init__(self, mlist=None): super(TableModel, self).__init__() self._items = [] if mlist == None else mlist self._header = [] def addRow(self, rowObject): row = self.rowCount() self.beginInsertRows(QtCore.QModelIndex(), row, row) self._items.append(rowObject) self.endInsertRows() self.layoutChanged.emit() -
I'm using Pyside2, python 3.8.
For design, and other project specific purposes, I'm writing a class that does inherits from my MainWindow class, the class gets initialized with a excel file, I'm aiming with this class to parse my excel file and add data to MyTableView (which is connected to a proxy model).
I'm trying to access the MyTableView methods from the child class (AddFromFile), which seems to work, but in MyTableView, there's no rows added.
Below is my (minimal version) code. Hope it's clear enough.
Thanks,
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): ProxyModel = ProxyModel() TableModel = TableModel() ProxyModel.setSourceModel(TableModel) MyTableView.setModel(ProxyModel) class AddFromFile(MainWindow): def __init__(self,File): super().__init__() self.MyTableView.model().sourceModel().addRow(['test data', 'column 1','column 2','column 3','column 4' ]) class TableModel(QtCore.QAbstractTableModel): def __init__(self, mlist=None): super(TableModel, self).__init__() self._items = [] if mlist == None else mlist self._header = [] def addRow(self, rowObject): row = self.rowCount() self.beginInsertRows(QtCore.QModelIndex(), row, row) self._items.append(rowObject) self.endInsertRows() self.layoutChanged.emit()@hachbani
No idea, there's no information about what yourProxyModeldoes/does not do.You should put a
print()intodef addRow()to verify it's being called.mlist == Noneis not good in Python.Having a class named
AddFromFilewhich inherits fromMainWindow, and is responsible for something to do with having an Excel file. is not a good idea. -
Well, the
ProxyModelis irrelevant to the problem, this is why I didn't include it in the minimal code, all what it does is filtering (QSortFilterProxyModel subclass).I did add print() in def addRow(), it's indeed being called, I also called .rowCount() from the AddFromFIle class, to my surprise, it outputs 0, knowing that MyTableView is not empty.
I Think that calling MyTableView from child class, don't point to the same Table that is in the runnig MainWindow, instead, it goes look into a new Table in an I-don't-know-what MainWindow.
I've been trying to set up some sorts of signals/slots communication, no success..
I've implemented a function
def AddLine()in myMainWindowclass, and call it fromAddFromFileclass usingsuper().AddLine(), it's been called (verified with a print in the function definition), but no line is been added to MyTableView.Any idea how can I sort this out ?
-
Well, the
ProxyModelis irrelevant to the problem, this is why I didn't include it in the minimal code, all what it does is filtering (QSortFilterProxyModel subclass).I did add print() in def addRow(), it's indeed being called, I also called .rowCount() from the AddFromFIle class, to my surprise, it outputs 0, knowing that MyTableView is not empty.
I Think that calling MyTableView from child class, don't point to the same Table that is in the runnig MainWindow, instead, it goes look into a new Table in an I-don't-know-what MainWindow.
I've been trying to set up some sorts of signals/slots communication, no success..
I've implemented a function
def AddLine()in myMainWindowclass, and call it fromAddFromFileclass usingsuper().AddLine(), it's been called (verified with a print in the function definition), but no line is been added to MyTableView.Any idea how can I sort this out ?
@hachbani said in Access QTableView method from child class:
Well, the ProxyModel is irrelevant to the problem
It isn't :) The table view is connected to the proxy, not the underlying table, so the proxy does 100% matter!
I Think that calling MyTableView from child class, don't point to the same Table that is in the runnig MainWindow,
From the code as shown,
self.MyTableView.model().sourceModel()inAddFromFiledoes point to theMyTableView->ProxyModel->TableModelyou have inMainWindow.Wait, hang on a sec!
MyTableView.setModel(ProxyModel). This is no good!MyTableViewin that__init__()is just a local variable to the function Where is theself.MyTableViewthat you will need to use??self.MyTableViewinAddFomFileis not the same as theMyTableViewyou have inMainWindow. -
@hachbani said in Access QTableView method from child class:
Well, the ProxyModel is irrelevant to the problem
It isn't :) The table view is connected to the proxy, not the underlying table, so the proxy does 100% matter!
I Think that calling MyTableView from child class, don't point to the same Table that is in the runnig MainWindow,
From the code as shown,
self.MyTableView.model().sourceModel()inAddFromFiledoes point to theMyTableView->ProxyModel->TableModelyou have inMainWindow.Wait, hang on a sec!
MyTableView.setModel(ProxyModel). This is no good!MyTableViewin that__init__()is just a local variable to the function Where is theself.MyTableViewthat you will need to use??self.MyTableViewinAddFomFileis not the same as theMyTableViewyou have inMainWindow.Here's the ProxyModel class, I still don't see how it's relevant to connecting the AddFromFile class to MainWIndow TableView.
class ProxyModel(QtCore.QSortFilterProxyModel): def __init__(self,parent=None): super(ProxyModel, self).__init__() self._filter = "Aucun" def setFilterColumn(self, header): if header == "Aucun": self.Filtre("Aucun") return True for col in range(self.sourceModel().columnCount()): if self.sourceModel().headerData(col) == header: self.setFilterKeyColumn(col) return True return False def Filtre(self, valeur): self._filter = str(valeur) self.invalidateFilter() def filterAcceptsRow(self, sourceRow, sourceParent): if self._filter == "Aucun": return True sourceModel = self.sourceModel() id = sourceModel.index(sourceRow, self.filterKeyColumn(), sourceParent) if sourceModel.data(id) == self._filter: return True return FalseNow to the MYTableView part, I'm building my GUI interface using Qt Designer, then generate a .py code from the .ui file which I inherits in my MainWindow class, this is just me trying to reproduce a minimal code for the forum, it is indeed self.MyTableView.setModel(ProxyModel).
-
Here's the ProxyModel class, I still don't see how it's relevant to connecting the AddFromFile class to MainWIndow TableView.
class ProxyModel(QtCore.QSortFilterProxyModel): def __init__(self,parent=None): super(ProxyModel, self).__init__() self._filter = "Aucun" def setFilterColumn(self, header): if header == "Aucun": self.Filtre("Aucun") return True for col in range(self.sourceModel().columnCount()): if self.sourceModel().headerData(col) == header: self.setFilterKeyColumn(col) return True return False def Filtre(self, valeur): self._filter = str(valeur) self.invalidateFilter() def filterAcceptsRow(self, sourceRow, sourceParent): if self._filter == "Aucun": return True sourceModel = self.sourceModel() id = sourceModel.index(sourceRow, self.filterKeyColumn(), sourceParent) if sourceModel.data(id) == self._filter: return True return FalseNow to the MYTableView part, I'm building my GUI interface using Qt Designer, then generate a .py code from the .ui file which I inherits in my MainWindow class, this is just me trying to reproduce a minimal code for the forum, it is indeed self.MyTableView.setModel(ProxyModel).
@hachbani said in Access QTableView method from child class:
I still don't see how it's relevant
So you have said before. Yet, for example, if you did not have the
self.invalidateFilter()you show that might have been an explanation.just me trying to reproduce a minimal code for the forum, it is indeed self.MyTableView.setModel(ProxyModel).
Then it's not going to be possible to help you, if what you reproduce is not like your real code, as in this case. Best of luck finding someone who can help you from different code from your actual. I'll leave it to others.