Qt Reference Documentation

QML GridView Element

[Inherits Flickable]

The GridView item provides a grid view of items provided by a model. More...

Properties

Attached Properties

Attached Signals

Methods

Detailed Description

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.

For example, if there is a simple list model defined in a file ContactModel.qml like this:

 import Qt 4.7

 ListModel {

     ListElement {
         name: "Jim Williams"
         portrait: "pics/portrait.png"
     }
     ListElement {
         name: "John Brown"
         portrait: "pics/portrait.png"
     }
     ListElement {
         name: "Bill Smyth"
         portrait: "pics/portrait.png"
     }
     ListElement {
         name: "Sam Wise"
         portrait: "pics/portrait.png"
     }
 }

Another component can display this model data in a GridView, like this:

 import Qt 4.7

 GridView {
     width: 300; height: 200

     model: ContactModel {}
     delegate: Column {
         Image { source: portrait; anchors.horizontalCenter: parent.horizontalCenter }
         Text { text: name; anchors.horizontalCenter: parent.horizontalCenter }
     }
 }

Here, the GridView creates a ContactModel component for its model, and a Column element (containing Image and Text elements) for its delegate. The view will create a new delegate for each item in the model. Notice 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. Also, 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.

 Rectangle {
     width: 300; height: 200

     Component {
         id: contactDelegate
         Item {
             width: grid.cellWidth; height: grid.cellHeight
             Column {
                 anchors.fill: parent
                 Image { source: portrait; anchors.horizontalCenter: parent.horizontalCenter }
                 Text { text: name; anchors.horizontalCenter: parent.horizontalCenter }
             }
         }
     }

     GridView {
         id: grid
         anchors.fill: parent
         cellWidth: 80; cellHeight: 80

         model: ContactModel {}
         delegate: contactDelegate
         highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
         focus: true
     }
 }

Delegates are instantiated as needed and may be destroyed at any time. State should never be stored in a delegate.

Note: Views do not enable clip automatically. If the view is not clipped by another item or the screen, it will be necessary to set clip: true in order to have the out of view items clipped nicely.


Property Documentation

cacheBuffer : int

This property determines whether delegates are retained outside the visible area of the view.

If non-zero the view will keep as many delegates instantiated as will fit within the buffer specified. For example, if in a vertical view the delegate is 20 pixels high and cacheBuffer is set to 40, then up to 2 delegates above and 2 delegates below the visible area may be retained.

Note that cacheBuffer is not a pixel buffer - it only maintains additional instantiated delegates.

Setting this value can make scrolling the list smoother at the expense of additional memory usage. It is not a substitute for creating efficient delegates; the fewer elements in a delegate, the faster a view may be scrolled.


cellWidth : int

cellHeight : int

These properties holds the width and height of each cell in the grid.

The default cell size is 100x100.


count : int

This property holds the number of items in the view.


currentIndex : int

currentItem : Item

currentIndex holds the index of the current item. currentItem is the current item. Note that the position of the current item may only be approximate until it becomes visible in the view.


delegate : Component

The delegate provides a template defining each item instantiated by the view. The index is exposed as an accessible index property. Properties of the model are also available depending upon the type of Data Model.

The number of elements in the delegate has a direct effect on the flicking performance of the view. If at all possible, place functionality that is not needed for the normal display of the delegate in a Loader which can load additional elements when needed.

The GridView will layout the items based on the size of the root item in the delegate.

Note: Delegates are instantiated as needed and may be destroyed at any time. State should never be stored in a delegate.


flow : enumeration

This property holds the flow of the grid.

Possible values:

  • GridView.LeftToRight (default) - Items are laid out from left to right, and the view scrolls vertically
  • GridView.TopToBottom - Items are laid out from top to bottom, and the view scrolls horizontally

footer : Component

This property holds the component to use as the footer.

An instance of the footer component is created for each view. The footer is positioned at the end of the view, after any items.

See also header.


header : Component

This property holds the component to use as the header.

An instance of the header component is created for each view. The header is positioned at the beginning of the view, before any items.

See also footer.


highlight : Component

This property holds the component to use as the highlight.

An instance of the highlight component is created for each view. The geometry of the resulting component instance will be managed by the view so as to stay with the current item, unless the highlightFollowsCurrentItem property is false.

See also highlightItem and highlightFollowsCurrentItem.


highlightFollowsCurrentItem : bool

This property sets whether the highlight is managed by the view.

If this property is true, the highlight is moved smoothly to follow the current item. Otherwise, the highlight is not moved by the view, and any movement must be implemented by the highlight.

Here is a highlight with its motion defined by a SpringFollow item:

 Component {
     id: highlight
     Rectangle {
         width: view.cellWidth; height: view.cellHeight
         color: "lightsteelblue"; radius: 5
         SpringFollow on x { to: view.currentItem.x; spring: 3; damping: 0.2 }
         SpringFollow on y { to: view.currentItem.y; spring: 3; damping: 0.2 }
     }
 }

 GridView {
     id: view
     width: 300; height: 200
     cellWidth: 80; cellHeight: 80

     model: ContactModel {}
     delegate: Column {
         Image { source: portrait; anchors.horizontalCenter: parent.horizontalCenter }
         Text { text: name; anchors.horizontalCenter: parent.horizontalCenter }
     }

     highlight: highlight
     highlightFollowsCurrentItem: false
     focus: true
 }

