How do I create a tab element to be created programmatically?

I dynamically add tabs to TabView

and pass the tab element to C ++ for further processing. The problem is that the method tabview.getTab(tabview.getTab(tabview.count-1).item)

returns null

for which the index is> 0. Here is the code:

//main.qml
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    signal tabAdded(variant c)
    ColumnLayout{
        anchors.fill: parent
        TabView{
            visible: true
            id:tabview
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Button{
            text: "add tab"
            onClicked:{
                var c = Qt.createComponent("Tab.qml");
                tabview.addTab("tab", c)
                // tabAdded(tabview.getTab(tabview.count-1).item)
                console.log(tabview.getTab(tabview.count-1).item)
        }
     }
  }

}

//Tab.qml
import QtQuick 2.0
import QtQuick.Controls 1.1

Item{
    signal tabButtonClicked()
    anchors.fill: parent
    Button{
       text: "tabButton"
       anchors.centerIn: parent
       onClicked: tabButtonClicked()
    }

}

      

I figured out that it tabview.getTab(index).item

returns the appropriate value if the tab (index) was manually activated (by clicking on it). It seems that the tab element is only created when the user first activates the tab. So how do you create an element right after creating a tab?

+3


source to share


1 answer


Each tab in the TableView is represented by a Tab component that inherits from the Loader component . If you want to force Tab to load its contents, just set the active property to true. This property controls when the component should be loaded. So now your button code looks like this:

Button{
    text: "add tab"
    onClicked:{
        var c = Qt.createComponent("Tab.qml");
        tabview.addTab("tab", c);
        var last = tabview.count-1;
        tabview.getTab(last).active = true; // Now the content will be loaded
        console.log(tabview.getTab(last).item);
    }

      



Hope this helps you.

+5


source







All Articles