Important: Please read the Qt Code of Conduct -

How to access C++ model from QML ListView

  • 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(
                } //button delegate
                ScrollIndicator.vertical: ScrollIndicator { }
            } //listview
        } //nav drawer
    // class QmlMain
    class QmlMain : public QObject
        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
        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 );
        QString id;
        QString type;
        QString title;
        QString icon;
        QString target;
    class Nav : public QObject
        Nav(QObject *parent = 0);
        Nav(const QString &pid, const QString &ptitle, QObject *parent = 0);
    //    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

  • 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.

Log in to reply