QGraphicsProxyWidget Class
QGraphicsProxyWidget 类提供了一个代理层,用于将QWidget 嵌入到QGraphicsScene 中。更多内容...
| 头文件: | #include <QGraphicsProxyWidget> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
| qmake: | QT += widgets |
| 继承自: | QGraphicsWidget |
公共类型
| enum { Type } |
公共函数
| QGraphicsProxyWidget(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags()) | |
| virtual | ~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) override |
| virtual void | setGeometry(const QRectF &rect) override |
| virtual int | type() const override |
重新实现的受保护函数
| virtual void | contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override |
| virtual void | dragEnterEvent(QGraphicsSceneDragDropEvent *event) override |
| virtual void | dragLeaveEvent(QGraphicsSceneDragDropEvent *event) override |
| virtual void | dragMoveEvent(QGraphicsSceneDragDropEvent *event) override |
| virtual void | dropEvent(QGraphicsSceneDragDropEvent *event) override |
| virtual bool | event(QEvent *event) override |
| virtual bool | eventFilter(QObject *object, QEvent *event) override |
| virtual void | focusInEvent(QFocusEvent *event) override |
| virtual bool | focusNextPrevChild(bool next) override |
| virtual void | focusOutEvent(QFocusEvent *event) override |
| virtual void | grabMouseEvent(QEvent *event) override |
| virtual void | hideEvent(QHideEvent *event) override |
| virtual void | hoverEnterEvent(QGraphicsSceneHoverEvent *event) override |
| virtual void | hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override |
| virtual void | hoverMoveEvent(QGraphicsSceneHoverEvent *event) override |
| virtual void | inputMethodEvent(QInputMethodEvent *event) override |
| virtual QVariant | inputMethodQuery(Qt::InputMethodQuery query) const override |
| virtual QVariant | itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override |
| virtual void | keyPressEvent(QKeyEvent *event) override |
| virtual void | keyReleaseEvent(QKeyEvent *event) override |
| virtual void | mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override |
| virtual void | mouseMoveEvent(QGraphicsSceneMouseEvent *event) override |
| virtual void | mousePressEvent(QGraphicsSceneMouseEvent *event) override |
| virtual void | mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override |
| virtual void | resizeEvent(QGraphicsSceneResizeEvent *event) override |
| virtual void | showEvent(QShowEvent *event) override |
| virtual QSizeF | sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override |
| virtual void | ungrabMouseEvent(QEvent *event) override |
| virtual void | wheelEvent(QGraphicsSceneWheelEvent *event) override |
受保护的槽
| QGraphicsProxyWidget * | newProxyWidget(const QWidget *child) |
详细说明
QGraphicsProxyWidget 将基于QWidget 的小部件(例如QPushButton 、QFontComboBox ,甚至是QFileDialog )嵌入到QGraphicsScene 中。它在两个对象之间转发事件,并在QWidget 基于整数的几何与QGraphicsWidget 基于qreal的几何之间进行转换。 QGraphicsProxyWidget 支持QWidget 的所有核心功能,包括标签聚焦、键盘输入、拖放和弹出窗口。您还可以嵌入复杂的小部件,例如包含子小部件的小部件。
示例:
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 会通过为每个弹出窗口创建子代理,自动处理嵌入式小部件中弹出窗口子节点的嵌入。这意味着当嵌入的QComboBox 显示其弹出窗口列表时,系统会自动创建一个新的 QGraphicsProxyWidget,将弹出窗口嵌入其中并正确定位。 此机制仅在弹出窗口是嵌入式小部件的子元素时才有效(例如,QToolButton::setMenu() 要求QMenu 实例是QToolButton 的子元素)。
使用 QGraphicsProxyWidget 嵌入小部件
使用 QGraphicsProxyWidget 嵌入小部件有两种方法。最常见的方法是将小部件指针与任何相关的Qt::WindowFlags 一起传递给QGraphicsScene::addWidget()。该函数返回一个指向 QGraphicsProxyWidget 的指针。然后,您可以选择重新设置代理或嵌入的小部件本身的父对象,或调整其位置。
例如,在下面的代码片段中,我们将一个组框嵌入到代理中:
QGroupBox *groupBox = new QGroupBox("Contact Details");
QLabel *numberLabel = new QLabel("Telephone number");
QLineEdit *numberEdit = new QLineEdit;
QFormLayout *layout = new QFormLayout(groupBox);
layout->addRow(numberLabel, numberEdit);
QGraphicsScene scene;
QGraphicsProxyWidget *proxy = scene.addWidget(groupBox);
QGraphicsView view(&scene);
view.show();下图是标注了 contents margin 和 contents rect 后的输出结果。

