【问题标题】:Adding dynamically to qml tabbar and stacklayout动态添加到 qml tabbar 和 stacklayout
【发布时间】:2019-12-09 19:30:42
【问题描述】:

我正在尝试在 qml 中创建一个标签页。我使用了与 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
      }
  }

这个代码可以很容易地动态添加一个新的tabButton:

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

其中 TTabButton 是一个单独的文件,由 TabButton 项组成。但我找不到任何方法将新页面添加到 StackLayout。它似乎应该是静态的。所以我的问题是如何在 qml 中动态分页?

【问题讨论】:

    标签: qt qml qt5 qtquick2


    【解决方案1】:

    你可以添加到 StackLayout 的子元素中:

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

    stackItem 是您添加到 StackLayout layout 的那些项目的组件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-31
      • 2021-10-30
      • 2017-10-06
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      相关资源
      最近更新 更多