Populating a listView in qml from c++?



  • I have a main.qml file that I want to be able to control what is in that list from c++
    I don't even know where to begin but I do have a static list with a few things in it in my qml code.
    My c++ looks like this
    @
    #include <QApplication>
    #include "qmlapplicationviewer.h"

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    QmlApplicationViewer viewer;
    viewer.addImportPath(QLatin1String("modules"));
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile&#40;QLatin1String("qml/Testing_List_Stuff/main.qml"&#41;);
    viewer.showExpanded();
    
    return app->exec&#40;&#41;;
    

    }
    @
    and my qml list stuff looks like this
    @import QtQuick 1.1

    Rectangle {
    width: 360
    height: 360

    ListModel {
        id: fruitModel
    
        ListElement {
            name: "Apple"
            cost: 2.45
        }
        ListElement {
            name: "Orange"
            cost: 3.25
        }
        ListElement {
            name: "Banana"
            cost: 1.95
        }
    }
    
    ListView {
        anchors.fill: parent
        model: fruitModel
        delegate: Row {
            Text { text: "Fruit: " + name }
            Text { text: "Cost: $" + cost }
        }
    }
    

    }
    @

    as you can see my c++ stuff only loads the qml and then my qml contains the static list.
    How can I make a list from c++?




Log in to reply
 

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