When to use columns versus custom roles in models?
-
If I subclass
QAbstractListModel
, how does one decide when to use columns versus rows for list item attributes? I am only using custom views in Qml, usingrowNames
to register item attributes that can be accessed. In fact, this seems like the only way to access item attributes in Qml since there isn't a clean way to access a row item's column value, and even then you'd have to map them to names.But whenever overriding
Model.data()
orModel.setData()
I am always querying by role and wondering why I am not using columns.Just trying to get the basic MVC concepts straight here.
-
Hi,
In the context of QML, the roles are used to create a mapping between the names you'll use in QML and the columns you want to access in your C++ models.
See here
@SGaist OK, so in QML roles and columns are synonymous in terms of data values? I sort of reflexively ended up doing things like this to allow setting of values from Python/C++ as well as QML:
def setData(self, index, value, role=Qt.EditRole): if self.isColumn(index, self.DATETIME) or role == self.DateTimeRole:
That always seemed pretty hack-ish.
And I assume the above only applies for list models, since table models different use of columns? If so then it seems that there is a problem with the concept of column and role parity.
-
No they are not synonymous.
If you go through the example, you'll see that the names are associated with UserRoles.
These user roles are what you will use to retrieve the right value from your data structure.
If you take the SQL model example from the link I gave in my previous post, you'll see that they provide a mapping so that you can access the database data from the QML side based on the table column names.