Using c++ created objects in qml



  • Hello,

    i want to use a objectslist from c++ in qml as model, but i´m not really understand how can i imagine it.
    Here my code:

    
    class DataObject : public QObject
    {
        Q_OBJECT
    
    
        Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
        Q_PROPERTY(QString detailText READ detailText WRITE setDetailText NOTIFY detailTextChanged)
        Q_PROPERTY(bool me READ me WRITE setMe NOTIFY meChanged)
    public:
        DataObject(QApplication *parent=0);
        DataObject(const qint16 &pid, const QString &text, const QString &detailText, QApplication *parent=0);
    
        DataObject(const QString &text, const bool &me, QApplication *parent=0);
    
    
    
        QString text() const;
        void setText(const QString &name);
    
         bool me() const;
        void setMe(const bool &own);
    
        QString detailText() const;
        void setDetailText(const QString &color);
    
    
    
    signals:
    
        void textChanged();
        void detailTextChanged();
        void meChanged();
    
    private:
        Kontaktmenu *contmenu ;
        Chatfenster *chatwindow;
        QQmlApplicationEngine *app;
        bool m_me;
        QString m_text;
        QString m_detailText;
        qint16 m_partnerid;
    
    };
    
    
    class Datas : public QObject
    {
       Q_OBJECT
    
        Q_PROPERTY(QQmlListProperty<DataObject> datas READ datas)
        Q_PROPERTY(int init READ init WRITE setInit)
    
        Q_PROPERTY(qint16 partnerid READ partnerid WRITE setPartnerid)
    
    
    public:
        Datas();
        QQmlListProperty<DataObject> datas();
        int init();
        void setInit(int in);
    
        qint16 partnerid() const;
        void setPartnerid(const qint16 &pid);
    
    private:
        QList<DataObject *> m_data;
    
        int m_init;
        Kontaktmenu *contmenu ;
        Chatfenster *chatwindow;
        qint16 m_partnerid;
    
    
    };
    
    #endif
    
    

    The Code from qml:

    import QtQuick 2.4
    import VPlayApps 1.0
    import DataObject 1.0
    
    ListPage {
              title: qsTr("Recent")
    
              emptyText.text: qsTr("No recent messages")
    
              // Use a predefined delegate but change some of its layout parameters
              delegate: SimpleRow {
                image.radius: image.height
                image.fillMode: Image.PreserveAspectCrop
                autoSizeImage: true
                style.showDisclosure: false
                imageMaxSize: dp(48)
                detailTextItem.maximumLineCount: 1
                detailTextItem.elide: Text.ElideRight
    
                onSelected: {
                  globalNavStack.popAllExceptFirstAndPush(detailPageComponent, {
                                                 person: item.text,
                                                 pid: item.partnerid,
                                                 newMsgs: [{me: true, text: item.detailText}]
                                               })
                }
              }
    
              Component { id: detailPageComponent;  ConversationPage { } }
              property var textModel
    
    

    Now i want to set in the object Datas a init value and use the constructed values in a model:

         Datas{
                  init: 1
    
    
              }
    
    
               model: [{text:   Datas.datas.text   ,  detailText:   Datas.datas.detailText     }
                    ]
    
    
              // Model should be loaded from your messaging backend
            //  model: [
            //    { text: "Tom McEloy", detailText: "Sorry for the late reply ...", image: Qt.resolvedUrl("../../assets/portrait0.jpg") },
            //    { text: "Leah Douglas", detailText: "Hahaha :D", image: Qt.resolvedUrl("../../assets/portrait1.jpg") }
            //  ]
            }
    
    
    
    
    
    
    


  • What is it that doesn't work? Any error messages?
    Perhaps you might want to have a look at Thomas Boutroues qqmlobjectlistmodel.h.