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 populate a QML ListView from a C++ program
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 2.9k 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
    skylendar
    wrote on last edited by skylendar
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mammamia
        wrote on last edited by
        #3

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

        1 Reply Last reply
        1

        • Login

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