Component QML 类型

封装 QML 组件定义。 更多...

导入语句: import QtQuick .

附加信号

详细描述

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

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

组件 type is commonly used to provide graphical components for views. For example, the ListView::delegate and Repeater::delegate properties require a 组件 to specify how each list item is to be displayed.

import QtQuick 2.15
Item {
    Component {
        id: redSquare
        Rectangle {
            color: "red"
            width: 10
            height: 10
        }
    }
    Row {
        Repeater {
            model: 3
            delegate: redSquare
        }
    }
}
						

Defining a 组件 is similar to defining a QML document. A QML document contains a single top-level item that defines the behavior and properties of the component. It cannot define properties or behavior outside of that top-level item. In the same way, a 组件 definition contains a single top-level item (which in the above example is a Rectangle ) and cannot define any data outside of this item, with the exception of an id .

另请参阅 Model-View-Delegate pattern .

Attached Signal Documentation

completed ()

Emitted after the object has been instantiated. This can be used to execute script code at startup, after the QML environment is ready.

相应处理程序是 onCompleted . It can be declared on any object. The order of running the onCompleted handlers is undefined.

Rectangle {
    Component.onCompleted: console.log("Completed Running!")
    Rectangle {
        Component.onCompleted: console.log("Nested Completed Running!")
    }
}
								

注意: 相应处理程序是 onCompleted .