Container QML Type

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

Import Statement: import QtQuick.Controls
Inherits:

Control

Inherited By:

DialogButtonBox, MenuBar, SplitView, 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 an object 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.


contentData : list<QtObject> [default]

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.


contentHeight : real [since QtQuick.Controls 2.5 (Qt 5.12)]

This property holds the content height. It is used for calculating the total implicit height of the container.

Unless explicitly overridden, the content height is automatically calculated based on the implicit height of the items in the container.

This property was introduced in QtQuick.Controls 2.5 (Qt 5.12).

See also contentWidth.


contentModel : model [read-only]

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.


contentWidth : real [since QtQuick.Controls 2.5 (Qt 5.12)]

This property holds the content width. It is used for calculating the total implicit width of the container.

Unless explicitly overridden, the content width is automatically calculated based on the implicit width of the items in the container.

This property was introduced in QtQuick.Controls 2.5 (Qt 5.12).

See also contentHeight.


count : int [read-only]

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.


currentItem : Item [read-only]

This property holds the current item.

See also currentIndex.


Method Documentation

void addItem(Item item)

Adds an item.


[since QtQuick.Controls 2.1 (Qt 5.8)] 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 method was introduced in QtQuick.Controls 2.1 (Qt 5.8).

See also currentIndex and Managing the Current Index.


[since QtQuick.Controls 2.1 (Qt 5.8)] 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 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.


[since QtQuick.Controls 2.3 (Qt 5.10)] void removeItem(Item item)

Removes and destroys the specified item.

This 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.


[since QtQuick.Controls 2.3 (Qt 5.10)] Item takeItem(int index)

Removes and returns the item at index.

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

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


© 2024 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.