Access QAbstractListModel roles from QML
-
Hi !
I am working on a software that can manage multiple "projects" at the same time (displayed as different tabs in the UI).
Each project is stored in a
QAbstractListModelthat is available from the QML. I have a tab bar that displays all the available projects and I need a page that contains the currently active project (the others arevisible: false)I thought about using a
ListViewthat displays only the currently selected project but it didn't worked. Now, I am trying to create a custom component that acts likeListViewbut displays only the currently active project.I was thinking about dynamically creating my pages in an
Itembased on theQAbstractListModelcontent and update it when needed. Then, simply hiding the pages of the projects not selected.But in order to mimic the
ListViewbehavior, I need to give each delegate (page) access to the roles defined by myQAbstractListModel(I have "color" and "size").In the end, I would have liked to have something like that :
PageView { model: myProjectsModel delegate: Text { text: 'My color is ' + color + ' and my size is ' + size } }My model already contains the project that is active, the only missing step is that I do not understand how I can give access to the model's roles to each delegate
If you have any idea that could help me (or if you see a simpler solution to my problem) that would be greatly appreciated
Thanks -
It's really confusing what a "project" is. if project=QAbstractListModel subclass then to switch from one project to the next you just need to change the
modelproperty of theListViewI need to give each delegate (page) access to the roles defined by my QAbstractListModel (I have "color" and "size").
Can you show how you define those role names?
-
A "project" is an item in my
QAbstractListModelIt looks like that :
class ProjectsModelList : public QAbstractListModel { Q_OBJECT Q_PROPERTY(int activeIndex READ activeIndex WRITE setActiveIndex NOTIFY activeIndexChanged) QList<Project*> _projects; int _activeIndex; public: enum class RolesTypes : int { COLOR = Qt::UserRole + 1, SIZE }; ProjectsModelList(QObject *parent = nullptr) { /* ... */ } int rowCount(QModelIndex const& parent = QModelIndex()) const override { /* ... */ } QVariant data(QModelIndex const& index, int role = Qt::UserRole) const override { /* ... */ } bool setData(QModelIndex const& index, QVariant const& value, int role = Qt::UserRole) override { /* ... */ } QHash<int, QByteArray> roleNames() const override { QHash<int, QByteArray> roles; roles[RolesNames::COLOR] = "color"; roles[RolesNames::SIZE] = "size"; return roles; } bool insertRows(int row, int count, QModelIndex const& parent) override { /* ... */ } bool removeRows(int row, int count, QModelIndex const& parent) override { /* ... */ } int activeIndex() const noexcept { /* ... */ } void setActiveIndex(int index) noexcept { /* ... */ } /** ... **/ };Then, I create an instance of this class that I called
myProjectsModeland given as a property of my QML context.What I try to explain (and I'm sorry if it's not clear) is that there can be multiple
Projectat the same time in the application (one by tab). But only one is visible at a time. MyPageViewwould be here to instantiate a delegate for eachProjectin myQAbstractListModelbut only display the one which index is in the propertyactiveIndex.Hope it's clearer this time