Dynamic Views

Repeaters work well for limited and static sets of data, but in the real world, models are commonly more complex – and larger. Here, a smarter solution is needed. For this, Qt Quick provides the ListView and GridView elements. These are both based on a Flickable area, so the user can move around in a larger dataset. At the same time, they limit the number of concurrently instantiated delegates. For a large model, that means fewer elements in the scene at once.

image

image

The two elements are similar in their usage. We will begin with the ListView and then describe the GridView with the former as the starting point of the comparison. Notice that the GridView places a list of items into a two-dimensional grid, left-to-right or top-to-bottom. If you want to show a table of data you need to use the TableView which is described in the Table Models section.

The ListView is similar to the Repeater element. It uses a model, instantiates a delegate and between the delegates, there can be spacing. The listing below shows how a simple setup can look.

  1. import QtQuick 6.2
  2. import "../common"
  3. Background {
  4. width: 80
  5. height: 300
  6. ListView {
  7. anchors.fill: parent
  8. anchors.margins: 20
  9. clip: true
  10. model: 100
  11. delegate: numberDelegate
  12. spacing: 5
  13. }
  14. Component {
  15. id: numberDelegate
  16. GreenBox {
  17. width: 40
  18. height: 40
  19. text: index
  20. }
  21. }
  22. }

image

If the model contains more data than can fit onto the screen, the ListView only shows part of the list. However, as a consequence of the default behavior of Qt Quick, the list view does not limit the screen area within which the delegates are shown. This means that delegates may be visible outside the list view and that the dynamic creation and destruction of delegates outside the list view is visible to the user. To prevent this, clipping must be activated on the ListView element by setting the clip property to true. The illustration below shows the result of this (left view), compared to when the clip property is false (right view).

image

To the user, the ListView is a scrollable area. It supports kinetic scrolling, which means that it can be flicked to quickly move through the contents. By default, it also can be stretched beyond the end of contents, and then bounces back, to signal to the user that the end has been reached.

The behavior at the end of the view is controlled using the boundsBehavior property. This is an enumerated value and can be conimaged from the default behavior, Flickable.DragAndOvershootBounds, where the view can be both dragged and flicked outside its boundaries, to Flickable.StopAtBounds, where the view never will move outside its boundaries. The middle ground, Flickable.DragOverBounds lets the user drag the view outside its boundaries, but flicks will stop at the boundary.

It is possible to limit the positions where a view is allowed to stop. This is controlled using the snapMode property. The default behavior, ListView.NoSnap, lets the view stop at any position. By setting the snapMode property to ListView.SnapToItem, the view will always align the top of an item with its top. Finally, the ListView.SnapOneItem, the view will stop no more than one item from the first visible item when the mouse button or touch was released. The last mode is very handy when flipping through pages.

Orientation

The list view provides a vertically scrolling list by default, but horizontal scrolling can be just as useful. The direction of the list view is controlled through the orientation property. It can be set to either the default value, ListView.Vertical, or to ListView.Horizontal. A horizontal list view is shown below.

  1. import QtQuick 6.2
  2. import "../common"
  3. Background {
  4. width: 480
  5. height: 80
  6. ListView {
  7. anchors.fill: parent
  8. anchors.margins: 20
  9. spacing: 4
  10. clip: true
  11. model: 100
  12. orientation: ListView.Horizontal
  13. delegate: numberDelegate
  14. }
  15. Component {
  16. id: numberDelegate
  17. GreenBox {
  18. width: 40
  19. height: 40
  20. text: index
  21. }
  22. }
  23. }

image

As you can tell, the direction of the horizontal flows from the left to the right by default. This can be controlled through the layoutDirection property, which can be set to either Qt.LeftToRight or Qt.RightToLeft, depending on the flow direction.

Keyboard Navigation and Highlighting

When using a ListView in a touch-based setting, the view itself is enough. In a scenario with a keyboard, or even just arrow keys to select an item, a mechanism to indicate the current item is needed. In QML, this is called highlighting.

Views support a highlight delegate which is shown in the view together with the delegates. It can be considered an additional delegate, only that it is only instantiated once, and is moved into the same position as the current item.

In the example below this is demonstrated. There are two properties involved for this to work. First, the focus property is set to true. This gives the ListView the keyboard focus. Second, the highlight property is set to point out the highlighting delegate to use. The highlight delegate is given the x, y and height of the current item. If the width is not specified, the width of the current item is also used.

In the example, the ListView.view.width attached property is used for width. The attached properties available to delegates are discussed further in the delegate section of this chapter, but it is good to know that the same properties are available to highlight delegates as well.

  1. import QtQuick 6.2
  2. import "../common"
  3. Background {
  4. width: 240
  5. height: 300
  6. ListView {
  7. id: view
  8. anchors.fill: parent
  9. anchors.margins: 20
  10. clip: true
  11. model: 100
  12. delegate: numberDelegate
  13. spacing: 5
  14. highlight: highlightComponent
  15. focus: true
  16. }
  17. Component {
  18. id: highlightComponent
  19. GreenBox {
  20. width: ListView.view.width
  21. }
  22. }
  23. Component {
  24. id: numberDelegate
  25. Item {
  26. width: ListView.view.width
  27. height: 40
  28. Text {
  29. anchors.centerIn: parent
  30. font.pixelSize: 10
  31. text: index
  32. }
  33. }
  34. }
  35. }

