StackView QML Type | Qt Quick Controls 2 5.7

Qt 5.7 | Qt Quick Controls 2 | QML Types | StackView QML Type

Qt 5.7.1 Reference Documentation

# StackView QML Type

Provides a stack-based navigation model. More...

Import Statement: | import QtQuick.Controls 2.0
Since: | Qt 5.7
Inherits: | Control

- List of all members, including inherited members

## Properties

- busy : bool
- currentItem : Item
- depth : int
- initialItem : var
- popEnter : Transition
- popExit : Transition
- pushEnter : Transition
- pushExit : Transition
- replaceEnter : Transition
- replaceExit : Transition

## Attached Properties

- index : int
- status : enumeration
- view : StackView

## Methods

- void clear()
- Item find(callback, behavior)
- Item get(index, behavior)
- Item pop(item, operation)
- Item push(item, properties, operation)
- Item replace(target, item, properties, operation)

## Detailed Description

StackView can be used with a set of inter-linked information pages. For example, an email application with separate views to list the latest emails, view a specific email, and list/view the attachments. The email list view is pushed onto the stack as users open an email, and popped out as they choose to go back.

The following snippet demonstrates a simple use case, where the `mainView` is pushed onto and popped out of the stack on relevant button click:

      ApplicationWindow {
          title: qsTr("Hello World")
          width: 640
          height: 480
          visible: true

          StackView {
              id: stack
              initialItem: mainView
              anchors.fill: parent
          }

          Component {
              id: mainView

              Row {
                  spacing: 10

                  Button {
                      text: "Push"
                      onClicked: stack.push(mainView)
                  }
                  Button {
                      text: "Pop"
                      enabled: stack.depth > 1
                      onClicked: stack.pop()

                  }
                  Text {
                      text: stack.depth
                  }
              }
          }
      }

## Using StackView in an Application

Using StackView in an application is as simple as adding it as a child to a Window. The stack is usually anchored to the edges of the window, except at the top or bottom where it might be anchored to a status bar, or some other similar UI component. The stack can then be used by invoking its navigation methods. The first item to show in the StackView is the one that was assigned to initialItem, or the topmost item if initialItem is not set.

## Basic Navigation

StackView supports three primary navigation operations: push(), pop(), and replace(). These correspond to classic stack operations where "push" adds an item to the top of a stack, "pop" removes the top item from the stack, and "replace" is like a pop followed by a push, which replaces the topmost item with the new item. The topmost item in the stack corresponds to the one that is currently visible on screen. Logically, "push" navigates forward or deeper into the application UI, "pop" navigates backward, and "replace" replaces the currentItem.

Sometimes, it is necessary to go back more than a single step in the stack. For example, to return to a "main" item or some kind of section item in the application. In such cases, it is possible to specify an item as a parameter for pop(). This is called an "unwind" operation, where the stack unwinds till the specified item. If the item is not found, stack unwinds until it is left with one item, which becomes the currentItem. To explicitly unwind to the bottom of the stack, it is recommended to use pop(null), although any non-existent item will do.

Given the stack [A, B, C]:

- push(D) => [A, B, C, D] - "push" transition animation between C and D
- pop() => [A, B] - "pop" transition animation between C and B
- replace(D) => [A, B, D] - "replace" transition between C and D
- pop(A) => [A] - "pop" transition between C and A

Note: When the stack is empty, a push() operation will not have a transition animation because there is nothing to transition from (typically on application start-up). A pop() operation on a stack with depth 1 or 0 does nothing. In such cases, the stack can be emptied using the clear() method.

## Deep Linking

Deep linking means launching an application into a particular state. For example, a newspaper application could be launched into showing a particular article, bypassing the topmost item. In terms of StackView, deep linking means the ability to modify the state of the stack, so much so that it is possible to push a set of items to the top of the stack, or to completely reset the stack to a given state.

The API for deep linking in StackView is the same as for basic navigation. Pushing an array instead of a single item adds all the items in that array to the stack. The transition animation, however, is applied only for the last item in the array. The normal semantics of push() apply for deep linking, that is, it adds whatever is pushed onto the stack.

Note: Only the last item of the array is loaded. The rest of the items are loaded only when needed, either on subsequent calls to pop or on request to get an item using get().

This gives us the following result, given the stack [A, B, C]:

