ObjectModel QML Type
モデルとして使用する項目のセットを定義します。詳細...
| Import Statement: | import QtQml.Models |
プロパティ
- count : int
関連するプロパティ
- index : int
方法
- void append(object item)
- void clear()
- QtObject get(int index)
- void insert(int index, object item)
- void move(int from, int to, int n)
- void remove(int index, int n)
詳細な説明
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]
モデル内のアイテム数。このプロパティは読み取り専用です。
添付プロパティのドキュメント
ObjectModel.index : int [attached]
この添付プロパティは、モデル内におけるこのデリゲートの項目のインデックスを保持します。
これは、デリゲートの各インスタンスに紐付けられています。
メソッドのドキュメント
void append(object item)
モデルの末尾に新しいitem を追加します。
objectModel.append(objectComponent.createObject())insert() およびremove()も参照してください 。
void clear()
モデルからすべての項目を削除します。
append() およびremove()も参照してください 。
QtObject get(int index)
モデル内のindex にあるアイテムを返します。これにより、JavaScriptからそのアイテムにアクセスしたり、変更したりできるようになります。
Component.onCompleted: {
objectModel.append(objectComponent.createObject())
console.log(objectModel.get(0).objectName);
objectModel.get(0).objectName = "first";
}index は、リスト内の要素でなければなりません。
append()も参照してください 。
void insert(int index, object item)
位置index に、新しいitem をモデルに挿入します。
objectModel.insert(2, objectComponent.createObject())index は、リスト内の既存の項目、またはリストの末尾の次の項目(append と同等)でなければなりません。
append() およびremove()も参照してください 。
void move(int from, int to, int n = 1)
n の項目を、from を1つ分移動させ、to をさらに1つ分移動させます。
from と to の範囲は存在している必要があります。例えば、最初の 3 つの項目をモデルの末尾に移動するには:
objectModel.move(0, objectModel.count - 3, 3)append()も参照してください 。
void remove(int index, int n = 1)
index にある「n 」の項目をモデルから削除します。
clear()も参照してください 。
© 2026 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.