[Solved] Dynamically populate Qml TabView .
-
I'm trying to dynamically create tabs in Qml. This is my simple example of what I'm trying to do, but the code does not work. Well..sort of doesn't work. It creates the first tab correctly, but throws:
@ mainWindow.qml:20: TypeError: Cannot set property 'color' of null. @ .It seems I'm missing something in adding Tabs. What is wrong?
@import QtQuick 2.0
import QtQuick.Controls 1.0ApplicationWindow{
id:win
TabView{
id:tb
Component{
id:viewComp
Rectangle{
anchors.fill:parent
color:"black"
}
}
anchors.fill:parent
function loadTab(){
var t=addTab("x",viewComp)
t.item.color="blue" //line 20
}
MouseArea{
anchors.fill:parent
onClicked:tb.loadTab()
}
}}@
-
The problem is that you are assuming that the "item" is loaded already when you add the tab. The actual creation happens lazily when the tab becomes visible. It works for the first tab since that is already visible when you add it.
-
Wow... finally some answer. Thanks.
This should definitely be in some of the TabView or Tab docs IMHO.
-
Sorry to reactivate this topic. What was the fix to the above problem? How can you force to component to be created? Thank you very much!
-
You can set the active property of the Tab (inherited from Loader) to true. That will force its creation, and the "item" property will be available afterwards.
-
Alright...thank you very much!