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. where we should use Component
Forum Updated to NodeBB v4.3 + New Features

where we should use Component

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 444 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.
  • D Offline
    D Offline
    dalishi
    wrote on last edited by dalishi
    #1

    Hi im still learning QML and playing around with this Map Viewer example. When it comes to defining Component in the main qml file, I'm a bit confused.

    Component.PNG

    A component is defined and it only contains a MapCompoent item defined in MapComponent.qml which has a Map object defined.
    MapComponent.qml:
    Component3.PNG

    This component is then used to create an object:
    Component2.PNG

    My question is why we dont just define a Map item in the main qml but bother to use a component inbetween (main qml -> mapComponent->map)?

    ODБOïO 1 Reply Last reply
    0
    • D dalishi

      Hi im still learning QML and playing around with this Map Viewer example. When it comes to defining Component in the main qml file, I'm a bit confused.

      Component.PNG

      A component is defined and it only contains a MapCompoent item defined in MapComponent.qml which has a Map object defined.
      MapComponent.qml:
      Component3.PNG

      This component is then used to create an object:
      Component2.PNG

      My question is why we dont just define a Map item in the main qml but bother to use a component inbetween (main qml -> mapComponent->map)?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      hi
      @dalishi said in where we should use Component:

      My question is why we dont just define a Map item in the main qml but bother to use a component inbetween (main qml -> mapComponent->map)?

      MapComponent.qml is 600 lines, you can write that code in your main.qml but its better to make a separate QML Component for it so it is reusable plus you keep your main.qml clean and easy to read.

      D 1 Reply Last reply
      0
      • ODБOïO ODБOï

        hi
        @dalishi said in where we should use Component:

        My question is why we dont just define a Map item in the main qml but bother to use a component inbetween (main qml -> mapComponent->map)?

        MapComponent.qml is 600 lines, you can write that code in your main.qml but its better to make a separate QML Component for it so it is reusable plus you keep your main.qml clean and easy to read.

        D Offline
        D Offline
        dalishi
        wrote on last edited by dalishi
        #3

        @ODБOï Hi thanks for your reply. I'm not sure about the "reusable" part. In this case, if i want to reuse the Map item defined in MapComponet.qml, I could just do:

        MapCompoment {
            id: myMap
            ....
        }
        

        Right? What's the point of encapsulating by another Component? I'm sorry if my question is dumb.

        ODБOïO 1 Reply Last reply
        0
        • D dalishi

          @ODБOï Hi thanks for your reply. I'm not sure about the "reusable" part. In this case, if i want to reuse the Map item defined in MapComponet.qml, I could just do:

          MapCompoment {
              id: myMap
              ....
          }
          

          Right? What's the point of encapsulating by another Component? I'm sorry if my question is dumb.

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by
          #4

          if you do like this
          @dalishi said in where we should use Component:

          MapCompoment {
          id: myMap
          ....
          }

          you will create/instanciate a MapCompoment component directly/statically, but if you put it in a Component QML type
          like this

          Component{
          MapCompoment {}
          }
          

          then you can use a QML Loader or Javascript createObject() function to create/instanciate it dinamically later somewhere else in that same .qml file
          Component is often used to define small qml objects inline and use them in the same .qml file

          D 1 Reply Last reply
          0
          • ODБOïO ODБOï

            if you do like this
            @dalishi said in where we should use Component:

            MapCompoment {
            id: myMap
            ....
            }

            you will create/instanciate a MapCompoment component directly/statically, but if you put it in a Component QML type
            like this

            Component{
            MapCompoment {}
            }
            

            then you can use a QML Loader or Javascript createObject() function to create/instanciate it dinamically later somewhere else in that same .qml file
            Component is often used to define small qml objects inline and use them in the same .qml file

            D Offline
            D Offline
            dalishi
            wrote on last edited by
            #5

            @ODБOï Thanks very much.

            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