highlightItem : Item

This holds the highlight item created from the highlight component.

The highlightItem is managed by the view unless highlightFollowsCurrentItem is set to false.

See also highlight and highlightFollowsCurrentItem.


highlightMoveDuration : int

This property holds the move animation duration of the highlight delegate.

highlightFollowsCurrentItem must be true for this property to have effect.

The default value for the duration is 150ms.

See also highlightFollowsCurrentItem.


keyNavigationWraps : bool

This property holds whether the grid wraps key navigation

If this is true, key navigation that would move the current item selection past one end of the view instead wraps around and moves the selection to the other end of the view.


model : model

This property holds the model providing data for the grid.

The model provides the set of data that is used to create the items in the view. Models can be created directly in QML using ListModel, XmlListModel or VisualItemModel, or provided by C++ model classes. If a C++ model class is used, it must be a subclass of QAbstractItemModel or a simple list.

See also Data Models.


preferredHighlightBegin : real

preferredHighlightEnd : real

highlightRangeMode : enumeration

These properties define the preferred range of the highlight (for the current item) within the view. The preferredHighlightBegin value must be less than the preferredHighlightEnd value.

These properties affect the position of the current item when the view is scrolled. For example, if the currently selected item should stay in the middle of the view when it is scrolled, set the preferredHighlightBegin and preferredHighlightEnd values to the top and bottom coordinates of where the middle item would be. If the currentItem is changed programmatically, the view will automatically scroll so that the current item is in the middle of the view. Furthermore, the behavior of the current item index will occur whether or not a highlight exists.

Valid values for highlightRangeMode are:

  • GridView.ApplyRange - the view attempts to maintain the highlight within the range. However, the highlight can move outside of the range at the ends of the view or due to mouse interaction.
  • GridView.StrictlyEnforceRange - the highlight never moves outside of the range. The current item changes if a keyboard or mouse action would cause the highlight to move outside of the range.
  • GridView.NoHighlightRange - this is the default value.

snapMode : enumeration

This property determines how the view scrolling will settle following a drag or flick. The possible values are:

  • GridView.NoSnap (default) - the view stops anywhere within the visible area.
  • GridView.SnapToRow - the view settles with a row (or column for GridView.TopToBottom flow) aligned with the start of the view.
  • GridView.SnapOneRow - the view will settle no more than one row (or column for GridView.TopToBottom flow) away from the first visible row at the time the mouse button is released. This mode is particularly useful for moving one page at a time.

Attached Property Documentation

GridView.delayRemove : bool

This attached property holds whether the delegate may be destroyed.

It is attached to each instance of the delegate.

It is sometimes necessary to delay the destruction of an item until an animation completes.

The example below ensures that the animation completes before the item is removed from the grid.

 Component {
     id: delegate
     Item {
         GridView.onRemove: SequentialAnimation {
             PropertyAction { target: wrapper; property: "GridView.delayRemove"; value: true }
             NumberAnimation { target: wrapper; property: "scale"; to: 0; duration: 250; easing.type: Easing.InOutQuad }
             PropertyAction { target: wrapper; property: "GridView.delayRemove"; value: false }
         }
     }
 }

GridView.isCurrentItem : bool

This attached property is true if this delegate is the current item; otherwise false.

It is attached to each instance of the delegate.


read-onlyGridView.view : GridView

This attached property holds the view that manages this delegate instance.

It is attached to each instance of the delegate.


Attached Signal Documentation

GridView::onAdd ()

This attached handler is called immediately after an item is added to the view.


GridView::onRemove ()

This attached handler is called immediately before an item is removed from the view.


Method Documentation

int GridView::indexAt ( int x, int y )

Returns the index of the visible item containing the point x, y in content coordinates. If there is no item at the point specified, or the item is not visible -1 is returned.

If the item is outside the visible area, -1 is returned, regardless of whether an item will exist at that point when scrolled into view.


GridView::moveCurrentIndexDown ()

Move the currentIndex down one item in the view. The current index will wrap if keyNavigationWraps is true and it is currently at the end.


GridView::moveCurrentIndexLeft ()

Move the currentIndex left one item in the view. The current index will wrap if keyNavigationWraps is true and it is currently at the end.


GridView::moveCurrentIndexRight ()

Move the currentIndex right one item in the view. The current index will wrap if keyNavigationWraps is true and it is currently at the end.


GridView::moveCurrentIndexUp ()

Move the currentIndex up one item in the view. The current index will wrap if keyNavigationWraps is true and it is currently at the end.


GridView::positionViewAtIndex ( int index, PositionMode mode )

Positions the view such that the index is at the position specified by mode:

  • GridView.Beginning - position item at the top (or left for GridView.TopToBottom flow) of the view.
  • GridView.Center - position item in the center of the view.
  • GridView.End - position item at bottom (or right for horizontal orientation) of the view.
  • GridView.Visible - if any part of the item is visible then take no action, otherwise bring the item into view.
  • GridView.Contain - ensure the entire item is visible. If the item is larger than the view the item is positioned at the top (or left for GridView.TopToBottom flow) of the view.

If positioning the view at the index would cause empty space to be displayed at the beginning or end of the view, the view will be positioned at the boundary.

It is not recommended to use contentX or contentY to position the view at a particular index. This is unreliable since removing items from the start of the view does not cause all other items to be repositioned. The correct way to bring an item into view is with positionViewAtIndex.


X