
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQGraphicsProxyWidget class provides a proxy layer for embedding aQWidget in aQGraphicsScene.More...
| Header: | #include <QGraphicsProxyWidget> |
| Since: | Qt 4.4 |
| Inherits: | QGraphicsWidget |
| QGraphicsProxyWidget(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0) | |
| ~QGraphicsProxyWidget() | |
| QGraphicsProxyWidget * | createProxyForChildWidget(QWidget * child) |
| void | setWidget(QWidget * widget) |
| QRectF | subWidgetRect(const QWidget * widget) const |
| QWidget * | widget() const |
| virtual void | paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) |
| virtual void | setGeometry(const QRectF & rect) |
| virtual int | type() const |
| virtual void | contextMenuEvent(QGraphicsSceneContextMenuEvent * event) |
| virtual void | dragEnterEvent(QGraphicsSceneDragDropEvent * event) |
| virtual void | dragLeaveEvent(QGraphicsSceneDragDropEvent * event) |
| virtual void | dragMoveEvent(QGraphicsSceneDragDropEvent * event) |
| virtual void | dropEvent(QGraphicsSceneDragDropEvent * event) |
| virtual bool | event(QEvent * event) |
| virtual bool | eventFilter(QObject * object, QEvent * event) |
| virtual void | focusInEvent(QFocusEvent * event) |
| virtual bool | focusNextPrevChild(bool next) |
| virtual void | focusOutEvent(QFocusEvent * event) |
| virtual void | grabMouseEvent(QEvent * event) |
| virtual void | hideEvent(QHideEvent * event) |
| virtual void | hoverEnterEvent(QGraphicsSceneHoverEvent * event) |
| virtual void | hoverLeaveEvent(QGraphicsSceneHoverEvent * event) |
| virtual void | hoverMoveEvent(QGraphicsSceneHoverEvent * event) |
| virtual QVariant | itemChange(GraphicsItemChange change, const QVariant & value) |
| virtual void | keyPressEvent(QKeyEvent * event) |
| virtual void | keyReleaseEvent(QKeyEvent * event) |
| virtual void | mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event) |
| virtual void | mouseMoveEvent(QGraphicsSceneMouseEvent * event) |
| virtual void | mousePressEvent(QGraphicsSceneMouseEvent * event) |
| virtual void | mouseReleaseEvent(QGraphicsSceneMouseEvent * event) |
| virtual void | resizeEvent(QGraphicsSceneResizeEvent * event) |
| virtual void | showEvent(QShowEvent * event) |
| virtual QSizeF | sizeHint(Qt::SizeHint which, const QSizeF & constraint = QSizeF()) const |
| virtual void | ungrabMouseEvent(QEvent * event) |
| virtual void | wheelEvent(QGraphicsSceneWheelEvent * event) |
| QGraphicsProxyWidget * | newProxyWidget(const QWidget * child) |
TheQGraphicsProxyWidget class provides a proxy layer for embedding aQWidget in aQGraphicsScene.
QGraphicsProxyWidget embedsQWidget-based widgets, for example, aQPushButton,QFontComboBox, or evenQFileDialog, intoQGraphicsScene. It forwards events between the two objects and translates betweenQWidget's integer-based geometry andQGraphicsWidget's qreal-based geometry.QGraphicsProxyWidget supports all core features ofQWidget, 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=newQTabWidget;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 embeddedQComboBox shows its popup list, a newQGraphicsProxyWidget is created automatically, embedding the popup, and positioning it correctly. This only works if the popup is child of the embedded widget (for exampleQToolButton::setMenu() requires theQMenu instance to be child of theQToolButton).
There are two ways to embed a widget usingQGraphicsProxyWidget. The most common way is to pass a widget pointer toQGraphicsScene::addWidget() together with any relevantQt::WindowFlags. This function returns a pointer to aQGraphicsProxyWidget. 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=newQGroupBox("Contact Details");QLabel*numberLabel=newQLabel("Telephone number");QLineEdit*numberEdit=newQLineEdit;QFormLayout*layout=newQFormLayout;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 newQGraphicsProxyWidget item, and then callsetWidget() to embed aQWidget later. Thewidget() function returns a pointer to the embedded widget.QGraphicsProxyWidget shares ownership withQWidget, so if either of the two widgets are destroyed, the other widget will be automatically destroyed as well.
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 aQGraphicsWidget is visible when created (you do not have to callshow()). If you explicitly hide the embedded widget, the proxy will also become invisible.
Example:
QGraphicsScene scene;QLineEdit*edit=newQLineEdit;QGraphicsProxyWidget*proxy= scene.addWidget(edit);edit->isVisible();// returns trueproxy->isVisible();// also returns trueedit->hide();edit->isVisible();// returns falseproxy->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 bysetWidget(). |
| QWidget::windowTitle | QGraphicsProxyWidget::windowTitle | Updated once bysetWidget(). |
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 alsoQGraphicsScene::addWidget() andQGraphicsWidget.
Constructs a new QGraphicsProxy widget.parent andwFlags are passed toQGraphicsItem's constructor.
Destroys the proxy widget and any embedded widget.
[virtual protected]void QGraphicsProxyWidget::contextMenuEvent(QGraphicsSceneContextMenuEvent * event)Reimplemented fromQGraphicsItem::contextMenuEvent().
Creates a proxy widget for the givenchild of the widget contained in this proxy.
This function makes it possible to acquire 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 alsonewProxyWidget() andQGraphicsScene::addWidget().
[virtual protected]void QGraphicsProxyWidget::dragEnterEvent(QGraphicsSceneDragDropEvent * event)Reimplemented fromQGraphicsItem::dragEnterEvent().
[virtual protected]void QGraphicsProxyWidget::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)Reimplemented fromQGraphicsItem::dragLeaveEvent().
[virtual protected]void QGraphicsProxyWidget::dragMoveEvent(QGraphicsSceneDragDropEvent * event)Reimplemented fromQGraphicsItem::dragMoveEvent().
[virtual protected]void QGraphicsProxyWidget::dropEvent(QGraphicsSceneDragDropEvent * event)Reimplemented fromQGraphicsItem::dropEvent().
[virtual protected]bool QGraphicsProxyWidget::event(QEvent * event)Reimplemented fromQObject::event().
[virtual protected]bool QGraphicsProxyWidget::eventFilter(QObject * object,QEvent * event)Reimplemented fromQObject::eventFilter().
[virtual protected]void QGraphicsProxyWidget::focusInEvent(QFocusEvent * event)Reimplemented fromQGraphicsItem::focusInEvent().
[virtual protected]bool QGraphicsProxyWidget::focusNextPrevChild(bool next)Reimplemented fromQGraphicsWidget::focusNextPrevChild().
[virtual protected]void QGraphicsProxyWidget::focusOutEvent(QFocusEvent * event)Reimplemented fromQGraphicsItem::focusOutEvent().
[virtual protected]void QGraphicsProxyWidget::grabMouseEvent(QEvent * event)Reimplemented fromQGraphicsWidget::grabMouseEvent().
[virtual protected]void QGraphicsProxyWidget::hideEvent(QHideEvent * event)Reimplemented fromQGraphicsWidget::hideEvent().
[virtual protected]void QGraphicsProxyWidget::hoverEnterEvent(QGraphicsSceneHoverEvent * event)Reimplemented fromQGraphicsItem::hoverEnterEvent().
[virtual protected]void QGraphicsProxyWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)Reimplemented fromQGraphicsItem::hoverLeaveEvent().
[virtual protected]void QGraphicsProxyWidget::hoverMoveEvent(QGraphicsSceneHoverEvent * event)Reimplemented fromQGraphicsItem::hoverMoveEvent().
[virtual protected]QVariant QGraphicsProxyWidget::itemChange(GraphicsItemChange change, constQVariant & value)Reimplemented fromQGraphicsItem::itemChange().
[virtual protected]void QGraphicsProxyWidget::keyPressEvent(QKeyEvent * event)Reimplemented fromQGraphicsItem::keyPressEvent().
[virtual protected]void QGraphicsProxyWidget::keyReleaseEvent(QKeyEvent * event)Reimplemented fromQGraphicsItem::keyReleaseEvent().
[virtual protected]void QGraphicsProxyWidget::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)Reimplemented fromQGraphicsItem::mouseDoubleClickEvent().
[virtual protected]void QGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent * event)Reimplemented fromQGraphicsItem::mouseMoveEvent().
[virtual protected]void QGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent * event)Reimplemented fromQGraphicsItem::mousePressEvent().
[virtual protected]void QGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)Reimplemented fromQGraphicsItem::mouseReleaseEvent().
[protected slot]QGraphicsProxyWidget * QGraphicsProxyWidget::newProxyWidget(constQWidget * child)Creates a proxy widget for the givenchild of the widget contained in this proxy.
You should not call this function directly; useQGraphicsProxyWidget::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 theQGraphicsProxyWidget() constructor with this proxy widget as the parent.
This function was introduced in Qt 4.5.
See alsocreateProxyForChildWidget().
[virtual]void QGraphicsProxyWidget::paint(QPainter * painter, constQStyleOptionGraphicsItem * option,QWidget * widget)Reimplemented fromQGraphicsItem::paint().
[virtual protected]void QGraphicsProxyWidget::resizeEvent(QGraphicsSceneResizeEvent * event)Reimplemented fromQGraphicsWidget::resizeEvent().
[virtual]void QGraphicsProxyWidget::setGeometry(constQRectF & rect)Reimplemented fromQGraphicsLayoutItem::setGeometry().
Embedswidget 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.
QGraphicsProxyWidget's window flags determine whether the widget, after embedding, will be given window decorations or not.
After this function returns,QGraphicsProxyWidget will keep its state synchronized with that ofwidget 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 thewidget 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 theQt::WA_PaintOnScreen widget attribute set and widgets that wrap an external application or controller cannot be embedded. Examples areQGLWidget andQAxWidget.
See alsowidget().
[virtual protected]void QGraphicsProxyWidget::showEvent(QShowEvent * event)Reimplemented fromQGraphicsWidget::showEvent().
[virtual protected]QSizeF QGraphicsProxyWidget::sizeHint(Qt::SizeHint which, constQSizeF & constraint = QSizeF()) constReimplemented fromQGraphicsLayoutItem::sizeHint().
Returns the rectangle forwidget, which must be a descendant ofwidget(), orwidget() itself, in this proxy item's local coordinates.
If no widget is embedded,widget is 0, orwidget is not a descendant of the embedded widget, this function returns an emptyQRectF.
See alsowidget().
[virtual]int QGraphicsProxyWidget::type() constReimplemented fromQGraphicsItem::type().
[virtual protected]void QGraphicsProxyWidget::ungrabMouseEvent(QEvent * event)Reimplemented fromQGraphicsWidget::ungrabMouseEvent().
[virtual protected]void QGraphicsProxyWidget::wheelEvent(QGraphicsSceneWheelEvent * event)Reimplemented fromQGraphicsItem::wheelEvent().
Returns a pointer to the embedded widget.
See alsosetWidget().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.