- push([D, E, F]) => [A, B, C, D, E, F] - "push" transition animation between C and F
- replace([D, E, F]) => [A, B, D, E, F] - "replace" transition animation between C and F
- clear() followed by push([D, E, F]) => [D, E, F] - no transition animation for pushing items as the stack was empty.

## Finding Items

An Item for which the application does not have a reference can be found by calling find(). The method needs a callback function, which is invoked for each item in the stack (starting at the top) until a match is found. If the callback returns `true`, find() stops and returns the matching item, otherwise `null` is returned.

The code below searches the stack for an item named "order_id" and unwinds to that item.

      stackView.pop(stackView.find(function(item) {
          return item.name == "order_id";
      }));

You can also get to an item in the stack using get(index).

      previousItem = stackView.get(myItem.StackView.index - 1));

## Transitions

For each push or pop operation, different transition animations are applied to entering and exiting items. These animations define how the entering item should animate in, and the exiting item should animate out. The animations can be customized by assigning different Transitions for the pushEnter, pushExit, popEnter, popExit, replaceEnter, and replaceExit properties of StackView.

Note: The transition animations affect each others' transitional behavior. Customizing the animation for one and leaving the other may give unexpected results.

The following snippet defines a simple fade transition for push and pop operations:

      StackView {
          id: stackview
          anchors.fill: parent

          pushEnter: Transition {
              PropertyAnimation {
                  property: "opacity"
                  from: 0
                  to:1
                  duration: 200
              }
          }
          pushExit: Transition {
              PropertyAnimation {
                  property: "opacity"
                  from: 1
                  to:0
                  duration: 200
              }
          }
          popEnter: Transition {
              PropertyAnimation {
                  property: "opacity"
                  from: 0
                  to:1
                  duration: 200
              }
          }
          popExit: Transition {
              PropertyAnimation {
                  property: "opacity"
                  from: 1
                  to:0
                  duration: 200
              }
          }
      }

Note: Using anchors on the items added to a StackView is not supported. Typically push, pop, and replace transitions animate the position, which is not possible when anchors are applied. Notice that this only applies to the root of the item. Using anchors for its children works as expected.

See also Customizing StackView, Navigation Controls, and Container Controls.

## Property Documentation

[read-only] busy : bool

This property holds whether a transition is running.

[read-only] currentItem : Item

This property holds the current top-most item in the stack.

[read-only] depth : int

This property holds the number of items currently pushed onto the stack.

initialItem : var

This property holds the initial item that should be shown when the StackView is created. The initial item can be an Item, Component, or a url. Specifying an initial item is equivalent to:

      Component.onCompleted: stackView.push(myInitialItem)

See also push().

popEnter : Transition

This property holds the transition that is applied to the item that enters the stack when another item is popped off of it.

See also Customizing StackView.

popExit : Transition

This property holds the transition that is applied to the item that exits the stack when the item is popped off of it.

See also Customizing StackView.

pushEnter : Transition

This property holds the transition that is applied to the item that enters the stack when the item is pushed onto it.

See also Customizing StackView.

pushExit : Transition

This property holds the transition that is applied to the item that exits the stack when another item is pushed onto it.

See also Customizing StackView.

replaceEnter : Transition

This property holds the transition that is applied to the item that enters the stack when another item is replaced by it.

See also Customizing StackView.

replaceExit : Transition

This property holds the transition that is applied to the item that exits the stack when it is replaced by another item.

See also Customizing StackView.

## Attached Property Documentation

[read-only] StackView.index : int

This attached property holds the stack index of the item it's attached to, or `-1` if the item is not in a stack.

[read-only] StackView.status : enumeration

This attached property holds the stack status of the item it's attached to, or `StackView.Inactive` if the item is not in a stack.

Available values:

Constant | Description
--- | ---
`StackView.Inactive` | The item is inactive (or not in a stack).
`StackView.Deactivating` | The item is being deactivated (popped off).
`StackView.Activating` | The item is being activated (becoming the current item).
`StackView.Active` | The item is active, that is, the current item.

[read-only] StackView.view : StackView

This attached property holds the stack view of the item it's attached to, or `null` if the item is not in a stack.

## Method Documentation

void clear()

Removes all items from the stack. No animations are applied.

Item find(callback, behavior)

