在 QML 中动态添加 TabButton 到 TabBar

pra*_*ra7 3 qt qml qtquick2 qtquickcontrols qtquickcontrols2

我试图在按下按钮时动态地将 tabButton 添加到TabBar但我花了很多时间搜索但我不知道如何添加,下面是我正在处理的代码:

我的选项卡按钮.qml

import QtQuick 2.4
import QtQuick.Controls 2.2

Item
{
    property int BtnWidth:0
    property int BtnHeight:0
    property string BtnText: ""
    property bool isChecked : false

    TabButton
    {
        id:tabBtn
        text:BtnText
        width:BtnWidth
        height:BtnHeight

    }
}
Run Code Online (Sandbox Code Playgroud)

MainForm.qml

import QtQuick 2.4
import QtQuick.Controls 2.2

Rectangle
{
    Button
    {
        id:button
        width:100
        height:100
        anchors.top:parent.top
        text:qStr("Add")
        onClicked{
            //How to add logic here to add tab in below tabBar.
        }
    }
    TabBar
    {
        id:tabBar
        anchors.top:button.bottom
        width:500
        height:500
    }
}
Run Code Online (Sandbox Code Playgroud)

jpn*_*rmi 6

例子:

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    id: window
    width: 360
    height: 630
    visible: true

    header: TabBar {
        id: tabBar
    }

    Component {
        id: tabButton
        TabButton { }
    }

    Button {
        text: "Add"
        anchors.centerIn: parent
        onClicked: {
            var tab = tabButton.createObject(tabBar, {text: "Tab " + tabBar.count})
            tabBar.addItem(tab)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)