Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Populate a QML ListView with a custom C++ model
Qt 6.12 LTS is out! See what's new in the release blog

[SOLVED] Populate a QML ListView with a custom C++ model

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.9k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I have a custom Model representing a tree based on this example:
    http://qt-project.org/doc/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

    My internal model is correctly populated and I can manually output it to console to check that it works perfectly.

    In the QML, I do something like:
    @ ListView
    {
    id: sceneGraphView
    anchors.fill: parent

    model: sceneGraphModel
    delegate: sceneNodeDelegate
    

    }@

    Unfortunately, I only get the first element of my model. Is this the correct way to populate a ListView?

    Also, the model's methods called when I launch the application are:
    @rowCount
    roleNames
    index
    rowCount
    columnCount
    data @

    Thanks for any help.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Are you calling "beginInsertRows":https://qt-project.org/doc/qt-5.1/qtcore/qabstractitemmodel.html#beginInsertRows and "endInsertRows":https://qt-project.org/doc/qt-5.1/qtcore/qabstractitemmodel.html#endInsertRows when populating the list ?
      See "this":https://qt-project.org/doc/qt-5.1/qtquick/qtquick-modelviewsdata-cppmodels.html example for how to use C++ model in QML

      157

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thanks for your answer.

        The model is already populated when the view access it the first time calling rowCount.

        My main concern is about the C++ model members called from the QML ListView. I am not sure it is correct and sufficient.

        Best regards.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Ok, it seems that the QML View I use is not appropriate for that. When testing with a QTreeView, it worked immediately. What QML view should I use then to display a Tree in QML? it seems that none are present for the moment.

          Thanks for any help.

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            Hi,

            Currently there's no of Treeview for QML. But there are some work around's
            For eg. See "this":http://qt-project.org/forums/viewthread/30521/#136940

            157

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Ok, I see. Thanks a lot for your help!

              1 Reply Last reply
              0

              • Login

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