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. Dynamic delegate in Repeater
Forum Updated to NodeBB v4.3 + New Features

Dynamic delegate in Repeater

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 5 Posters 4.8k 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.
  • CharlieGC Offline
    CharlieGC Offline
    CharlieG
    wrote on last edited by
    #1

    Hello,

    I want use Repeater with dynamic delegates.

    For example, I have this model :

     ListModel {
            id: listModelLabel
            ListElement {lib: "SITE"; type: "TEXT"}
            ListElement {lib: "REP"; type: "TEXT"}
            ListElement {lib: "FAB"; type: "AREA"}
    }
    

    and this components :

    Component {
        id: text
        Label { ... }
    }
    
    Component {
        id: area
        TextArea { ... }
    }
    

    In my Repeater, if the current item has the property type = TEXT, so the delegate will be text. But if type = AREA, the delegate will be area

    But, this doesn't work.

    Here, the code of my repeater :

    Repeater {
                id: repeater
                model: listModelLabel
                delegate: {
                    if (listModelLabel[index].type === "TEXT"){
                        text
                    }
                    if (listModelLabel[index].type === "AREA"){
                        area
                    }
                }
            }
    

    Do you know, if this is possible ?

    Thank you for your help.

    Bye.

    Charlie

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

      You should use a Loader for that :

      Repeater {
          id: repeater
          model: listModelLabel
          Loader {
              sourceComponent: {
                  if (listModelLabel[index].type === "TEXT") 
                      return text;
                  if (listModelLabel[index].type === "AREA")
                      return area;
              }
          }
      }
      

      You could also use source instead of sourceComponent if you'd like to define your different delegates in their own file.

      1 Reply Last reply
      2
      • Marco PellinM Offline
        Marco PellinM Offline
        Marco Pellin
        wrote on last edited by
        #3

        Completing the previous answer:

        • listModelLabel[index].type is not correct; use type instead, because it is automatically referring to the current element loaded from your List Model
        • I would suggest to use a Positioner (Row, Column, Grid, ...) for the repeater, otherwise everything will be overlapped. Add some spacing if needed.
        • Given the fact that both your components have a property called "text", you may change it according to the "lib" string in your model, for example. This is possible using "onLoaded" property of the Loader component.

        This is the code I used:

            ListModel {
               id: listModelLabel
               ListElement {lib: "SITE"; type: "TEXT"}
               ListElement {lib: "REP"; type: "TEXT"}
               ListElement {lib: "FAB"; type: "AREA"}
            }
        
            Component {
                id: text
                Label {
                }
            }
        
            Component {
                id: area
                TextArea {
                }
            }
        
            Column {
                Repeater {
                    id: repeater
                    model: listModelLabel
                    delegate: Loader {
                        sourceComponent: {
                            if (type === "TEXT")
                                text;
                            if (type === "AREA")
                                area;
                        }
                        onLoaded: {
                            item.text = lib;
                        }
                    }
                }
            }
        
        1 Reply Last reply
        1
        • CharlieGC Offline
          CharlieGC Offline
          CharlieG
          wrote on last edited by
          #4

          Thank you. It works.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Surajkumar Tanurkar
            wrote on last edited by
            #5

            How to append multiple qml's in my ListView??
            Please help..

            Pablo J. RoginaP 1 Reply Last reply
            0
            • S Surajkumar Tanurkar

              How to append multiple qml's in my ListView??
              Please help..

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Surajkumar-Tanurkar would you mind creating a new post for your question?

              You're using a topic already marked as solved, and with no activity since 3 years ago, come on...

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              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