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

How to use custom components in QML Repeaters

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 1.3k 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 4 Feb 2022, 09:54 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 4 Feb 2022, 21:38 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
      • M Offline
        M Offline
        Markkyboy
        wrote on 6 Feb 2022, 16:54 last edited by Markkyboy 2 Jun 2022, 18:49
        #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 1 Reply Last reply 7 Feb 2022, 13:27
        0
        • M Markkyboy
          6 Feb 2022, 16:54

          @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 Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 7 Feb 2022, 13:27 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.

          M 1 Reply Last reply 10 Feb 2022, 07:43
          0
          • J J.Hilk
            7 Feb 2022, 13:27

            @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?

            M Offline
            M Offline
            Markkyboy
            wrote on 10 Feb 2022, 07:43 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 1 Reply Last reply 10 Feb 2022, 07:48
            0
            • M Markkyboy
              10 Feb 2022, 07:43

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

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 10 Feb 2022, 07:48 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

              1/6

              4 Feb 2022, 09:54

              • Login

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