IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

ObjectModel QML Type

Defines a set of items to be used as a model.

This type was introduced in Qt 5.1.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

ObjectModel QML Type

  • Import Statement: import QtQml.Models

  • Since:: Qt 5.1

  • Group: ObjectModel is part of qtquick-models

Detailed Description

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 attached property.

The example below places three colored rectangles in a ListView.

 
Sélectionnez
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
    }
}
Image non disponible

See Also

Property Documentation

 

[read-only] count : int

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

Attached Property Documentation

 

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.

Method Documentation

 

[since 5.6] append(object item)

Appends a new item to the end of the model.

 
Sélectionnez
objectModel.append(objectComponent.createObject())

This method was introduced in Qt 5.6.

See Also

See also insert(), remove()

[since 5.6] clear()

Clears all items from the model.

This method was introduced in Qt 5.6.

See Also

See also append(), remove()

[since 5.6] object get(int index)

Returns the item at index in the model. This allows the item to be accessed or modified from JavaScript:

 
Sélectionnez
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.

See Also

See also append()

[since 5.6] insert(int index, object item)

Inserts a new item to the model at position index.

 
Sélectionnez
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.

See Also

See also append(), remove()

[since 5.6] move(int from, int to, int n = 1)

Moves n items 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:

 
Sélectionnez
objectModel.move(0, objectModel.count - 3, 3)

This method was introduced in Qt 5.6.

See Also

See also append()

[since 5.6] remove(int index, int n = 1)

Removes n items at index from the model.

This method was introduced in Qt 5.6.

See Also

See also clear()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+