Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED]Adding Tab in existing TabView from C++ side
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laumaya
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      laumaya
      wrote on last edited by
      #2

      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()));
      

      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mraynelson
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mraynelson
          wrote on last edited by
          #4

          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)));
          }
          @

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved