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. Using a Repeater inside a ListModel to generate ListElements
Forum Updated to NodeBB v4.3 + New Features

Using a Repeater inside a ListModel to generate ListElements

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 1.3k 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.
  • R Offline
    R Offline
    raiyan0511
    wrote on last edited by
    #1

    I am trying to use a backend C++ model inside my ListView, and was trying to create ListElements with a Repeater.

    Here's the ListModel code I have:

    import QtQuick 2.0
    
    ListModel {
    //    ListElement {
    //        name: "Forceps"
    //        weight: 1
    //    }
    
    //    ListElement {
    //        name: "Endoscope"
    //        weight: 2
    //    }
    
    //    ListElement {
    //        name: "Drill"
    //        weight: 3
    //    }
    
        Repeater {
            model: myModel
    
            ListElement {
                name: myModel.toolName
                weight: myModel.toolWeight
            }
        }
    }
    

    Here is the relevant code from main.cpp

        QList<QObject*> dataList;
    
        dataList.append(new ToolViewModel(0, "Forceps"));
        dataList.append(new ToolViewModel(1, "Endoscope"));
        dataList.append(new ToolViewModel(2, "Drill"));
        QQuickView view;
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
    

    I wanted to know if there was any way this could work, i.e using a Repeater inside a ListModel. Currently getting this error:
    ListElement: cannot contain nested elements

    Thank you!

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      This is not possible.

      You can use your dataList context property directly as a model of a ListView instead.

      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