Search code examples
qtwidgetqt4qgraphicsview

Adding a QWidget to a QGraphicsScene


I have a QGraphicsScene for drawing, where I now want to "add" a QWidget to a QGraphicsItem (display on top of the item, which can of course be moved).

How could this be accomplished? Is there any QGraphicsItem, which may function as a Widget container?


Solution

  • You can use QGraphicsScene::addWidget which creates a new QGraphicsProxyWidget for widget, adds it to the scene, and returns a pointer to the proxy :

    QGraphicsProxyWidget * item = myScene->addWidget(myWidget);
    item->setParentItem(anOtherItem);
    item->setPos(100,100);
    item->setZValue(1);