Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Dynamically creating TabView tabs?

Dynamically creating TabView tabs?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
tabviewqmlc++
4 Posts 3 Posters 2.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • matt4682M Offline
    matt4682M Offline
    matt4682
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • jpnurmiJ Offline
        jpnurmiJ Offline
        jpnurmi
        wrote on last edited by
        #3

        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
                    }
                }
            }
        }
        
        matt4682M 1 Reply Last reply
        3
        • jpnurmiJ jpnurmi

          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
                      }
                  }
              }
          }
          
          matt4682M Offline
          matt4682M Offline
          matt4682
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved