Accessing "parent"-property of GraphicsItem fails

  • I'm having a hard time figuring out, how to use the property-(s/g)etters to change the parentItem of a QGraphicsObject or QDeclarativeItem.

    The following little program will make my point:
    #include <QApplication>
    #include <QGraphicsView>
    #include <QGraphicsScene>
    #include <QGraphicsWidget>
    #include <QDeclarativeEngine>
    #include <QDeclarativeComponent>
    #include <QDeclarativeItem>
    #include <QDebug>

    int main( int argNum , char** arg )
    QApplication app( argNum , arg );
    QGraphicsScene scene( QRectF(0,0,200,200) );
    QGraphicsWidget* rootItem = new QGraphicsWidget;

    QDeclarativeEngine engine;
    QDeclarativeComponent qml( &engine );
    qml.setData( "import QtQuick 1.1; Rectangle { width: 100; height: 100; color:'red' }", QUrl() );
    QGraphicsObject* qmlItem = qobject_cast<QGraphicsObject*>(qml.create());
    qmlItem->setParentItem( rootItem );
    //qmlItem->setProperty( "parent", QVariant::fromValue(qobject_cast<QObject*>(rootItem)) );
    //qmlItem->setProperty( "parent", QVariant::fromValue(qobject_cast<QGraphicsItem*>(rootItem)) );
    //qmlItem->setProperty( "parent", QVariant::fromValue(qobject_cast<QGraphicsObject*>(rootItem)) );
    //qmlItem->setProperty( "parent", QVariant::fromValue(qobject_cast<QDeclarativeItem*>(rootItem)) );

    scene.addItem( rootItem );
    QGraphicsView view( &scene );;
    return app.exec();

    As noted in the source, setting the parent item of qmlItem directly works as expected. But trying to set via the parent property does not work in any of the version I tried above.

    What am I'm doing wrong?

Log in to reply

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