ObjectModel QML Type

モデルとして使用するアイテムのセットを定義します。詳細...

Import Statement: import QtQml.Models

プロパティ

付属物件

方法

詳細説明

ObjectModel にはビューで使用されるビジュアルアイテムが含まれています。ObjectModel がビューで使用される場合、ObjectModel には既にビジュアルデリゲート(アイテム)が含まれているため、ビューはデリゲートを必要としません。

アイテムは、index のアタッチプロパティにより、モデル内のインデックスを決定することができます。

以下の例では、ListView に 3 つの色の付いた長方形を配置しています。

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 [read-only]

モデル内のアイテムの数。このプロパティは読み取り専用です。


Attached Property ドキュメント

ObjectModel.index : int

この Attached プロパティは、モデル内のこのデリゲートのアイテムのインデックスを保持します。

このプロパティは、デリゲートの各インスタンスにアタッチされます。


メソッドの説明

append(object item)

新しいitem をモデルの最後に追加します。

objectModel.append(objectComponent.createObject())

insert() およびremove()も参照して ください。


clear()

モデルからすべての項目を削除します。

append() およびremove() も参照して ください。


object get(int index)

モデル内のindex にある項目を返します。これにより、JavaScript からアイテムにアクセスしたり、アイテムを変更したりすることができます:

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

index はリストの要素でなければなりません。

append()も参照してください


insert(int index, object item)

index の位置に新しいitem をモデルに挿入します。

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

index は、リスト内の既存の項目か、リストの末尾をひとつ過ぎた項目でなければなりません(append と同じ)。

append() およびremove()も参照


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

n 項目from をある位置to に移動する。

fromとtoの範囲が存在する必要があります。例えば、最初の3つの項目をモデルの最後に移動する場合です:

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

append()も参照


remove(int index, int n = 1)

indexn 項目をモデルから削除します。

clear() も参照


© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.