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. How can I create qml dynamically with c++ and preverse the context of root [SOLVED]

How can I create qml dynamically with c++ and preverse the context of root [SOLVED]

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.9k Views
  • 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.
  • J Offline
    J Offline
    jonatanp
    wrote on last edited by
    #1

    I create QDeclarativeItem dynamically with c++ that I attach to my root but the context of each is diferente

    @
    QDeclarativeComponent* component = new QDeclarativeComponent(_view->engine(), "image.qml");
    QDeclarativeItem* item = qobject_cast<QDeclarativeItem*>(component->create());
    _view->scene()->addItem(item);
    item->setParentItem(qobject_cast<QDeclarativeItem*>(_view->rootObject()));
    qWarning() << " context for root: " << qmlContext(_view->rootObject());
    qWarning() << " context for image " << qmlContext(item);
    @
    the result is:
    context for root: QDeclarativeContext(0x13e54a0)
    context for image: QDeclarativeContext(0x13e3ea0)

    I also can't find the item on root with this function
    @if( _view->rootObject()->findChild<QDeclarativeView*>("image1"))@

    _view is my QDeclarativeView and "image1" is the id of my qml file

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tolszak
      wrote on last edited by
      #2

      Contexts will be different but you should be able to see contextProperties set in rootContext in image context.

      Be aware that findChild takes objectName as argument not id. You should set
      @
      Image {
      id:image1
      objectName:"image1"
      }
      @

      in yor image.qml file and everything should work as expected.
      You should also add:
      @
      item->setParent(_view->rootObject());
      @
      to your code ( http://doc.qt.digia.com/qt/qdeclarativeengine.html#ObjectOwnership-enum )

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jonatanp
        wrote on last edited by
        #3

        I added the line
        @item->setParent(_view->rootObject());@
        and now it's work.

        the two function, setParent and setParentItem are necessary, the first is for the item appears on hierarchy of parents and the seconds function to has link on stage to the parent (eg with the change of opacity)

        now I have an another problem, this is my qml file
        @Image {
        id: image3
        objectName: "image1"
        width: 150
        height: 150
        anchors.bottom: parent.bottom
        source: "5.png"
        }@

        the error is: Unable to assign undefined value
        but the image appers on the correct position. I think this error is because the qml don't know who is parent, because the serParent is after the create component.
        I can put the id of parent instead of "parent" into qml file but the error is worse
        "ReferenceError: Can't find variable: main" mais is id of my main qml and nothing apper.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tolszak
          wrote on last edited by
          #4

          try using this:
          @
          Image {
          id: image3
          objectName: "image1"
          width: 150
          height: 150

          onParentChanged: {
          if(parent) {
          anchors.bottom = parent.bottom
          }
          }
          source: "5.png"
          }
          @

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jonatanp
            wrote on last edited by
            #5

            It's work, even with the id everything works.
            Thanks

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tolszak
              wrote on last edited by
              #6

              Could you please add [SOLVED] to thread title?

              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