Extending QML - When is the parent set?



  • I'm writing a QML C++ extension class and need to do something with the parent during initialization of my object. I have noticed that in the constructor parent is null, but sometime later parent is set. I am trying to find something I can connect to that will tell me either when the parent is set or right before the Componet.onCompleted signal is emitted.

    Looking through the qt source it looks like I can override componentComplete if I inherit from QDeclarativeItem, but I am not creating an item that needs to be draw so I inherit from QObject. Is it possible to do this inheriting from QObject or do I have to inherit from QDeclarativeItem, or is there another way of doing this?



  • Have you look around setParent function? Maybe you can overload it.



  • setParent isn't a virtual functions so overriding it won't work in this case.



  • QDeclarativeItem is not drawn by default unless you set "ItemHasNoContenst" like this somewhere in your constructor :
    setFlag(QGraphicsItem::ItemHasNoContents, false);



  • [quote author="Tipiak" date="1300290488"]QDeclarativeItem is not drawn by default unless you set "ItemHasNoContenst" like this somewhere in your constructor :
    setFlag(QGraphicsItem::ItemHasNoContents, false);
    [/quote]

    As far as I know, the ItemHasNoContents flag is not set by default. By default, an item will be drawn, or at least QML/GraphicsView will try to do that.



  • "from the QDeclarativeItem doc":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeitem.html#details

    bq. "Note that, because it does not draw anything, QDeclarativeItem sets the QGraphicsItem::ItemHasNoContents flag. If you subclass QDeclarativeItem to create a visual item, you will need to unset this flag."

    It's true for the QGrahpicItem, but as far as i understand this flag is set to true by default for QDeclarativItem.



  • [quote author="Tipiak" date="1300292916"]"from the QDeclarativeItem doc":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeitem.html#details

    bq. "Note that, because it does not draw anything, QDeclarativeItem sets the QGraphicsItem::ItemHasNoContents flag. If you subclass QDeclarativeItem to create a visual item, you will need to unset this flag."

    It's true for the QGrahpicItem, but as far as i understand this flag is set to true by default for QDeclarativItem. [/quote]
    Ah, you are right, I stand corrected.



  • [quote author="bundickt" date="1299870489"]
    Looking through the qt source it looks like I can override componentComplete if I inherit from QDeclarativeItem, but I am not creating an item that needs to be draw so I inherit from QObject. Is it possible to do this inheriting from QObject or do I have to inherit from QDeclarativeItem, or is there another way of doing this?[/quote]

    You can do the same thing as QDeclarativeItem for your custom QObject-derived class, you'll just need to inherit "QDeclarativeParserStatus":http://doc.qt.nokia.com/4.7/qdeclarativeparserstatus.html

    Regards,
    Michael


  • Moderators

    There is something like QDeclarativeParserStatus which has some methods which might help here.



  • The itemchange() method informs you when the parent is set.

    @RenderWindow::itemChange( ItemChange change, const ItemChangeData& value )
    {
    switch ( change )
    {
    case QQuickItem::ItemParentHasChanged:
    // value.item and parent() both contain the new parent
    }
    }@


Log in to reply
 

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