1
votes

Qt's doc, says that :

Components are reusable, encapsulated QML types with well-defined interfaces.

Components are often defined by component files - that is, .qml files. The Component type essentially allows QML components to be defined inline, within a QML document, rather than as a separate QML file.

What is the meaning of "encapsulated QML types with well-defined interfaces" ?

Also when it comes to define a new QML object type, from Qt's doc we have :

To create an object type, a QML document should be placed into a text file named as TypeName.qml where TypeName is the desired name of the type

So what is the difference between a QML component and a QML object type ?

Thank you.

2

2 Answers

2
votes

in this document they call custom objects as component.

A component is a reusable type with a well-defined interface, built entirely in QML. Any snippet of QML code can become a component, by placing the code in a file ".qml" where is the new component name, beginning with an uppercase letter. These QML files automatically become available as new QML element (object) types to other QML components and applications in the same directory.

custom object type and component are the same but object like rectangle isn't component . whenever you create a custom object with TypeName.qml file , engin loads the TypeName.qml document as a component.

we have 2 kind of component:

  1. defined by component file - that is, .qml files. so we can say this kind of component is same as qml document.
  2. The Component type essentially allows QML components to be defined inline, within a QML document, rather than as a separate QML file.

in this document they say :

An instance of the object type defined by a document may be created using a Component in QML code, or a QQmlComponent in C++. Alternatively, if the object type is explicitly exposed to the QML type system with a particular type name, the type may be used directly in object declarations in other documents.

so if you create a custom object type you actually create a component and vice versa.

2
votes

What is the meaning of "encapsulated QML types with well-defined interfaces" ?

To me, the encapsulated part means that the QML contained in the component is self-contained and hence can be reused. The well-defined part means that only the properties defined in the root object of the component can be accessed by outside code, similar to how you would use the protected and private keywords in C++.

So what is the difference between a QML component and a QML object type ?

My understanding of what the documentation is saying is that a Component that is declared inline can't be instantiated in the same manner [1]. For example:

Component {
    id: myComponent

    Text {
        text: "Hello"
    }
}

You can use myComponent as e.g. a delegate:

ListView {
    // ...
    delegate: myComponent
}

But you can't use it to directly instantiate the type it represents:

myComponent {
    // ...
}

That's why you need to move the component into its own file (a "QML Object Type"):

import QtQuick 2.0

Text {
    text: "Hello"
}

Assuming you named the file MyType.qml, you can directly instantiate the type like this:

MyType {
    text: "Some text"
}

[1] As of Qt 5.15, it will be possible to instantiate components inline. See this section once 5.15 is released.