I have TabView in main.qml
TabView {
id: tabRoot
objectName: "tabRootObj"
}
My application creates new tab on every new incoming TCP connection. Following code create new tabs on demand (It based on this answer https://stackoverflow.com/a/27093137/3960195).
void addTab(QQmlApplicationEngine& engine) {
root_tab = engine.rootObjects().first()->findChild<QQuickItem*>(QStringLiteral("tabRootObj"));
QVariant new_tab;
QQmlComponent component(&engine, QUrl("qrc:/MyTab.qml");
QMetaObject::invokeMethod(root_tab, "addTab",
Q_RETURN_ARG(QVariant, new_tab),
Q_ARG(QVariant, QStringLiteral("Tab name")),
Q_ARG(QVariant, QVariant::fromValue(&component)));
}
With every TCP connection It also creates new instance of class ConnectionManager which contains some statistics (eg count of transferred bytes) accessible through properties.
//ConnectionManager.hpp
class ConnectionManager : public QObject
{
Q_OBJECT
public:
// ...
Q_PROPERTY(QString address READ address NOTIFY ipChanged)
Q_PROPERTY(int received READ received NOTIFY receivedChanged)
//...
}
//MyTab.qml
Item {
property string ip
property int received
...
}
What I need is bind those properties with properties inside MyTab.qml.
The problem is that the method TabView.addTab creates component of MyTab on it's own and I cannot inject concrete instance of ConnectionManager to its context. Also ConnectionManager doesn't exist until new connection is created so I cannot add it on the start of application through rootContext. How can I create bindings between this newly created objects?
It's my first project in QML so maybe there is a better "QML" way how to do it. In that case answer with showing the "right" way is also acceptable.
ConnectionManagerto the QMLaddTabfunction ? Also to limit the coupling between C++ and QML, I wouldn't add tabs from c++. I would expose a c++ model of the different connections and expose it to QML. Then I would use aRepeaterwithMyTabas a delegate. - GrecKoaddTabfunction has only two parameters which are title of the tab and constructed component (doc.qt.io/qt-5/qml-qtquick-controls-tabview.html#addTab-method). The repeater solution looks really great. I didn't know that you can useRepeaterinsideTabView. - QeekaddTab(...), where you create the component. - derMaddTabbut I still need somehow set to newly created tab associatedConnectionManager- Qeek