[SOLVED]Adding Tab in existing TabView from C++ side



  • Hello,
    I have a TabView defined in QML file :
    @import QtQuick 2.0
    import QtQuick.Controls 1.1
    Item {
    id : root
    width: 300
    height: 300

    TabView {
        id : tabView
        objectName: "tabView"
        anchors.fill: parent
    }
    

    }
    @

    And I would like to add, remove tabs from C++ side.
    To retrieve the tabview I use this code :
    @ QObject* pRoot = QQuickView::rootObject();
    m_pTabView= pRoot->findChild<QObject*>("tabView");
    @

    But, I don't know how to access TabView methods.

    Thanks.



  • Solution found
    @ QVariant returnedValue;
    QVariant title = "Hello";

    QMetaObject::invokeMethod(m_pTabView, "addTab",
            Q_RETURN_ARG(QVariant, returnedValue),
            Q_ARG(QVariant, title), Q_ARG(QVariant, QVariant()));
    

    @



  • The documentation for addTab shows it takes an optional component in the second argument. Is it possible to provide the component using your method? It would seem you would need to pass a QQuickItem* as a QVariant, or pass a QQuickItem* direct, rather than QVariant. I could not get either of these approaches to work.



  • To pass component, pass pointer to QQmlComponent inside fromValue(), as follows...
    @ QObject* m_pTabView = findChild<QQuickItem >("myTabView");
    if (m_pTabView)
    {
    QVariant returnedValue;
    QVariant title = "Hello";
    QQmlComponent
    component = new QQmlComponent(&app.qmlEngine(), QUrl("qrc:/qml/Measurements.qml"), this);
    QMetaObject::invokeMethod(m_pTabView, "addTab",
    Q_RETURN_ARG(QVariant, returnedValue),
    Q_ARG(QVariant, title),
    Q_ARG(QVariant, QVariant::fromValue(component)));
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.