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] Create qml object in C++ setting a parent
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Create qml object in C++ setting a parent

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 4 Posters 4.0k 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.
  • A Offline
    A Offline
    AlterX
    wrote on 1 Jun 2012, 10:46 last edited by
    #1

    Hi all,
    I've created a component like this (VisualObj.qml):
    @
    Item {
    Rectangle {
    ...
    }
    }
    @

    Now I need to create that object in C++ ...below my code:
    @
    QDeclarativeComponent component(this->rootContext->engine(), QUrl("qrc:/qml/VisualObj.qml"));

    object = component.create();
    object->setProperty("x", 110);
    object->setProperty("y", 250);
    object->setProperty("z", 100);
    object->setProperty("width", 90);
    @

    I call this code (as slot) in Component.onCompleted of a Page component...my problem is that the object I've created is not displayed
    in the Page!
    Ok, that is because I've not set the parent...I've just tried to get it as parameter of the slot (yes...I'm crazy) and it works: the parameter is a valid qml object and so I use that:
    @object->setProperty("parent", paramObj);@

    but nothing, even I've set the "visible" property of the new dynamic object!

    How can I resolve that? Is there a correct way to achieve what I want?

    thanks to all
    P.S. due to the big flexibility of QML and C++, and relatively new technology, I think that at least an official book is needed!

    Qt Ambassador
    Real-time cooperative teams: http://www.softairrealfight.net
    Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

    https://codereview.qt-project.org/...

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aabc
      wrote on 3 Jun 2012, 06:53 last edited by
      #2

      Are you sure the ctor is called ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dmcr
        wrote on 4 Jun 2012, 06:15 last edited by
        #3

        Hello,

        if the object has no height, it is likely that it will not be visible, so did you
        put @object->setProperty("height", 90);@ as well?

        On the other hand, depending on what you like to do, it may be intersting to consider creating your object in javascript as well, since you call the creation on Component.complete, ie in js.

        dmcr

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AlterX
          wrote on 4 Jun 2012, 09:29 last edited by
          #4

          Hi all,
          thank you for resposes...I've resolved it:
          1 - as dmcr said, object must be resized
          2 - the technique to pass qml object thru c++ slot function is valid and it works
          3 - when I create object in C++ is a good manner to convert it in QDeclarativeItem and not use QObject directly; so I've set parent with setParentItem() that seems to work very well then setParent()
          4 - I set parent relation (point 3) at the end of all other assignments (width, height and so on) otherwise the component isn't shown

          That's all :)

          Thank you for support
          Bye

          Qt Ambassador
          Real-time cooperative teams: http://www.softairrealfight.net
          Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

          https://codereview.qt-project.org/...

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Peppy
            wrote on 4 Jun 2012, 09:50 last edited by
            #5

            Prepend the title of thread with [SOLVED], thanks.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AlterX
              wrote on 4 Jun 2012, 10:05 last edited by
              #6

              Sorry...i forgot it!

              Qt Ambassador
              Real-time cooperative teams: http://www.softairrealfight.net
              Free Real-time network platform sdk: https://github.com/AlterX76/Solomon

              https://codereview.qt-project.org/...

              1 Reply Last reply
              0

              6/6

              4 Jun 2012, 10:05

              • Login

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