0

I am having problems exposing my C++ data model to a ListView in QML. QML can't find the properties of each row.

I have this class (QT 5.7):

class Identity : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged);
    Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged);
    ...
}

And I have a model class that stores Identity objects:

class Identities: public QAbstractItemModel {
    Q_OBJECT

private:
    QMap<QString,Identity*>          identities_map;
    ...
}

I'm also registering it in main.cpp:

qmlRegisterType<Identities>("project_identities",1,0,"Identities");

My main.qml has these definitions:

Identities {
    id: identidades
}
ListView {
    anchors.fill: parent
    model: identidades
    delegate: Row {
        Text { text: "Email: " + model.email}
    }
}

Now the problem is, when I display the list of emails and passwords, this is the output:

Email: undefined

What I am missing ? When I debug my code with the debugger, the only method that is being executed is Identities::rowCount(). The Identities::index() is not executed, nor Identities::data().

Where could the problem be?

2 Answers 2

1

Providing the roles in the model are properly implemented, all you need to do is:

Text { text: "Email: " + email}.

If that doesn't work, then definitely you have a problem with your model implementation.

As GrecKo noted in the comments, the format model.role is actually possible, but not really necessary unless you have name conflicts.

Sign up to request clarification or add additional context in comments.

13 Comments

if I do it like you say, I get " ReferenceError: email is not defined" error. I have tried it already
Emphasis on the "providing the roles in the model are properly implemented" part.
I think the problem is with the delegate. The delegate is not calling data() nor index() method , so nothing is ever executed at the model class. I have breakpoints everywhere in it but there is no calls
There is no particular requirement for the delegate, if the model works, the delegate will work, it is all automatic. The problem is most likely with your model.
"That's not how you use roles, the item role is not a property of the list object, you don't do model.role you just do role." That's not true at all. You can use model.role in a delegate when role is ambiguous (and I do believe it's a better convention) : doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#models
|
0

Your model is either not mapping the role name correctly to the role value, i.e. missing or wrong implementation of QAbstractItemModel::roleNames() or your data() method is not correctly processing the role values.

Since your data object's seem to be QObjects you could have a single model role that returns the object of a given index and access that object's properties instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.