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



  • 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.


Log in to reply
 

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