Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using c++ created objects in qml
Forum Update on Monday, May 27th 2025

Using c++ created objects in qml

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlc++ and qml codqml binding
2 Posts 2 Posters 767 Views
  • 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.
  • A Offline
    A Offline
    Allerknappe
    wrote on 21 May 2018, 15:24 last edited by Allerknappe
    #1

    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") }
            //  ]
            }
    
    
    
    
    
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SeDi
      wrote on 25 May 2018, 14:09 last edited by
      #2

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

      1 Reply Last reply
      0

      2/2

      25 May 2018, 14:09

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved