Qt 4.8
|
The GridView item provides a grid view of items provided by a model. More...
The GridView item provides a grid view of items provided by a model.
A GridView displays data from models created from built-in QML elements like ListModel and XmlListModel, or custom model classes defined in C++ that inherit from QAbstractListModel.
A GridView has a model , which defines the data to be displayed, and a delegate , which defines how the data should be displayed. Items in a GridView are laid out horizontally or vertically. Grid views are inherently flickable as GridView inherits from Flickable .
The following example shows the definition of a simple list model defined in a file called ContactModel.qml:
{class="float-right"}
This model can be referenced as ContactModel
in other QML files. See QML Modules for more information about creating reusable components like this.
Another component can display this model data in a GridView, as in the following example, which creates a ContactModel
component for its model, and a Column element (containing Image and Text elements) for its delegate.
{class="float-right"}
The view will create a new delegate for each item in the model. Note that the delegate is able to access the model's name
and portrait
data directly.
An improved grid view is shown below. The delegate is visually improved and is moved into a separate contactDelegate
component.
The currently selected item is highlighted with a blue Rectangle using the highlight property, and focus
is set to true
to enable keyboard navigation for the grid view. The grid view itself is a focus scope (see the focus documentation page for more details).
Delegates are instantiated as needed and may be destroyed at any time. State should never be stored in a delegate.
GridView attaches a number of properties to the root item of the delegate, for example {GridView.isCurrentItem}
. In the following example, the root delegate item can access this attached property directly as GridView.isCurrentItem
, while the child contactInfo
object must refer to this property as wrapper.GridView.isCurrentItem
.