Search for a specific item inside the stack. The callback function is called for each item in the stack (with the item and index as arguments) until the callback function returns `true`. The return value is the item found. For example:

      stackView.find(function(item, index) {
          return item.isTheOne
      })

Supported behavior values:

Constant | Description
--- | ---
`StackView.DontLoad` | Unloaded items are skipped (the callback function is not called for them).
`StackView.ForceLoad` | Unloaded items are forced to load.

Item get(index, behavior)

Returns the item at position index in the stack, or `null` if the index is out of bounds.

Supported behavior values:

Constant | Description
--- | ---
`StackView.DontLoad` | The item is not forced to load (and `null` is returned if not yet loaded).
`StackView.ForceLoad` | The item is forced to load.

Item pop(item, operation)

Pops one or more items off the stack. Returns the last item removed from the stack.

If the item argument is specified, all items down to (but not including) item will be popped. If item is `null`, all items down to (but not including) the first item is popped. If not specified, only the current item is popped.

An operation can be optionally specified as the last argument. Supported operations:

Constant | Description
--- | ---
`StackView.Transition` | An operation with transitions.
`StackView.Immediate` | An immediate operation without transitions.

Examples:

      stackView.pop()
      stackView.pop(someItem, StackView.Immediate)
      stackView.pop(StackView.Immediate)
      stackView.pop(null)

See also clear().

Item push(item, properties, operation)

Pushes an item onto the stack using the specified operation, and optionally applies a set of properties on the item. The item can be an Item, Component, or a url. Returns the item that became current.

StackView creates an instance automatically if the pushed item is a Component, or a url. The optional properties argument specifies a map of initial property values for the pushed item. For dynamically created items, these values are applied before the creation is finalized. This is more efficient than setting property values after creation, particularly where large sets of property values are defined, and also allows property bindings to be set up (using Qt.binding()) before the item is created.

Pushing a single item:

      stackView.push(rect)

      // or with properties:
      stackView.push(rect, {"color": "red"})

Multiple items can be pushed at the same time either by passing them as additional arguments, or as an array. The last item becomes the current item. Each item can be followed by a set of properties to apply.

Passing a variable amount of arguments:

      stackView.push(rect1, rect2, rect3)

      // or with properties:
      stackView.push(rect1, {"color": "red"}, rect2, {"color": "green"}, rect3, {"color": "blue"})

Pushing an array of items:

      stackView.push([rect1, rect2, rect3])

      // or with properties:
      stackView.push([rect1, {"color": "red"}, rect2, {"color": "green"}, rect3, {"color": "blue"}])

An operation can be optionally specified as the last argument. Supported operations:

Constant | Description
--- | ---
`StackView.Transition` | An operation with transitions.
`StackView.Immediate` | An immediate operation without transitions.

See also initialItem.

Item replace(target, item, properties, operation)

Replaces one or more items on the stack with the specified item and operation, and optionally applies a set of properties on the item. The item can be an Item, Component, or a url. Returns the item that became current.

If the target argument is specified, all items down to the item will be replaced. If target is `null`, all items in the stack will be replaced. If not specified, only the top item will be replaced.

StackView creates an instance automatically if the replacing item is a Component, or a url. The optional properties argument specifies a map of initial property values for the replacing item. For dynamically created items, these values are applied before the creation is finalized. This is more efficient than setting property values after creation, particularly where large sets of property values are defined, and also allows property bindings to be set up (using Qt.binding()) before the item is created.

Replace the top item:

      stackView.replace(rect)

      // or with properties:
      stackView.replace(rect, {"color": "red"})

Multiple items can be replaced at the same time either by passing them as additional arguments, or as an array. Each item can be followed by a set of properties to apply.

Passing a variable amount of arguments:

      stackView.replace(rect1, rect2, rect3)

      // or with properties:
      stackView.replace(rect1, {"color": "red"}, rect2, {"color": "green"}, rect3, {"color": "blue"})

Replacing an array of items:

      stackView.replace([rect1, rect2, rect3])

      // or with properties:
      stackView.replace([rect1, {"color": "red"}, rect2, {"color": "green"}, rect3, {"color": "blue"}])

An operation can be optionally specified as the last argument. Supported operations:

Constant | Description
--- | ---
`StackView.Transition` | An operation with transitions.
`StackView.Immediate` | An immediate operation without transitions.

See also push().