Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access roles without naming it in QAbstractListModel. How?
Qt 6.11 is out! See what's new in the release blog

Access roles without naming it in QAbstractListModel. How?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 342 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by bogong
    #1

    Hello all!
    Is there any way to access roles in QAbstractListModel without naming it? I mean something like access to array[index]:

    public:
        enum Roles {
            Role0 = Qt::UserRole + 1,
            Role1,
            Role2,
            Role3,
            ...
        };
    
        ...
    
       QHash<int, QByteArray> roleNames() const {
    
           QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
           roles[Role0] = "Role0";
           roles[Role1] = "Role1";
           roles[Role2] = "Role2";
           roles[Role3] = "Role3";
           ...
    
           return roles;
       }
    

    In this example defined exact roles in model. I want to create universal model where I can access to it by something like position-index in array. Is there any way to make it possible? Or at least update roleNames dynamically via variable or parameter in class. Something like example below.

    *.h file:

    public:
    	
    	explicit ATableViewModel(QObject *parent = nullptr);
    	virtual ~ATableViewModel(void);
    		
    	QHash<int, QByteArray> roleNames() const;
    		
    private:
    	
    	QHash<int, QByteArray> pRoleNames = {};
    

    *.cpp

    QHash<int, QByteArray> ATableViewModel::roleNames() const {
    	
    	return pRoleNames;
    }
    
    1 Reply Last reply
    0
    • AxelViennaA Offline
      AxelViennaA Offline
      AxelVienna
      wrote on last edited by
      #2

      You can use a 1-dimensional role and fill it with a JSON object, document or array which gives you even more flexibility than a single array.
      https://doc.qt.io/qt-5/qvariant.html#toJsonObject

      C++ and Python walk into a bar. C++ reuses the first glass.

      B 1 Reply Last reply
      1
      • AxelViennaA AxelVienna

        You can use a 1-dimensional role and fill it with a JSON object, document or array which gives you even more flexibility than a single array.
        https://doc.qt.io/qt-5/qvariant.html#toJsonObject

        B Offline
        B Offline
        bogong
        wrote on last edited by bogong
        #3

        @AxelVienna said in Access roles without naming it in QAbstractListModel. How?:

        https://doc.qt.io/qt-5/qvariant.html#toJsonObject

        Thx. Issue closed. Example published.

        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