Container QML Type

Abstract base type providing functionality common to containers. More...

Import Statement: import QtQuick.Controls 2.3
Since: Qt 5.7
Inherits:

Control

Inherited By:

DialogButtonBox, MenuBar, SwipeView, and TabBar

Properties

Methods

Detailed Description

Container is the base type of container-like user interface controls that allow dynamic insertion and removal of items.

Using Containers

Typically, items are statically declared as children of Container, but it is also possible to add, insert, move and remove items dynamically. The items in a container can be accessed using itemAt() or contentChildren.

Most containers have the concept of a "current" item. The current item is specified via the currentIndex property, and can be accessed using the read-only currentItem property.

The following example illustrates dynamic insertion of items to a TabBar, which is one of the concrete implementations of Container.

Row {
    TabBar {
        id: tabBar

        currentIndex: 0
        width: parent.width - addButton.width

        TabButton { text: "TabButton" }
    }

    Component {
        id: tabButton
        TabButton { text: "TabButton" }
    }

    Button {
        id: addButton
        text: "+"
        flat: true
        onClicked: {
            tabBar.addItem(tabButton.createObject(tabBar))
            console.log("added:", tabBar.itemAt(tabBar.count - 1))
        }
    }
}

Managing the Current Index

When using multiple containers, such as TabBar and SwipeView, together, their currentIndex properties can be bound to each other to keep them in sync. When the user interacts with either container, its current index changes automatically propagate to the other container.

Notice, however, that assigning a currentIndex value in JavaScript removes the respective binding. In order to retain the bindings, use the following methods to alter the current index:

TabBar {
    id: tabBar
    currentIndex: swipeView.currentIndex
}

SwipeView {
    id: swipeView
    currentIndex: tabBar.currentIndex
}

Button {
    text: qsTr("Home")
    onClicked: swipeView.setCurrentIndex(0)
    enabled: swipeView.currentIndex != 0
}

Button {
    text: qsTr("Previous")
    onClicked: swipeView.decrementCurrentIndex()
    enabled: swipeView.currentIndex > 0
}

Button {
    text: qsTr("Next")
    onClicked: swipeView.incrementCurrentIndex()
    enabled: swipeView.currentIndex < swipeView.count - 1
}

Implementing Containers

Container does not provide any default visualization. It is used to implement such containers as SwipeView and TabBar. When implementing a custom container, the most important part of the API is contentModel, which provides the contained items in a way that it can be used as a delegate model for item views and repeaters.

Container {
    id: container

    contentItem: ListView {
        model: container.contentModel
        snapMode: ListView.SnapOneItem
        orientation: ListView.Horizontal
    }

    Text {
        text: "Page 1"
        width: container.width
        height: container.height
    }

    Text {
        text: "Page 2"
        width: container.width
        height: container.height
    }
}

Notice how the sizes of the page items are set by hand. This is because the example uses a plain Container, which does not make any assumptions on the visual layout. It is typically not necessary to specify sizes for items in concrete Container implementations, such as SwipeView and TabBar.

See also Container Controls.

Property Documentation

contentChildren : list<Item>

This property holds the list of content children.

The list contains all items that have been declared in QML as children of the container, and also items that have been dynamically added or inserted using the addItem() and insertItem() methods, respectively.

Note: Unlike contentData, contentChildren does not include non-visual QML objects. It is re-ordered when items are inserted or moved.

See also Item::children and contentData.


[default] contentData : list<Object>

This property holds the list of content data.

The list contains all objects that have been declared in QML as children of the container, and also items that have been dynamically added or inserted using the addItem() and insertItem() methods, respectively.

Note: Unlike contentChildren, contentData does include non-visual QML objects. It is not re-ordered when items are inserted or moved.

See also Item::data and contentChildren.


[read-only] contentModel : model

This property holds the content model of items.

The content model is provided for visualization purposes. It can be assigned as a model to a content item that presents the contents of the container.

Container {
    id: container
    contentItem: ListView {
        model: container.contentModel
    }
}

See also contentData and contentChildren.


[read-only] count : int

This property holds the number of items.


currentIndex : int

This property holds the index of the current item.

See also currentItem and Managing the Current Index.


[read-only] currentItem : Item

This property holds the current item.

See also currentIndex.


Method Documentation

void addItem(Item item)

Adds an item.


void decrementCurrentIndex()

Decrements the current index of the container.

This method can be called to alter the current index without breaking existing currentIndex bindings.

This QML method was introduced in QtQuick.Controls 2.1 (Qt 5.8).

See also currentIndex and Managing the Current Index.


void incrementCurrentIndex()

Increments the current index of the container.

This method can be called to alter the current index without breaking existing currentIndex bindings.

This QML method was introduced in QtQuick.Controls 2.1 (Qt 5.8).

See also currentIndex and Managing the Current Index.


void insertItem(int index, Item item)

Inserts an item at index.


Item itemAt(int index)

Returns the item at index, or null if it does not exist.


void moveItem(int from, int to)

Moves an item from one index to another.


void removeItem(Item item)

Removes and destroys the specified item.

This QML method was introduced in QtQuick.Controls 2.3 (Qt 5.10).


void setCurrentIndex(int index)

Sets the current index of the container.

This method can be called to set a specific current index without breaking existing currentIndex bindings.

See also currentIndex and Managing the Current Index.


Item takeItem(int index)

Removes and returns the item at index.

Note: The ownership of the item is transferred to the caller.

This QML method was introduced in QtQuick.Controls 2.3 (Qt 5.10).


© 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.