where we should use Component
-
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.
A component is defined and it only contains a MapCompoent item defined in MapComponent.qml which has a Map object defined.
MapComponent.qml:
This component is then used to create an object:
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)?
-
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.
-
@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.
-
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 thisComponent{ 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