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 use custom components in QML Repeaters

How to use custom components in QML Repeaters

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 1.6k 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.
  • P Offline
    P Offline
    Praveen.Illa
    wrote on last edited by
    #1

    Hi Team,

    If I use a rectangle item inside repeater, the output is working as expected.
    I am facing an issue if a custom component is used inside the repeater. The output is not as expected.
    Can someone please help me on this.

    Below is the code
    main.qml

    Window {
        visible: true
        width: 500
        height: 500
        title: qsTr("Hello World")
    
        property var arr : ["red", "black"]
    
        ColumnLayout {
            anchors.fill: parent
    
            Repeater {
                model: 2
    
                Rectangle {
                    width: 100
                    height: 100
                    color: arr[index]
                }
    
                //CustomComponent {
                //    colorName: arr[index]
                //}
            }
        }
    }
    

    CustomComponent.qml

    Item {
        id: customComponent
        width: 500
        height: 500
        visible: true
    
        property string colorName
    
        Rectangle {
            width: 100
            height: 100
            color: colorName
        }
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Praveen-Illa said in How to use custom components in QML Repeaters:

      The output is not as expected.

      What is wrong, then? Code looks fine.

      (Z(:^

      1 Reply Last reply
      0
      • MarkkyboyM Offline
        MarkkyboyM Offline
        Markkyboy
        wrote on last edited by Markkyboy
        #3

        @Praveen-Illa is right, if the custom component is used in place of rectangle, the error from console says that CustomComponent is not a type (words to the effect). I compiled this with Qt6.

        QQmlApplicationEngine failed to load component
        qrc:/custom-component-repeater/main.qml:10:5: CustomComponent is not a type
        

        I checked out the documents https://doc.qt.io/qt-6/qtqml-documents-definetypes.html and followed the brief tutorial and the same result is the one given above.

        So it seems that QQmlApplicationEngine is not happy with something, but i don't understand what is missing or incorrect.

        Don't just sit there standing around, pick up a shovel and sweep up!

        I live by the sea, not in it.

        J.HilkJ 1 Reply Last reply
        0
        • MarkkyboyM Markkyboy

          @Praveen-Illa is right, if the custom component is used in place of rectangle, the error from console says that CustomComponent is not a type (words to the effect). I compiled this with Qt6.

          QQmlApplicationEngine failed to load component
          qrc:/custom-component-repeater/main.qml:10:5: CustomComponent is not a type
          

          I checked out the documents https://doc.qt.io/qt-6/qtqml-documents-definetypes.html and followed the brief tutorial and the same result is the one given above.

          So it seems that QQmlApplicationEngine is not happy with something, but i don't understand what is missing or incorrect.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Markkyboy works perfectly fine for me.

          0da21daa-afb1-4065-a313-b5fa6ee798ef-image.png

          your CustomComponent.qml is inside a resource file, right ?
          And you did rerun qmake after adding it to it?


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          MarkkyboyM 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @Markkyboy works perfectly fine for me.

            0da21daa-afb1-4065-a313-b5fa6ee798ef-image.png

            your CustomComponent.qml is inside a resource file, right ?
            And you did rerun qmake after adding it to it?

            MarkkyboyM Offline
            MarkkyboyM Offline
            Markkyboy
            wrote on last edited by
            #5

            @J-Hilk - Yes, Rectangle works, now replace that with CustomComponent.qml as in the crossed out chunk of code given by the OP.

            Don't just sit there standing around, pick up a shovel and sweep up!

            I live by the sea, not in it.

            J.HilkJ 1 Reply Last reply
            0
            • MarkkyboyM Markkyboy

              @J-Hilk - Yes, Rectangle works, now replace that with CustomComponent.qml as in the crossed out chunk of code given by the OP.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Markkyboy

              fdd43a24-e6fe-401d-84e7-4bccb3f10cdc-image.png

              and if you actually adjust the sizes a bit, one also sees the 2nd component
              0bc85a73-67b1-44a4-ab2d-68e5a3571e82-image.png

              like I said, works perfectly fine. But admittedly I should have used the correct screenshot.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              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 Extensions
              • Unsolved