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. Populating a listView in qml from c++?
Forum Update on Monday, May 27th 2025

Populating a listView in qml from c++?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 845 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.
  • K Offline
    K Offline
    koolbi
    wrote on 3 Feb 2014, 21:00 last edited by
    #1

    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++?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      turaz
      wrote on 4 Feb 2014, 07:06 last edited by
      #2

      You should use C++ model (doc.
      https://qt-project.org/doc/qt-5.1/qtquick/qtquick-modelviewsdata-cppmodels.html )

      1 Reply Last reply
      0

      1/2

      3 Feb 2014, 21:00

      • Login

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