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 add tabs - declarative approach?
Forum Updated to NodeBB v4.3 + New Features

Dynamically add tabs - declarative approach?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 490 Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Bob64
    wrote on last edited by
    #1

    I need to extend a tab view that currently has a fixed number of tabs. It is implemented using TabBar and StackLayout. The buttons and stack items are just coded directly as nested QML items. These existing tabs all show very different things (e.g. a list view in one, a chart in another, etc.).

    For the new behaviour, new tabs can appear dynamically at certain times. The new tabs are of a homogenous type (essentially they represent a collection of text logs), but how many there are, and details such as their labelling, are not known until run time.

    I have been able to get this to work using an imperative approach. I have written a JS function that in response to relevant events dynamically creates the new buttons and the new stacked items and adds them to the respective containers. However, I am wondering if there is a cleaner way to do this more declaratively.

    I wondered if a Repeater together with some model might work. I haven't had cause to use Repeater before so have no experience with it. Reading the document, I am somewhat put off by this statement: "The Repeater type creates all of its delegate items when the repeater is first created." Does this mean that it cannot dynamically adjust itself to a changing model? That would seem to limit its applicability, but it is possible that I have misunderstood what it means.

    Any thoughts on using Repeater, or any other suggestions, would be welcomed. Thanks.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      The Repeater is the right answer. The Repeater can change and grow as things are added to the model.
      You can test this out easily by using a ListModel in your QML code and programmatically adding and removing entries to the model. If you want help then create a barebones app in a test project and put together just the code for this. Then share that main.qml (and other files) here. We can help you test this out.

      C++ is a perfectly valid school of magic.

      B 1 Reply Last reply
      1
      • fcarneyF fcarney

        The Repeater is the right answer. The Repeater can change and grow as things are added to the model.
        You can test this out easily by using a ListModel in your QML code and programmatically adding and removing entries to the model. If you want help then create a barebones app in a test project and put together just the code for this. Then share that main.qml (and other files) here. We can help you test this out.

        B Offline
        B Offline
        Bob64
        wrote on last edited by
        #3

        @fcarney thanks. I think I can see how to do it if it is the right way to go. It was that line of documentation I mentioned that threw me and made me doubt my understanding.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          Models in QML depend upon notification.

          This will update when changing things in the model because it has notification signals:

          ListModel {
              id: lmodel
          }
          Repeater {
            model:lmodel
          }
          

          This will not if you try and modify vmodel elements (unless the change causes the vmodel variable itself to change):

          property var vmodel: [1,2,3]
          
          Repeater {
            model: vmodel
          }
          

          So code like vmodel.push(4) won't provide signals to change for the repeater to see.

          C++ is a perfectly valid school of magic.

          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