QList<CustomClass> as Q_PROPERTY



  • Hi all,

    I know this topic has already been discussed in the past but I can't find a clear, final and working solution to it.
    I have a class named ChildClass that basically contains a QString msg (full source code at the end of the msg)

    class ChildClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString msg READ msg WRITE setMsg NOTIFY msgChanged)
    …
    }
    

    I have a second class containing 4 Q_PROP members. Note : each of this member has the same use, they just illustrate different ways to do the same thing : displaying a message from c++ in qml

    class ParentClass : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString msg READ msg WRITE setMsg NOTIFY msgChanged)
        Q_PROPERTY(ChildClass* child READ child WRITE setChild NOTIFY childChanged)
        Q_PROPERTY(QList<QString> listMsg READ listMsg WRITE setListMsg NOTIFY listMsgChanged)
        Q_PROPERTY(QList<ChildClass*> listChild READ listChild WRITE setListChild NOTIFY listChildChanged)
    
        QString m_msg;
        ChildClass* m_child;
        QList<QString> m_listMsg;
        QList<ChildClass*> m_listChild;
    …
    }
    

    The ParentClass constructor initialises its member as done below :

    ParentClass::ParentClass(QObject *parent) : QObject(parent)
    {
        setMsg("Hello World 1");
    
        m_child = new ChildClass(this);
        m_child->setMsg("Hello World 2");
    
        m_listMsg.append("Hello World 3");
    
        ChildClass* child2 = new ChildClass(this);
        child2->setMsg("Hello World 4");
        m_listChild.append(child2);
    }
    

    in my main.cpp, I instantiant a ParentClassObject and inject it in qml :

        ParentClass *myParent = new ParentClass();
        context->setContextProperty("_myParent", myParent);
    

    Finally, in my qml, I display 4 labels and for each, I have a different binding :

                text: _myParent.msg
                text: _myParent.child.msg
                text: _myParent.listMsg[0]
                text: _myParent.listChild[0].msg
    
    

    The 3 first binding work fine, the last one (_myParent.listChild[0].msg ) does not work and gives the error : "TypeError: Cannot read property 'msg' of undefined"

    Do you know why ?
    Is it possible to have something working with QList ?
    What should I do to have this working that way ?

    Thanks in advance,

    Antoine


  • Moderators

    @JoBill

    1. you also need to make sure to call qmlRegisterType() for your ChildClass class (in fact for every custom object type you want to access from QML)
    2. you should only use QList properties for basic types if possible. For QObject derived types you should use QQmlListProperty


  • I don't do that because actually, my ChildClass is not declared itself in the qml.
    And in any cases, I don't do it for the second binding : "_myParent.child.msg" which works fine

    anyway, I tried it (+ the corresponding import in the qml file) but it did not change anything and my 3 first binding are working fine but not the last one.

    And concerning QQmlListProperty, I don't understand how to use it in my specific case.
    I mean if I just replace QList by QQmlListProperty, it does not compile because I don't implement the append method. And I don't knwo what to do with that.
    Moreover : in my example, the Childclass only have one member, but in real life, it will have several members and I would like to access to all of them from the QML.
    And to finish, it will not be used with a tableView, so I don't see either how to use QAbstractItemModel.
    If you have any idea ....
    Antoine


  • Moderators

    @JoBill said in QList<CustomClass> as Q_PROPERTY:

    And concerning QQmlListProperty, I don't understand how to use it in my specific case.
    I mean if I just replace QList by QQmlListProperty, it does not compile because I don't implement the append method. And I don't knwo what to do with that.

    the link i've posted is a working example - using a QList internally, so not much to change for you.
    Take a closer look at the example, you missed something for sure. This QQmlListProperty<Person> guests(); is the most interesting part.



  • @raven-worx
    Thanks a lot, it works


Log in to reply
 

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