QML Object Types

A QML object type is a type from which a QML object can be instantiated.

Unlike value types, object types are passed by reference. If you assign an instance of an object type to two different properties, both properties carry the same value. If the object is modified, the change is reflected in both properties.

In syntactic terms, a QML object type is one which can be used to create an instance of an object by specifying the type name followed by a set of curly braces that contains the attributes of that object. For example, Rectangle is a QML object type: it can be used to create Rectangle type objects, like this one:

 Rectangle {
     width: 200
     height: 200
     color: "blue"
 }

Value types, such as the int and bool primitive types cannot be used in the same way. These are used to hold simple values rather than objects.

There are two built-in object types that can be used without importing any other modules: QtObject is the base type of all object types. Component can be used to define new object types inline in QML documents.

Defining Object Types

Custom QML object types can be defined in several different ways, enabling each new type to be developed with an appropriate amount of effort for each use case, using the most suitable tools and programming language to implement it.

Defining Object Types with QML Documents

Object types can be defined by creating a QML document in a .qml file that defines the type. A QML document, when visible to the QML import system, usually defines a type identified by the name of the file minus the file extensions. A qmldir file can override this.

For example, a QML document named "MyButton.qml" provides the definition of the "MyButton" type, which may be used in a QML application.

See the documentation about QML Documents for information on how to define a QML document, and the syntax of the QML language. Once you are familiar with the QML language and how to define QML documents, see Defining Object Types with QML Documents for more information.

Defining Anonymous Types with Component

Another method of creating object types from within QML is to use the Component type. This allows a type to be defined inline within a QML document, instead of using a separate document in a .qml file.

 Item {
     id: root
     width: 500; height: 500

     Component {
         id: myComponent
         Rectangle { width: 100; height: 100; color: "red" }
     }

     Component.onCompleted: {
         myComponent.createObject(root)
         myComponent.createObject(root, {"x": 200})
     }
 }

Here, the myComponent object essentially defines an anonymous type that can be instantiated using Component::createObject to create objects of this anonymous type.

Inline components share all the characteristics of regular top-level components and use the same import list as their containing QML document.

Note that each Component object declaration creates its own component scope. Any id values used and referred to from within a Component object declaration must be unique within that scope, but do not need to be unique within the document in which the inline component is declared. So, the Rectangle declared in the myComponent object declaration could have an id of root without conflicting with the root declared for the Item object in the same document, as these two id values are declared within different component scopes.

See Scope and Naming Resolution for more details.

Defining Object Types from C++

C++ plugin writers and application developers may register types defined in C++ through APIs provided by the Qt Qml module. There are various registration functions which each allow different use-cases to be fulfilled. For more information about those registration functions, and the specifics of exposing custom C++ types to QML, see the documentation regarding Defining QML Types from C++.

The QML type-system relies on QML modules being installed into a known import path. QML modules may be provided by third-party developers and reused by client application developers. Please see the documentation about QML modules for more information about how to create and deploy a QML extension module.

For more information about the different C++ and QML integration methods, see the C++ and QML integration overview page.