What is the difference between Component
and Item
in QML ? The documentation is not absolutely clear here. What is the preferred type to use as a container for several widgets? Can it be replacable by Rectangle
?
For example, what is the difference in the following declarations:
Item { id: itemWidget Rectangle { id: one } Rectangle { id: two } }
and
Component { id: componentWidget Rectangle { id: one } Rectangle { id: two } }
Why do we usually use Component
when declaring a delegate
?
A component provides a way of defining a new type that we can re-use in other QML files. A QML component is like a black-box and interacts with the outside world through properties, signals and functions and is generally defined in its own QML file. (For more details, see the Component documentation).
Qt Quick provides everything needed to create a rich application with a fluid and dynamic user interface. It enables user interfaces to be built around the behavior of user interface components and how they connect with one another, and it provides a visual canvas with its own coordinate system and rendering engine.
Using alias means use another name for property like C++ reference type declaration it must been init at time that has been created. The main usage of alias is to ability to export inner scope property to outer scope of object scope. Also we can use property to achieve this goal across binding feature of qml.
QML is compiled to an optimized bytecode-like stream and the JavaScript expressions pass through an optimized evaluator for simple expressions.
The difference between those two snippets is that the Rectangle
will be immediately displayed. This is written in the documentation:
Notice that while a Rectangle by itself would be automatically rendered and displayed, this is not the case for the above rectangle because it is defined inside a Component. The component encapsulates the QML types within, as if they were defined in a separate QML file, and is not loaded until requested (in this case, by the two Loader objects). Because Component is not derived from Item, you cannot anchor anything to it.
When declaring delegates, Component
is used because there are several delegate items that must be created. A single Item
doesn't work here. You can think of Component
as a template that you can create objects from.
A Component
is both a concept and a thing in QML. An Item
is a visual thing defined in QtQuick module which is usable in QML. These two things are conceptually different.
As a QML concept, all reusable things are called components. You can define a component in multiple ways, but one easy way is to create a .qml
file and name it as you name your component. i.e: Button.qml or Switch.qml. When the QML engine loads that file, you can use it as buttons or switches.
Another way to define a component is to use Component {}
in a .qml
file. This allows you to define a new component inline. A component defined inline does not work after explicitly loaded by a loader.
An Item
, on the other hand, is a simple type defined in the QtQuick module.
I think it is OK to call an Item
a Component
even though an Item
is not technically inherited from Component
. Or more precisely, you could say your custom component is based on an Item
if your .qml
has Item
as a root type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With