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 QAbstractListModel that 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 are visible: false)

    I thought about using a ListView that displays only the currently selected project but it didn't worked. Now, I am trying to create a custom component that acts like ListView but displays only the currently active project.

    I was thinking about dynamically creating my pages in an Item based on the QAbstractListModel content and update it when needed. Then, simply hiding the pages of the projects not selected.

    But in order to mimic the ListView behavior, I need to give each delegate (page) access to the roles defined by my QAbstractListModel (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 model property of the ListView

    I 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 QAbstractListModel

    It 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 myProjectsModel and 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 Project at the same time in the application (one by tab). But only one is visible at a time. My PageView would be here to instantiate a delegate for each Project in my QAbstractListModel but only display the one which index is in the property activeIndex.

    Hope it's clearer this time


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.