Solved QSortFilterProxyModel(AttributeError: 'X' object has no attribute 'Y')
-
Hi, my objective was to make a treeview with data on it and to be able to sort the data:
So far I managed to:
1-connect to a DataBase
2-make a model using the database
3-dropping the model inside a warper so it can be sorted (QSortFilterProxyModel
)
4-then I filtered the model using a regular expression....and here is the problem, i get an error:AttributeError: 'PySide2.QtCore.QSortFilterProxyModel' object has no attribute 'setFilterRegExpression'
This is the line of code that gives the error:
self.ui.model_sorted.setFilterRegExpression(QRegularExpression("(red|blue)"))
I tried stuff but so far i didn't get the solution, i think a
self
is missing somewhere or that it has something to do with private and public methods/classes, but i'm not sure.---------------------------------------------------------------
the code(i dont think its necessary to read, but just in case):
def Starting_DataBase(self): #CONNECTING TO THE DATABASE: self.ui.db = QSqlDatabase.addDatabase('QSQLITE') self.ui.db.setDatabaseName('The directory\DataBase.db') self.ui.db.open() #MAKING THE MODEL USING THE DATABASE: self.ui.model = QSqlTableModel() self.ui.model.setTable('Table_Name') self.ui.model.setEditStrategy(QSqlTableModel.OnManualSubmit) self.ui.model.select() #PUTTING THE MODEL INSIDE A WARPER THAT CAN SORT AND FILTER IT: self.ui.model_sorted = QSortFilterProxyModel() self.ui.model_sorted.setSourceModel(self.ui.model) self.ui.model_sorted.setFilterKeyColumn(1) #ESTE NO SE QUE HACE self.ui.model_sorted.setFilterRegExpression(QRegularExpression("(red|blue)")) #MAKING THE RELATIONSHIP BETWEEN THE TABLEVIEW AND THE MODEL_SORTED: self.ui.demosTree.setModel(self.ui.model_sorted)
-
@adrian88888888 said in QSortFilterProxyModel(AttributeError: 'X' object has no attribute 'Y'):
setFilterRegExpression
As per docs, it's
setFilterRegularExpression()
. -
@adrian88888888 said in QSortFilterProxyModel(AttributeError: 'X' object has no attribute 'Y'):
setFilterRegExpression
As per docs, it's
setFilterRegularExpression()
. -
How did you noticed?, those things escape from me, thanks!!!!!!
-
@adrian88888888
I'm afraid that getting the exact right spelling of functions is a requirement for writing code :)Now you know that in Python a message like
AttributeError: 'PySide2.QtCore.QSortFilterProxyModel' object has no attribute 'setFilterRegExpression'
is what you get for a method of a class does not exist. It will refer to a method (and, I think, a variable) as an "attribute" of the class, because that's how Python works.
-
@JonB oh, i see, I have a new mental note now, newbie problems