PDA

View Full Version : Creating dynamic tab in Qml TabView



volcano
11th March 2016, 10:16
I'm creating tabs dynamically in QML TabView on Component.onCompleted



var t = tabview.addTab("tab", Qt.createComponent("TabItem.qml"));

console.log("Creating Tabs " + t)
if(t != null)
{
var last = tabview.count-1;
tabview.getTab(last).active = true;
t.item.partsModel = model.getMenuModel(i)
}


The problem is that I need to set an objectName to the dynamic tab to set properties from Qt.
Can you suggest a method for the same?

anda_skoa
11th March 2016, 12:07
The object name is a property called objectName in QML.

However, "set properties from Qt" sounds like you are trying to manipulate a QML created object from C++.
There are usually better way than doing that.

Cheers,
_

volcano
13th March 2016, 05:36
Is it possible to set the objectName to a dynamically created tab like the way listed here?


QQuickItem* tab= view->rootObject()->findChild<QQuickItem*>("dynamicTab");

QQuickItem* tabInfoPanel = tab->findChild<QQuickItem*>("tabInfoPanel");

tabInfoPanel->setProperty("visible", false);

I get runtime error from the tab pointer saying it's not initialized.


owever, "set properties from Qt" sounds like you are trying to manipulate a QML created object from C++.
There are usually better way than doing that.
Can you suggest me them?

anda_skoa
13th March 2016, 12:58
Is it possible to set the objectName to a dynamically created tab like the way listed here?

It is a standard property, defined in QObject.
You can set it and read it like any other property.



Can you suggest me them?

The most common way is for C++ to expose values via QObject properties or as a list model and then bind to these values in QML.
This way you only have a dependency from QML to C++, usually QtQuick to C++, so GUI to core, not the other way around.

Cheers,
_

volcano
13th March 2016, 13:47
I understand that's it's better to keep it in the direction from C++ to QML and not make it bidirectional

However, the app has been designed with the requirement to be bidirectional to set certain properties.

Here's the code implementation but I still get this error, that tab pointer isn't initialized.



var last = tabview.count-1;
tabview.getTab(last).active = true;
t.item.partsModel = model.getMenuModel(i)
t.objectName = "tabItem"


To recover from Qt/C++, done in the overridden class using QQuickView


QQuickItem* tab= view->rootObject()->findChild<QQuickItem*>("tabItem");


Can you suggest what am I missing?

anda_skoa
13th March 2016, 14:33
I understand that's it's better to keep it in the direction from C++ to QML and not make it bidirectional

However, the app has been designed with the requirement to be bidirectional to set certain properties.

Ok, but it is often better to invest time in improving the design than to make the implementation of a bad design work.



Can you suggest what am I missing?
Maybe the tab isn't part of the QObject tree, QQuickItems don't necessarily have a QObject parent.

You could try manuyll recursing over the childItems() or calling a C++ function from QML when you have created the tab.

Cheers,
_

volcano
15th March 2016, 01:12
Thanks anda_skoa

Will try the suggestion.