Dynamically creating TabView tabs?



  • Right now in my app I load in a bunch of data into a container that is essentially a QMap<QString, QList<QVariant>>.
    The ideal behavior for my QML view would be to have a TabView with a Tab for each key in the QMap.

    As far as I know there isn't any model-like behavior for a TabView like there is with ListView so you either have to hard code the Tabs into your QML file, or use the addTab function.

    What would be the best way to connect the QMap to the TabView so that if I add an entry to the map it could create a new Tab and pass in the QList<QVariant> to the component?



  • Expose the C++ object which contains the map to QML. Whenever you add any key to Map, emit the signal. Handle this signal in QML and add the new tab using the addTab function.



  • QMap<QString, QList<QVariant>> is not something QML understands. You're limited to QVariantMap (JS dictionary) and QVariantList (JS array), so you'll have to refactor the model types a bit. Here's an example that creates tabs dynamically using Repeater and a simple JS array model:

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.3
    
    ApplicationWindow {
        width: 640
        height: 480
        visible: true
    
        TabView {
            anchors.fill: parent
            Repeater {
                model: ["Foo", "Bar", "Baz"]
                Tab {
                    title: modelData
                }
            }
        }
    }
    


  • @jpnurmi Oh neat, that's perfect!

    So then for the Repeater model I would need to transform my model type into a QVariantList that contains a QVariantMap that contains the title and whatever data the Tab content needs?

    Or is it be able to somehow interface with a map?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.