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. Nested Repeaters with Loader
Forum Updated to NodeBB v4.3 + New Features

Nested Repeaters with Loader

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.4k 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.
  • K Offline
    K Offline
    Kobus
    wrote on 3 Mar 2021, 18:54 last edited by
    #1

    This is a simplified version of my problem. I'm loading items through a loader and placing them inside a Repeater. Some of the items in turn, consists of repeated elements. The first QML, SimpleWidget.qml creates a rectangle. Widget.qml in turn contains a Repeater that creates a rectangle for each model element.

    I would like the items to be placed in a row next to each other.
    Untitled.png

    However, the contents of the second widget is placed on top of the first widget.
    Screenshot from 2021-03-03 20-28-44.png

    main.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        id: root
        visible: true
        width: 640
        height: 480
        color: "darkgray"
    
        Row {
            spacing: 10
            Repeater {
                model: widgetsModel
                delegate: Loader {
                    source: _source
                }
            }
        }
    
        ListModel {
            id: widgetsModel
    
            ListElement {
                _source: "SimpleWidget.qml"
            }
            ListElement {
                _source: "Widget.qml"
            }
        }
    }
    
    

    SimpleWidget.qml

    import QtQuick 2.0
    
    Rectangle {
        width: 100
        height: 60
        color: "green"
        border.color: "black"
    }
    

    Widget.qml

    import QtQuick 2.0
    
    Rectangle {
        Row {
            spacing: 5
            Repeater {
                model: itemsModel
                delegate: Rectangle {
                    width: _width
                    height: 50
                    color: _color
                    border.color: "black"
                }
            }
        }
    
        ListModel {
            id: itemsModel
            ListElement {
                _color: "brown"
                _width: 67
            }
            ListElement {
                _color: "yellow"
                _width: 80
            }
        }
    }
    
    1 Reply Last reply
    1
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 4 Mar 2021, 06:44 last edited by sierdzio 3 Apr 2021, 06:45
      #2

      In Widget.qml the top-level Rectangle has no dimensions (width and height are 0). That's why your items are overlapping - Repeater thinks an item has no size while it does.

      Btw. you can also simplify the code in main.qml like this:

      model: [ "SimpleWidget.qml", "Widget.qml" ]
      delegate: Loader {
        source: modelData
      }
      

      (Z(:^

      K 1 Reply Last reply 4 Mar 2021, 08:28
      2
      • S sierdzio
        4 Mar 2021, 06:44

        In Widget.qml the top-level Rectangle has no dimensions (width and height are 0). That's why your items are overlapping - Repeater thinks an item has no size while it does.

        Btw. you can also simplify the code in main.qml like this:

        model: [ "SimpleWidget.qml", "Widget.qml" ]
        delegate: Loader {
          source: modelData
        }
        
        K Offline
        K Offline
        Kobus
        wrote on 4 Mar 2021, 08:28 last edited by Kobus 3 Apr 2021, 08:33
        #3

        @sierdzio Thanks, that solved it. Ideally, I would like the size of the Rectangle in Widget.qml to be based on the size of the repeated subcomponents and I assumed that is what would happen.

        OK, so I added an id property to the row and added the following to the rectangle:

            height: widgetsRow.height
            width: widgetsRow.width
        

        So simple, thanks!

        1 Reply Last reply
        1
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 4 Mar 2021, 08:33 last edited by
          #4

          You have 2 options:

          • use Row as your top level component (remove Rectangle)
          • set Rectangle dimensions to match Row dimensions (set width and height to the same value as Row)

          (Z(:^

          K 1 Reply Last reply 4 Mar 2021, 08:35
          1
          • S sierdzio
            4 Mar 2021, 08:33

            You have 2 options:

            • use Row as your top level component (remove Rectangle)
            • set Rectangle dimensions to match Row dimensions (set width and height to the same value as Row)
            K Offline
            K Offline
            Kobus
            wrote on 4 Mar 2021, 08:35 last edited by
            #5

            @sierdzio Thanks!

            1 Reply Last reply
            0

            1/5

            3 Mar 2021, 18:54

            • Login

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