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. Adding a new component definition in a class method to the scene
Forum Updated to NodeBB v4.3 + New Features

Adding a new component definition in a class method to the scene

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 690 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.
  • S Offline
    S Offline
    silex92
    wrote on last edited by
    #1

    Hi,

    there's a class woman. It is registered in QML system.
    main.qml contains the object definition with a signal handler.
    The handler calls c++ method which should add a new button into the scene.

    I need to find the root object - the window in the main.qml to become a parent for the new button.
    How do you find a root object?

    Here's the method:
    @
    extern QQuickWindow *window;
    void woman::add_hip()
    {
    setProperty("hip", 87);

    QQmlEngine *engine = QtQml::qmlEngine(this);
    
    // 1. Define a QML component.
    QQmlComponent *readHipComp = new QQmlComponent(engine);
    readHipComp->setData("import QtQuick.Controls 1.2\n\
                         Button {\n\
                             anchors.top: addHipBtn.top\n\
                             anchors.left: addHipBtn.left\n\
                             anchors.margins: 3\n\
                             text: \"Hip value\"\n\
                             onClicked: {\n\
                                 msgDlg.text = woman.hip\n\
                                 msgDlg.open()\n\
                             }\
                         }", QUrl());
    // 2. Create the component instance.
    QQuickItem *readHipBtn = qobject_cast<QQuickItem *>(readHipComp->create());
    // 3. Add the instance to the scene.
    readHipBtn->setParentItem(qobject_cast<QQuickItem *>(engine->rootContext()->contextObject()));
    

    readHipBtn->setParentItem(window->contentItem());
    }
    @

    throws
    <Unknown File>: ReferenceError: addHipBtn is not defined
    <Unknown File>: ReferenceError: addHipBtn is not defined.

    It is added to GUI but it does not see other main.qml objects.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      If I'm not mistaken you can pass the engine object to the class and then find the root object there.
      @
      //main.qml
      Window {
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")
      }

      //main.cpp
      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
      QObject *rootObject= engine.rootObjects().first();
      //rootObject will point to Window
      @

      In your case you can either pass rootObject to your class or pass engine to the class so that you can find root element there.

      157

      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