ObjectModel QML 类型

定义要用作模型的一组项。 更多...

import 语句: import QtQml.Models 2.15

特性

附加特性

方法

详细描述

An ObjectModel contains the visual items to be used in a view. When an ObjectModel is used in a view, the view does not require a delegate since the ObjectModel already contains the visual delegate (items).

An item can determine its index within the model via the index 附加特性。

The example below places three colored rectangles in a ListView .

import QtQuick 2.0
import QtQml.Models 2.1
Rectangle {
    ObjectModel {
        id: itemModel
        Rectangle { height: 30; width: 80; color: "red" }
        Rectangle { height: 30; width: 80; color: "green" }
        Rectangle { height: 30; width: 80; color: "blue" }
    }
    ListView {
        anchors.fill: parent
        model: itemModel
    }
}
					

另请参阅 Qt Quick 范例 - 视图 .

特性文档编制

count : int

The number of items in the model. This property is readonly.

附加特性文档编制

ObjectModel.index : int

This attached property holds the index of this delegate's item within the model.

It is attached to each instance of the delegate.

方法文档编制

append ( 对象 item )

追加新的 item to the end of the model.

objectModel.append(objectComponent.createObject())
					

This method was introduced in Qt 5.6.

另请参阅 insert() and remove() .

clear ()

Clears all items from the model.

This method was introduced in Qt 5.6.

另请参阅 append() and remove() .

对象 get ( int index )

返回项在 index in the model. This allows the item to be accessed or modified from JavaScript:

Component.onCompleted: {
    objectModel.append(objectComponent.createObject())
    console.log(objectModel.get(0).objectName);
    objectModel.get(0).objectName = "first";
}
					

The index must be an element in the list.

This method was introduced in Qt 5.6.

另请参阅 append() .

insert ( int index , 对象 item )

Inserts a new item to the model at position index .

objectModel.insert(2, objectComponent.createObject())
					

The index must be to an existing item in the list, or one past the end of the list (equivalent to append).

This method was introduced in Qt 5.6.

另请参阅 append() and remove() .

move ( int from , int to , int n = 1)

移动 n from one position to another.

The from and to ranges must exist; for example, to move the first 3 items to the end of the model:

objectModel.move(0, objectModel.count - 3, 3)
					

This method was introduced in Qt 5.6.

另请参阅 append() .

remove ( int index , int n = 1)

移除 n items at index from the model.

This method was introduced in Qt 5.6.

另请参阅 clear() .