lnk
lnk

Reputation: 603

How to make tab's item to be created programmatically?

I dynamically add tabs to TabView and pass tab's item to c++ for futher processing. The problem is that method tabview.getTab(tabview.getTab(tabview.count-1).item) returns null for the which index is >0. Here is 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 tabview.getTab(index).item returns apropriate value if tab(index) was activated manually (by clicking with mouse on it). It seems like tab's item is created only when user firstly activate tab. So, how to make item to be created immediately after tab creation?

Upvotes: 3

Views: 2025

Answers (1)

Jairo
Jairo

Reputation: 886

Every tab in TableView is represented by a Tab component that inherits from Loader component. If you want force the Tab to load its contents, just set active property to true. This property controls when the component must 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);
    }

I hope this help you.

Upvotes: 5

Related Questions