|  | Home · All Classes · Modules

# QGraphicsProxyWidget Class Reference
[QtGui module]

The QGraphicsProxyWidget class provides a proxy layer for embedding a QWidget in a QGraphicsScene. More...

Inherits QGraphicsWidget.

### Methods

- __init__ (self, QGraphicsItem parent = None, Qt.WindowFlags wFlags = 0)
- contextMenuEvent (self, QGraphicsSceneContextMenuEvent event)
- QGraphicsProxyWidget createProxyForChildWidget (self, QWidget child)
- dragEnterEvent (self, QGraphicsSceneDragDropEvent event)
- dragLeaveEvent (self, QGraphicsSceneDragDropEvent event)
- dragMoveEvent (self, QGraphicsSceneDragDropEvent event)
- dropEvent (self, QGraphicsSceneDragDropEvent event)
- bool event (self, QEvent event)
- bool eventFilter (self, QObject object, QEvent event)
- focusInEvent (self, QFocusEvent event)
- bool focusNextPrevChild (self, bool next)
- focusOutEvent (self, QFocusEvent event)
- grabMouseEvent (self, QEvent event)
- hideEvent (self, QHideEvent event)
- hoverEnterEvent (self, QGraphicsSceneHoverEvent event)
- hoverLeaveEvent (self, QGraphicsSceneHoverEvent event)
- hoverMoveEvent (self, QGraphicsSceneHoverEvent event)
- QVariant itemChange (self, QGraphicsItem.GraphicsItemChange change, QVariant value)
- keyPressEvent (self, QKeyEvent event)
- keyReleaseEvent (self, QKeyEvent event)
- mouseDoubleClickEvent (self, QGraphicsSceneMouseEvent event)
- mouseMoveEvent (self, QGraphicsSceneMouseEvent event)
- mousePressEvent (self, QGraphicsSceneMouseEvent event)
- mouseReleaseEvent (self, QGraphicsSceneMouseEvent event)
- QGraphicsProxyWidget newProxyWidget (self, QWidget)
- paint (self, QPainter painter, QStyleOptionGraphicsItem option, QWidget widget)
- resizeEvent (self, QGraphicsSceneResizeEvent event)
- setGeometry (self, QRectF rect)
- setWidget (self, QWidget widget)
- showEvent (self, QShowEvent event)
- QSizeF sizeHint (self, Qt.SizeHint which, QSizeF constraint = QSizeF())
- QRectF subWidgetRect (self, QWidget widget)
- int type (self)
- ungrabMouseEvent (self, QEvent event)
- wheelEvent (self, QGraphicsSceneWheelEvent event)
- QWidget widget (self)

## Detailed Description

The QGraphicsProxyWidget class provides a proxy layer for embedding a QWidget in a QGraphicsScene.

QGraphicsProxyWidget embeds QWidget-based widgets, for example, a QPushButton, QFontComboBox, or even QFileDialog, into QGraphicsScene. It forwards events between the two objects and translates between QWidget's integer-based geometry and QGraphicsWidget's qreal-based geometry. QGraphicsProxyWidget supports all core features of QWidget, including tab focus, keyboard input, Drag & Drop, and popups. You can also embed complex widgets, e.g., widgets with subwidgets.

Example:

     int main(int argc, char **argv)
     {
         QApplication app(argc, argv);

         QTabWidget *tabWidget = new QTabWidget;

         QGraphicsScene scene;
         QGraphicsProxyWidget *proxy = scene.addWidget(tabWidget);

         QGraphicsView view(&scene);
         view.show();

         return app.exec();
     }

QGraphicsProxyWidget takes care of automatically embedding popup children of embedded widgets through creating a child proxy for each popup. This means that when an embedded QComboBox shows its popup list, a new QGraphicsProxyWidget is created automatically, embedding the popup, and positioning it correctly.

### Embedding a Widget with QGraphicsProxyWidget

There are two ways to embed a widget using QGraphicsProxyWidget. The most common way is to pass a widget pointer to QGraphicsScene.addWidget() together with any relevant Qt.WindowFlags. This function returns a pointer to a QGraphicsProxyWidget. You can then choose to reparent or position either the proxy, or the embedded widget itself.

For example, in the code snippet below, we embed a group box into the proxy:

     QGroupBox *groupBox = new QGroupBox("Contact Details");
     QLabel *numberLabel = new QLabel("Telephone number");
     QLineEdit *numberEdit = new QLineEdit;

     QFormLayout *layout = new QFormLayout;
     layout->addRow(numberLabel, numberEdit);
     groupBox->setLayout(layout);

     QGraphicsScene scene;
     QGraphicsProxyWidget *proxy = scene.addWidget(groupBox);

     QGraphicsView view(&scene);
     view.show();

The image below is the output obtained with its contents margin and contents rect labeled.

Alternatively, you can start by creating a new QGraphicsProxyWidget item, and then call setWidget() to embed a QWidget later. The widget() function returns a pointer to the embedded widget. QGraphicsProxyWidget shares ownership with QWidget, so if either of the two widgets are destroyed, the other widget will be automatically destroyed as well.

### Synchronizing Widget States

QGraphicsProxyWidget keeps its state in sync with the embedded widget. For example, if the proxy is hidden or disabled, the embedded widget will be hidden or disabled as well, and vice versa. When the widget is embedded by calling addWidget(), QGraphicsProxyWidget copies the state from the widget into the proxy, and after that, the two will stay synchronized where possible. By default, when you embed a widget into a proxy, both the widget and the proxy will be visible because a QGraphicsWidget is visible when created (you do not have to call show()). If you explicitly hide the embedded widget, the proxy will also become invisible.

Example:

     QGraphicsScene scene;

     QLineEdit *edit = new QLineEdit;
     QGraphicsProxyWidget *proxy = scene.addWidget(edit);

     edit->isVisible();  // returns true
     proxy->isVisible(); // also returns true

     edit->hide();

     edit->isVisible();  // returns false
     proxy->isVisible(); // also returns false

QGraphicsProxyWidget maintains symmetry for the following states:

QWidget state | QGraphicsProxyWidget state | Notes
--- | --- | ---
QWidget.enabled | QGraphicsProxyWidget.enabled |
QWidget.visible | QGraphicsProxyWidget.visible | The explicit state is also symmetric.
QWidget.geometry | QGraphicsProxyWidget.geometry | Geometry is only guaranteed to be symmetric while the embedded widget is visible.
QWidget.layoutDirection | QGraphicsProxyWidget.layoutDirection |
QWidget.style | QGraphicsProxyWidget.style |
QWidget.palette | QGraphicsProxyWidget.palette |
QWidget.font | QGraphicsProxyWidget.font |
QWidget.cursor | QGraphicsProxyWidget.cursor | The embedded widget overrides the proxy widget cursor. The proxy cursor changes depending on which embedded subwidget is currently under the mouse.
QWidget.sizeHint() | QGraphicsProxyWidget.sizeHint() | All size hint functionality from the embedded widget is forwarded by the proxy.
QWidget.getContentsMargins() | QGraphicsProxyWidget.getContentsMargins() | Updated once by setWidget().
QWidget.windowTitle | QGraphicsProxyWidget.windowTitle | Updated once by setWidget().

Note: QGraphicsScene keeps the embedded widget in a special state that prevents it from disturbing other widgets (both embedded and not embedded) while the widget is embedded. In this state, the widget may differ slightly in behavior from when it is not embedded.

Warning: This class is provided for convenience when bridging QWidgets and QGraphicsItems, it should not be used for high-performance scenarios.

See also QGraphicsScene.addWidget() and QGraphicsWidget.

## Method Documentation

### QGraphicsProxyWidget.__init__ (self, QGraphicsItem parent = None, Qt.WindowFlags wFlags = 0)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a new QGraphicsProxy widget. parent and wFlags are passed to QGraphicsItem's constructor.

### QGraphicsProxyWidget.contextMenuEvent (self, QGraphicsSceneContextMenuEvent event)

### QGraphicsProxyWidget QGraphicsProxyWidget.createProxyForChildWidget (self, QWidget child)

Creates a proxy widget for the given child of the widget contained in this proxy.

This function makes it possible to aquire proxies for non top-level widgets. For instance, you can embed a dialog, and then transform only one of its widgets.

If the widget is already embedded, return the existing proxy widget.

This function was introduced in Qt 4.5.

See also newProxyWidget() and QGraphicsScene.addWidget().

### QGraphicsProxyWidget.dragEnterEvent (self, QGraphicsSceneDragDropEvent event)

### QGraphicsProxyWidget.dragLeaveEvent (self, QGraphicsSceneDragDropEvent event)

### QGraphicsProxyWidget.dragMoveEvent (self, QGraphicsSceneDragDropEvent event)

### QGraphicsProxyWidget.dropEvent (self, QGraphicsSceneDragDropEvent event)

### bool QGraphicsProxyWidget.event (self, QEvent event)

### bool QGraphicsProxyWidget.eventFilter (self, QObject object, QEvent event)

### QGraphicsProxyWidget.focusInEvent (self, QFocusEvent event)

### bool QGraphicsProxyWidget.focusNextPrevChild (self, bool next)

### QGraphicsProxyWidget.focusOutEvent (self, QFocusEvent event)

### QGraphicsProxyWidget.grabMouseEvent (self, QEvent event)

### QGraphicsProxyWidget.hideEvent (self, QHideEvent event)

### QGraphicsProxyWidget.hoverEnterEvent (self, QGraphicsSceneHoverEvent event)

### QGraphicsProxyWidget.hoverLeaveEvent (self, QGraphicsSceneHoverEvent event)

### QGraphicsProxyWidget.hoverMoveEvent (self, QGraphicsSceneHoverEvent event)

### QVariant QGraphicsProxyWidget.itemChange (self, QGraphicsItem.GraphicsItemChange change, QVariant value)

### QGraphicsProxyWidget.keyPressEvent (self, QKeyEvent event)

### QGraphicsProxyWidget.keyReleaseEvent (self, QKeyEvent event)

### QGraphicsProxyWidget.mouseDoubleClickEvent (self, QGraphicsSceneMouseEvent event)

### QGraphicsProxyWidget.mouseMoveEvent (self, QGraphicsSceneMouseEvent event)

### QGraphicsProxyWidget.mousePressEvent (self, QGraphicsSceneMouseEvent event)

### QGraphicsProxyWidget.mouseReleaseEvent (self, QGraphicsSceneMouseEvent event)

### QGraphicsProxyWidget QGraphicsProxyWidget.newProxyWidget (self, QWidget)

Creates a proxy widget for the given child of the widget contained in this proxy.

You should not call this function directly; use QGraphicsProxyWidget.createProxyForChildWidget() instead.

This function is a fake virtual slot that you can reimplement in your subclass in order to control how new proxy widgets are created. The default implementation returns a proxy created with the QGraphicsProxyWidget() constructor with this proxy widget as the parent.

This function was introduced in Qt 4.5.

See also createProxyForChildWidget().

### QGraphicsProxyWidget.paint (self, QPainter painter, QStyleOptionGraphicsItem option, QWidget widget)

### QGraphicsProxyWidget.resizeEvent (self, QGraphicsSceneResizeEvent event)

### QGraphicsProxyWidget.setGeometry (self, QRectF rect)

### QGraphicsProxyWidget.setWidget (self, QWidget widget)

The widget argument has it's ownership transferred to Qt.

Embeds widget into this proxy widget. The embedded widget must reside exclusively either inside or outside of Graphics View. You cannot embed a widget as long as it is is visible elsewhere in the UI, at the same time.

widget must be a top-level widget whose parent is 0.

When the widget is embedded, its state (e.g., visible, enabled, geometry, size hints) is copied into the proxy widget. If the embedded widget is explicitly hidden or disabled, the proxy widget will become explicitly hidden or disabled after embedding is complete. The class documentation has a full overview over the shared state.

After this function returns, QGraphicsProxyWidget will keep its state synchronized with that of widget whenever possible.

If a widget is already embedded by this proxy when this function is called, that widget will first be automatically unembedded. Passing 0 for the widget argument will only unembed the widget, and the ownership of the currently embedded widget will be passed on to the caller. Every child widget that are embedded will also be embedded and their proxy widget destroyed.

Note that widgets with the Qt.WA_PaintOnScreen widget attribute set and widgets that wrap an external application or controller cannot be embedded. Examples are QGLWidget and QAxWidget.

See also widget().

### QGraphicsProxyWidget.showEvent (self, QShowEvent event)

### QSizeF QGraphicsProxyWidget.sizeHint (self, Qt.SizeHint which, QSizeF constraint = QSizeF())

### QRectF QGraphicsProxyWidget.subWidgetRect (self, QWidget widget)

Returns the rectangle for widget, which must be a descendant of widget(), or widget() itself, in this proxy item's local coordinates.

If no widget is embedded, widget is 0, or widget is not a descendant of the embedded widget, this function returns an empty QRectF.

See also widget().

### int QGraphicsProxyWidget.type (self)

### QGraphicsProxyWidget.ungrabMouseEvent (self, QEvent event)

### QGraphicsProxyWidget.wheelEvent (self, QGraphicsSceneWheelEvent event)

### QWidget QGraphicsProxyWidget.widget (self)

Returns a pointer to the embedded widget.

See also setWidget().