简体   繁体   English

动态添加到qml tabbar和stacklayout

[英]Adding dynamically to qml tabbar and stacklayout

I'm trying to create a tabbed pages in qml. 我正在尝试在qml中创建标签页。 I used TabBar associated with StackLayout: 我使用了与StackLayout相关联的TabBar:

      TabBar {
      id: bar
      width: parent.width
      TabButton {
          text: qsTr("Home")
      }
      TabButton {
          text: qsTr("Discover")
      }
      TabButton {
          text: qsTr("Activity")
      }
  }

  StackLayout {
      width: parent.width
      currentIndex: bar.currentIndex
      Item {
          id: homeTab
      }
      Item {
          id: discoverTab
      }
      Item {
          id: activityTab
      }
  }

A new tabButton can be easily added by this code dynamically: 这个代码可以动态地添加一个新的tabButton:

        var tab = tabButton.createObject(TTabButton, {text: tabName});
        bar.addItem(tab);

which TTabButton is a separate file consisting TabButton item. 哪个TTabButton是一个单独的文件,包含TabButton项。 But I can't find any ways to add a new page to StackLayout. 但我找不到任何方法来向StackLayout添加新页面。 It seems it is supposed to be static. 它似乎应该是静态的。 So my question is how to have dynamic tab-paged in qml? 所以我的问题是如何在qml中使用动态tab-paged?

You can add to the children of StackLayout: 您可以添加到StackLayout的子项:

var item = stackItem.createObject(null, {id: "tabName"})
layout.children.push(item)

Where stackItem is the Component of those items you add to your StackLayout layout . 其中stackItem是您添加到StackLayout layout的那些项的Component。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM