Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Access QAbstractListModel roles from QML

Access QAbstractListModel roles from QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ebatsin
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ebatsin
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved