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. How to access C++ model from QML ListView
Forum Updated to NodeBB v4.3 + New Features

How to access C++ model from QML ListView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 1.3k 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.
  • S Offline
    S Offline
    sly110
    wrote on last edited by
    #1

    I am using the drawer control to navigate between various qml pages displayed in a stackview. I have a model in C++ exposed to QML as a QListProperty. The C++ class containing this property is instantiated in main.cpp and set as a context property. My drawer has delegate Buttons each associated with a Key class in the QList. Each button should display the 'title' QString in each Key object. Error in QML debugger is qrc:/main.qml:191:23: Unable to assign [undefined] to QString qrc:/main.qml: 191

    How do I set the text property in the ListView / delegate?

    Also when the drawer button is clicked a slot in the C++ should be called with the Key source QString. How will this be defined in QML.

    Thank you for any help.

       //nav drawer
        Drawer {
            id: drawer
            width: Math.min(window.width, window.height) / 3 * 2
            height: window.height
            background: Rectangle {  //add fill to drawer
                anchors.fill: parent
                color: "white"
            }
            ListView {
                id: listView
                currentIndex: -1
                anchors.fill: parent
                model: QML_MAIN.KeyModel     //c++ model
    
                delegate: Button {
                    id: buttonDelegate
                    width: parent.width - 2
                    height: window.height / 6
                    text: listView.model.title
    
                    contentItem: Text {
                        text: buttonDelegate.text
                        font: buttonDelegate.font
                        opacity: 1.0
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        elide: Text.ElideRight
                    }
                    background: Rectangle {
                        color: "blue"
                        border.width: 2
                        border.color: "white"
                    }
                    highlighted: ListView.isCurrentItem
                    onClicked: {
                            console.log("Drawer Clicked")
    //                       QML_MAIN.cppNavSlot(QML_MAIN.model.target)
    
                    }
                } //button delegate
                ScrollIndicator.vertical: ScrollIndicator { }
            } //listview
        } //nav drawer
    
    //------------------------------------------------------------------------------
    // class QmlMain
    //------------------------------------------------------------------------------
    class QmlMain : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY  (QString Id READ getId NOTIFY idChanged)
        Q_PROPERTY  (QString Title READ getTitle NOTIFY titleChanged)
        Q_PROPERTY  (QString Source READ getSource NOTIFY sourceChanged)
    //SLY TODO:
    //    Q_PROPERTY(QString icon READ icon WRITE setIcon NOTIFY iconChanged)
    //    Q_PROPERTY(QString target READ target WRITE setTarget NOTIFY targetChanged)
    
        Q_PROPERTY  (QUrl Help READ gethelpUrl NOTIFY helpUrlChanged)
        Q_PROPERTY  (QString StatusText READ getStatusText WRITE setStatusText NOTIFY statusTextChanged)
    
        Q_PROPERTY  (QQmlListProperty<Key> KeyModel READ readKeyModel NOTIFY keyModelChanged)
    ...
      QList<Key *> _keyModel;
    
    
    class Key : public QObject
    {
        Q_OBJECT
    
    
    public:
    
        Key(QObject *parent = 0);
        Key(const QString &id, const QString &type, const QString &title, const QString &icon, const QString &target, QObject *parent = 0);
    
    //    Key(const Key &key, QObject *parent = 0);     //my public copy constructor
    //    Key &operator=( const Key& key );
    
        ~Key();
    
        QString id;
        QString type;
        QString title;
        QString icon;
        QString target;
    
    
    };
    
    
    class Nav : public QObject
    {
        Q_OBJECT
    
    
    public:
        Nav(QObject *parent = 0);
        Nav(const QString &pid, const QString &ptitle, QObject *parent = 0);
        ~Nav();
    
    //    Nav(const Nav &nav, QObject *parent = 0);    //my public copy constructor
    //    Nav &operator=( const Nav& nav );
    
    
    
        QString id;
        QString title;
        QString source;
        QList <Key *> keys;
        QList <int> milestones;
    };
    
    
    #endif // KEYDATA_H
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      sly110
      wrote on last edited by
      #2

      Apparently the included class must have Q_PROPERTY defined for items intended to be bound to QML.
      At least it is working now. I will mark as solved in case this helps someone else.

      1 Reply Last reply
      0

      • Login

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