How to populate a QML ListView from a C++ program



  • First, thx for reading and possibly answering this post.

    I'm thriving to populate a QML ListView from a C++ program. The
    ListView uses a ListModel described in the QML file.

    After hours of efforts, I got nada. I need help.

    QML

    ListView {
    id: listView
    delegate: Item {
    id: item1
    x: 2
    width: parent.width
    height: 22
    Row {
    id: row1
    x: 0
    y: 0
    spacing: 10
    }
    Text {
    id: txt
    x: 0
    y: 1
    width: parent.width
    height: 21
    color: "#fdfdfd"
    text: caption
    font.pixelSize: 12
    }
    Rectangle {
    id: rectangle
    x: txt.x
    y: txt.y
    width: txt.width
    height: txt.height
    color: colorCode
    z: -1
    }
    }
    model: ListModel {
    id: mymodel
    ListElement {
    caption: "red"
    colorCode: "red"
    }

    C++

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    

    ...

    Thx again if you answer this post.


  • Lifetime Qt Champion

    Hi,

    You should take a look at the Using C++ Models with Qt Quick Views chapter of Qt's documentation.



  • You can have a look at this Blog post
    and here is the Source code


Log in to reply
 

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