In order to show a neat feature of Graphics View, have a look at the following code snippet, which adds a widget to the scene:
QSpinBox *box = new QSpinBox; QGraphicsProxyWidget *proxyItem = new QGraphicsProxyWidget; proxyItem->setWidget(box); QGraphicsScene scene; scene.addItem(proxyItem); proxyItem->setScale(2); proxyItem->setRotation(45);
First we create a QSpinBox
and a QGraphicsProxyWidget
element, which act as containers for widgets and indirectly inherit QGraphicsItem.
Then we add the spin box to the the proxy widget by calling addWidget()
. The ownership of the spin box isn't transferred, but when QGraphicsProxyWidget
gets deleted, it calls delete
on all assigned widgets. We thus do not have to worry about that ourselves. The widget you add should be parentless and must not be shown elsewhere. After setting the widget to the proxy, you can treat the proxy widget like any other item. Next, we add it to the scene and apply a transformation for demonstration...