image

When using a highlight in conjunction with a ListView, a number of properties can be used to control its behavior. The highlightRangeMode controls how the highlight is affected by what is shown in the view. The default setting, ListView.NoHighlightRange means that the highlight and the visible range of items in the view not being related at all.

The value ListView.StrictlyEnforceRange ensures that the highlight is always visible. If an action attempts to move the highlight outside the visible part of the view, the current item will change accordingly, so that the highlight remains visible.

The middle ground is the ListView.ApplyRange value. It attempts to keep the highlight visible but does not alter the current item to enforce this. Instead, the highlight is allowed to move out of view if necessary.

In the default configuration, the view is responsible for moving the highlight into position. The speed of the movement and resizing can be controlled, either as a velocity or as a duration. The properties involved are highlightMoveSpeed, highlightMoveDuration, highlightResizeSpeed and highlightResizeDuration. By default, the speed is set to 400 pixels per second, and the duration is set to -1, indicating that the speed and distance control the duration. If both a speed and a duration is set, the one that results in the quickest animation is chosen.

To control the movement of the highlight more in detail, the highlightFollowCurrentItem property can be set to false. This means that the view is no longer responsible for the movement of the highlight delegate. Instead, the movement can be controlled through a Behavior or an animation.

In the example below, the y property of the highlight delegate is bound to the ListView.view.currentItem.y attached property. This ensures that the highlight follows the current item. However, as we do not let the view move the highlight, we can control how the element is moved. This is done through the Behavior on y. In the example below, the movement is divided into three steps: fading out, moving, before fading in. Notice how SequentialAnimation and PropertyAnimation elements can be used in combination with the NumberAnimation to create a more complex movement.

  1. Component {
  2. id: highlightComponent
  3. Item {
  4. width: ListView.view.width
  5. height: ListView.view.currentItem.height
  6. y: ListView.view.currentItem.y
  7. Behavior on y {
  8. SequentialAnimation {
  9. PropertyAnimation { target: highlightRectangle; property: "opacity"; to: 0; duration: 200 }
  10. NumberAnimation { duration: 1 }
  11. PropertyAnimation { target: highlightRectangle; property: "opacity"; to: 1; duration: 200 }
  12. }
  13. }
  14. GreenBox {
  15. id: highlightRectangle
  16. anchors.fill: parent
  17. }
  18. }
  19. }

At each end of the ListView contents, a header and a footer element can be inserted. These can be considered special delegates placed at the beginning or end of the list. For a horizontal list, these will not appear at the head or foot, but rather at the beginning or end, depending on the layoutDirection used.

The example below illustrates how a header and footer can be used to enhance the perception of the beginning and end of a list. There are other uses for these special list elements. For instance, they can be used to keep buttons to load more contents.

  1. import QtQuick 6.2
  2. import "../common"
  3. Background {
  4. width: 240
  5. height: 300
  6. ListView {
  7. anchors.fill: parent
  8. anchors.margins: 20
  9. clip: true
  10. model: 4
  11. delegate: numberDelegate
  12. spacing: 2
  13. header: headerComponent
  14. footer: footerComponent
  15. }
  16. Component {
  17. id: headerComponent
  18. YellowBox {
  19. width: ListView.view.width
  20. height: 20
  21. text: 'Header'
  22. }
  23. }
  24. Component {
  25. id: footerComponent
  26. YellowBox {
  27. width: ListView.view.width
  28. height: 20
  29. text: 'Footer'
  30. }
  31. }
  32. Component {
  33. id: numberDelegate
  34. GreenBox {
  35. width: ListView.view.width
  36. height: 40
  37. text: 'Item #' + index
  38. }
  39. }
  40. }

TIP

Header and footer delegates do not respect the spacing property of a ListView, instead they are placed directly adjacent to the next item delegate in the list. This means that any spacing must be a part of the header and footer items.

image

The GridView

Using a GridView is very similar to using a ListView. The only real difference is that the grid view places the delegates in a two-dimensional grid instead of in a linear list.

image

Compared to a list view, the grid view does not rely on spacing and the size of its delegates. Instead, it uses the cellWidth and cellHeight properties to control the dimensions of the contents delegates. Each delegate item is then placed in the top left corner of each such cell.

  1. import QtQuick 2.5
  2. import "../common"
  3. Background {
  4. width: 220
  5. height: 300
  6. GridView {
  7. id: view
  8. anchors.fill: parent
  9. anchors.margins: 20
  10. clip: true
  11. model: 100
  12. cellWidth: 45
  13. cellHeight: 45
  14. delegate: numberDelegate
  15. }
  16. Component {
  17. id: numberDelegate
  18. GreenBox {
  19. width: 40
  20. height: 40
  21. text: index
  22. }
  23. }
  24. }

A GridView contains headers and footers, can use a highlight delegate and supports snap modes as well as various bounds behaviors. It can also be orientated in different directions and orientations.

The orientation is controlled using the flow property. It can be set to either GridView.LeftToRight or GridView.TopToBottom. The former value fills a grid from the left to the right, adding rows from the top to the bottom. The view is scrollable in the vertical direction. The latter value adds items from the top to the bottom, filling the view from left to right. The scrolling direction is horizontal in this case.

In addition to the flow property, the layoutDirection property can adapt the direction of the grid to left-to-right or right-to-left languages, depending on the value used.