QGraphicsProxyWidget Class

QGraphicsProxyWidget 类提供了一个代理层,用于在QGraphicsScene 中嵌入QWidget... 更多...

Header: #include <QGraphicsProxyWidget>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QGraphicsWidget

公共类型

enum anonymous { 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 的所有核心功能,包括标签焦点、键盘输入、拖放和弹出窗口。您还可以嵌入复杂的窗口小部件,例如带有子窗口小部件的窗口小部件。

示例:QGraphicsProxyWidget

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 通过为每个弹出窗口创建子代理,自动嵌入嵌入式 widget 的弹出窗口子窗口。这意味着,当嵌入式QComboBox 显示其弹出窗口列表时,就会自动创建一个新的 QGraphicsProxyWidget,嵌入弹出窗口并正确定位。这只有在弹出窗口是嵌入窗口小部件的子窗口时才有效(例如QToolButton::setMenu() 要求QMenu 实例是QToolButton 的子窗口)。

使用 QGraphicsProxyWidget 嵌入 widget

使用 QGraphicsProxyWidget 嵌入 Widget 有两种方法。最常见的方法是将 widget 指针连同任何相关的Qt::WindowFlags 传递给QGraphicsScene::addWidget() 。该函数会返回一个指向 QGraphicsProxyWidget 的指针。然后,您可以选择对代理或嵌入式 widget 本身进行重定向或定位。

例如,在下面的代码片段中,我们在代理中嵌入了一个分组框:

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();

下图是输出结果,其中标注了内容边距和内容矩形。

另外,您也可以先创建一个新的 QGraphicsProxyWidget 项目,然后调用setWidget() 来嵌入QWidgetwidget() 函数返回一个指向嵌入 widget 的指针。QGraphicsProxyWidget 与QWidget 共享所有权,因此如果这两个部件中的任何一个被销毁,另一个部件也会自动销毁。

同步部件状态

QGraphicsProxyWidget 与嵌入式 widget 保持状态同步。例如,如果代理被隐藏或禁用,嵌入的部件也会被隐藏或禁用,反之亦然。当通过调用 addWidget() 嵌入部件时,QGraphicsProxyWidget 会将部件的状态复制到代理中,之后两者将尽可能保持同步。默认情况下,当您将部件嵌入到代理中时,部件和代理都将是可见的,因为QGraphicsWidget 在创建时是可见的(您不必调用show() )。如果明确隐藏嵌入的 widget,代理也会变成不可见。

示例

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 在以下状态下保持对称:

QWidget 状态QGraphicsProxyWidget 状态注释
QWidget::enabledQGraphicsProxyWidget::enabled
QWidget::visibleQGraphicsProxyWidget::visible显式状态也是对称的。
QWidget::geometryQGraphicsProxyWidget::geometry几何图形只有在嵌入式 widget 可见时才保证对称。
QWidget::layoutDirectionQGraphicsProxyWidget::layoutDirection
QWidget::styleQGraphicsProxyWidget::style
QWidget::paletteQGraphicsProxyWidget::palette
QWidget::fontQGraphicsProxyWidget::font
QWidget::cursorQGraphicsProxyWidget::cursor嵌入式 widget 会覆盖代理 widget 的光标。代理游标的变化取决于当前鼠标所在的嵌入式子部件。
QWidget::sizeHint()QGraphicsProxyWidget::sizeHint()嵌入式 widget 的所有尺寸提示功能都由代理转发。
QWidget::getContentsMargins()QGraphicsProxyWidget::getContentsMargins()通过setWidget() 更新一次。
QWidget::windowTitleQGraphicsProxyWidget::windowTitlesetWidget() 更新一次。

注意: QGraphicsScene 将嵌入式窗口小部件保持在一种特殊状态,以防止在窗口小部件嵌入时干扰其他窗口小部件(包括已嵌入和未嵌入的)。在这种状态下,窗口小部件的行为可能与未嵌入时略有不同。

警告 提供该类是为了在桥接 QWidgets 和 QGraphicsItems 时提供方便,它不应被用于高性能场景。特别是,将部件嵌入到场景中,然后通过使用 OpenGL 视口的QGraphicsView 显示时,并非所有组合都能正常工作。

另请参阅 QGraphicsScene::addWidget() 和QGraphicsWidget

成员类型文档

enum QGraphicsProxyWidget::anonymous

虚拟type() 函数返回的值。

常量说明
QGraphicsProxyWidget::Type12图形代理部件

成员函数文档

QGraphicsProxyWidget::QGraphicsProxyWidget(QGraphicsItem *parent = nullptr, Qt::WindowFlags wFlags = Qt::WindowFlags())

构造一个新的 QGraphicsProxy widget。parentwFlags 被传递给QGraphicsItem 的构造函数。

[virtual noexcept] QGraphicsProxyWidget::~QGraphicsProxyWidget()

销毁代理 widget 和任何嵌入式 widget。

[override virtual protected] void QGraphicsProxyWidget::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)

重实现:QGraphicsItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event).

QGraphicsProxyWidget *QGraphicsProxyWidget::createProxyForChildWidget(QWidget *child)

为该代理中包含的 widget 的给定child 创建一个代理 widget。

通过该函数,可以为非顶级 widget 获取代理。例如,您可以嵌入一个对话框,然后只转换其中一个部件。

如果该部件已嵌入,则返回现有的代理部件。

另请参见 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)

为该代理中包含的 widget 的给定child 创建一个代理 widget。

请勿直接调用此函数;请使用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 嵌入此代理部件。嵌入的 widget 必须完全位于图形视图内部或外部。只要在用户界面的其他地方同时可见,就不能嵌入 Widget。

widget Widget 必须是父级为 的顶级 Widget。nullptr

嵌入部件后,其状态(如可见、启用、几何形状、尺寸提示)会复制到代理部件中。如果嵌入的 widget 显式隐藏或禁用,那么代理 widget 也会在嵌入完成后显式隐藏或禁用。类文档对共享状态有全面的概述。

QGraphicsProxyWidget代理窗口部件的窗口标志决定了嵌入后窗口部件是否会被赋予窗口装饰。

该函数返回后,QGraphicsProxyWidget 将尽可能保持与widget 的状态同步。

如果在调用此函数时,某个部件已被此代理嵌入,该部件将首先被自动解除嵌入。为widget 参数传递nullptr 只会取消嵌入 widget,而当前嵌入 widget 的所有权将传递给调用者。嵌入的每个子部件也将被嵌入,其代理部件也将被销毁。

请注意,设置了Qt::WA_PaintOnScreen widget 属性的 widget 和封装了外部应用程序或控制器的 widget 不能被嵌入。例如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() 本身,以该代理项的本地坐标表示。

如果没有嵌入部件,widgetnullptr ,或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

返回指向嵌入式 widget 的指针。

另请参阅 setWidget()。

© 2025 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.