或者,您可以先创建一个新的 QGraphicsProxyWidget 项,然后稍后调用setWidget() 来嵌入一个QWidget 。widget() 函数返回指向嵌入小部件的指针。QGraphicsProxyWidget 与QWidget 共享所有权,因此如果其中任一小部件被销毁,另一个小部件也会自动被销毁。
小部件状态的同步
QGraphicsProxyWidget 会使其状态与嵌入的小部件保持同步。例如,如果代理被隐藏或禁用,嵌入的小部件也会被隐藏或禁用,反之亦然。 当通过调用 addWidget() 将小部件嵌入时,QGraphicsProxyWidget 会将小部件的状态复制到代理中,此后两者将在可能的情况下保持同步。 默认情况下,当您将小部件嵌入代理时,小部件和代理都会处于可见状态,因为QGraphicsWidget 在创建时即为可见(您无需调用show())。如果您显式隐藏了嵌入的小部件,代理也会随之变为不可见。
示例:
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 falseQGraphicsProxyWidget 针对以下状态保持对称性:
| QWidget 状态 | QGraphicsProxyWidget 状态 | 注释 |
|---|---|---|
| QWidget::enabled | QGraphicsProxyWidget::enabled | |
| QWidget::visible | QGraphicsProxyWidget::visible | 该显式状态也是对称的。 |
| QWidget::geometry | QGraphicsProxyWidget::geometry | 只有当嵌入的小部件可见时,才保证几何形状是对称的。 |
| QWidget::layoutDirection | QGraphicsProxyWidget::layoutDirection | |
| QWidget::style | QGraphicsProxyWidget::style | |
| QWidget::palette | QGraphicsProxyWidget::palette | |
| QWidget::font | QGraphicsProxyWidget::font | |
| QWidget::cursor | QGraphicsProxyWidget::cursor | 嵌入式控件会覆盖代理控件的光标。代理光标会根据当前鼠标悬停在哪个嵌入式子控件上而发生变化。 |
| QWidget::sizeHint() | QGraphicsProxyWidget::sizeHint() | 代理会转发来自嵌入式小部件的所有尺寸提示功能。 |
| QWidget::getContentsMargins() | QGraphicsProxyWidget::getContentsMargins() | 由setWidget()更新一次。 |
| QWidget::windowTitle | QGraphicsProxyWidget::windowTitle | 由setWidget() 更新一次。简体中文(大陆) |
注意: QGraphicsScene 会将嵌入的小部件置于一种特殊状态,以防止该小部件在嵌入期间干扰其他小部件(无论是否嵌入)。在此状态下,该小部件的行为可能与未嵌入时略有不同。
警告:本类 旨在为连接 QWidgets 和 QGraphicsItems 提供便利,不应用于高性能场景。特别是,将小部件嵌入到场景中,然后通过使用 OpenGL 视口 的QGraphicsView 进行显示,这种做法并非在所有组合情况下都能正常工作。
另请参阅 QGraphicsScene::addWidget() 和QGraphicsWidget 。
成员函数文档
QGraphicsProxyWidget::QGraphicsProxyWidget(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags())
创建一个新的 QGraphicsProxy 控件。parent 和wFlags 将作为参数传递给QGraphicsItem 的构造函数。
[virtual noexcept] QGraphicsProxyWidget::~QGraphicsProxyWidget()
销毁代理控件及其中的所有嵌入式控件。
[override virtual protected] void QGraphicsProxyWidget::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
重写:QGraphicsItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)。
QGraphicsProxyWidget *QGraphicsProxyWidget::createProxyForChildWidget(QWidget *child)
为该代理中包含的小部件的指定child 创建一个代理小部件。
此函数使得获取非顶级控件的代理成为可能。例如,您可以嵌入一个对话框,然后仅对其中的某个控件进行转换。
如果该控件已被嵌入,则返回现有的代理控件。
另请参阅 newProxyWidget() 和QGraphicsScene::addWidget()。
[override virtual protected] void QGraphicsProxyWidget::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
重写了:QGraphicsItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
重写了:QGraphicsItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
重写了:QGraphicsItem::dragMoveEvent(QGraphicsSceneDragDropEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::dropEvent(QGraphicsSceneDragDropEvent *event)
重写了:QGraphicsItem::dropEvent(QGraphicsSceneDragDropEvent *event)。
[override virtual protected] bool QGraphicsProxyWidget::event(QEvent *event)
重写了:QGraphicsWidget::event(QEvent *event)。
[override virtual protected] bool QGraphicsProxyWidget::eventFilter(QObject *object, QEvent *event)
重写了:QObject::eventFilter(QObject *watched, QEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::focusInEvent(QFocusEvent *event)
重写了:QGraphicsWidget::focusInEvent(QFocusEvent *event)。
[override virtual protected] bool QGraphicsProxyWidget::focusNextPrevChild(bool next)
重新实现了:QGraphicsWidget::focusNextPrevChild (bool next)。
[override virtual protected] void QGraphicsProxyWidget::focusOutEvent(QFocusEvent *event)
重写了:QGraphicsWidget::focusOutEvent(QFocusEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::grabMouseEvent(QEvent *event)
重写了:QGraphicsWidget::grabMouseEvent(QEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::hideEvent(QHideEvent *event)
重写了:QGraphicsWidget::hideEvent(QHideEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
重写了:QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
重写:QGraphicsWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
重写:QGraphicsWidget::hoverMoveEvent(QGraphicsSceneHoverEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::inputMethodEvent(QInputMethodEvent *event)
重写了:QGraphicsItem::inputMethodEvent(QInputMethodEvent *event)。
[override virtual protected] QVariant QGraphicsProxyWidget::inputMethodQuery(Qt::InputMethodQuery query) const
重写了:QGraphicsItem::inputMethodQuery (Qt::InputMethodQuery query)const。
[override virtual protected] QVariant QGraphicsProxyWidget::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
重写了:QGraphicsWidget::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)。
[override virtual protected] void QGraphicsProxyWidget::keyPressEvent(QKeyEvent *event)
重写了:QGraphicsItem::keyPressEvent(QKeyEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::keyReleaseEvent(QKeyEvent *event)
重写了:QGraphicsItem::keyReleaseEvent(QKeyEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
重写了:QGraphicsItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
重写了:QGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
重写了:QGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
重写了:QGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)。
[protected slot] QGraphicsProxyWidget *QGraphicsProxyWidget::newProxyWidget(const QWidget *child)
为该代理中所包含的小部件的指定child 创建一个代理小部件。
请勿直接调用此函数;请改用QGraphicsProxyWidget::createProxyForChildWidget()。
此函数是一个伪虚拟槽,您可以在子类中重写它,以控制新代理小部件的创建方式。默认实现会返回一个使用QGraphicsProxyWidget()构造函数创建的代理,并将此代理小部件作为其父对象。
另请参阅 createProxyForChildWidget()。
[override virtual] void QGraphicsProxyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
重写了:QGraphicsWidget::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)。
[override virtual protected] void QGraphicsProxyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
重写了:QGraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent *event)。
[override virtual] void QGraphicsProxyWidget::setGeometry(const QRectF &rect)
重写了:QGraphicsLayoutItem::setGeometry(const QRectF &rect)。
void QGraphicsProxyWidget::setWidget(QWidget *widget)
将widget 嵌入到此代理控件中。被嵌入的控件必须完全位于Graphics View内部或外部。如果该控件同时在UI的其他位置可见,则无法将其嵌入。
widget 必须是顶级控件,且其父控件为nullptr 。
当控件被嵌入时,其状态(例如:可见性、启用状态、几何形状、尺寸提示)会被复制到代理控件中。如果被嵌入的控件被显式隐藏或禁用,则在嵌入完成后,代理控件也将被显式隐藏或禁用。类文档中提供了关于共享状态的完整概述。
QGraphicsProxyWidget的窗口标志决定了小部件在嵌入后是否会显示窗口装饰。
该函数返回后,QGraphicsProxyWidget 将尽可能保持其状态与widget 的状态同步。
如果调用此函数时某个控件已被该代理嵌入,则该控件将首先被自动解除嵌入。将 `nullptr ` 作为 `widget ` 参数传递时,仅会解除该控件的嵌入,且当前已嵌入控件的所有权将转移给调用方。所有已嵌入的子控件也将被解除嵌入,其代理控件将被销毁。
请注意,设置了Qt::WA_PaintOnScreen 控件属性的控件,以及封装了外部应用程序或控制器的控件,均无法被嵌入。例如QOpenGLWidget 和 QAxWidget。
另请参阅 widget()。
[override virtual protected] void QGraphicsProxyWidget::showEvent(QShowEvent *event)
重写了:QGraphicsWidget::showEvent(QShowEvent *event)。
[override virtual protected] QSizeF QGraphicsProxyWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const
重写了:QGraphicsWidget::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const。
QRectF QGraphicsProxyWidget::subWidgetRect(const QWidget *widget) const
返回widget 对应的矩形,该矩形必须是widget()的子元素,或者直接是widget()本身,且以该代理项的本地坐标系为基准。
如果未嵌入任何小部件,widget 为nullptr ,或者widget 不是嵌入小部件的子元素,则该函数返回一个空的QRectF 。
另请参阅 widget()。
[override virtual] int QGraphicsProxyWidget::type() const
重新实现了:QGraphicsWidget::type() const。
[override virtual protected] void QGraphicsProxyWidget::ungrabMouseEvent(QEvent *event)
重写了:QGraphicsWidget::ungrabMouseEvent(QEvent *event)。
[override virtual protected] void QGraphicsProxyWidget::wheelEvent(QGraphicsSceneWheelEvent *event)
重写了:QGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event)。
QWidget *QGraphicsProxyWidget::widget() const
返回指向嵌入式控件的指针。
另请参阅 setWidget()。
© 2026 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 the GNU 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.