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. Custom C++ ListModel needing explicit role names to be accessible from QML?
Forum Updated to NodeBB v4.3 + New Features

Custom C++ ListModel needing explicit role names to be accessible from QML?

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 783 Views 1 Watching
  • 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.
  • K Offline
    K Offline
    Korchkidu
    wrote on last edited by
    #1

    Hi,

    in the doc (for QML), we can read that:

    bq. Models that do not have named roles (such as the ListModel shown below) will have the data provided via the modelData role. The modelData role is also provided for models that have only one role. In this case the modelData role contains the same data as the named role.

    Also, according to the documentation, I should at least reimplement those 3 methods:
    @ int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index,
    int role) const;
    QVariant headerData(int section,
    Qt::Orientation orientation,
    int role = Qt::DisplayRole) const;@

    What I did. When I register my custom model type into QML and try to use it, I cannot access modelData property. I get:
    @Unable to assign [undefined] to QString@

    However, reimplementing roleNames() as:
    @QHash<int, QByteArray> myClass::roleNames() const
    {
    QHash<int, QByteArray> roles;
    roles[0] = "nodeName";
    return roles;
    }@

    Then it works and I can access my model data either as "nodeName" or "modelData". Could someone clear this confusion for me please?

    Best regards.

    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