Amir eas
Amir eas

Reputation: 131

How can set a Qwidget inside a QGraphicsWidget?

Is it possible that a QGraphicsWidget be a parent for QWidget? I have a QGraphicsItem and I want to add a QWidget inside this item, How can I set a QWidget in a QGraphicsItem or QGraphicsWidget that is a child of QGraphicsItem?

Upvotes: 1

Views: 4857

Answers (2)

TheDarkKnight
TheDarkKnight

Reputation: 27611

QWidget and QGraphicsWidget are very different. However, the QGraphics system provides a QGraphicsProxyWidget for embedding QWidget and QWidget-based items in a QGraphicsScene.

You can directly create the QGraphicsProxyWidget and call the function setWidget, before adding the QGraphicsProxyWidget to your QGraphicsScene: -

QGraphicsScene* pScene = new QGraphicsScene;
QWidget* pWidget = new QWidget;
QGraphicsProxyWidget* pProxy = new QGraphicsProxyWidget(parent); // parent can be NULL
pProxy->setWidget(pWidget);
pScene->addItem(pProxy);

The proxy widget can now be moved, scaled etc in the scene and the functionality of its QWidget will have signals passed through to it to work as expected.

Alternatively, the QGraphicsScene contains a shortcut function addWidget, which internally creates the QGraphicsProxyWidget for you and returns it from the function: -

QGraphicsProxyWidget* pProxy = pScene->addWidget(pWidget);

Upvotes: 4

Zaiborg
Zaiborg

Reputation: 2522

QGraphicsWidget is basically a QGraphicsItem for QWidgets ... it provides a layout you can use so it should be no problem at all

use: QGraphicsScene::addWidget( QWidget * widget, Qt::WindowFlags wFlags = 0 )

Upvotes: 5

Related Questions