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



  • 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!



  • Are you sure the ctor is called ?



  • 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.



  • 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



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



  • Sorry...i forgot it!


Log in to reply
 

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