QGraphicsProxyWidget 类

QGraphicsProxyWidget 类提供代理层为嵌入 QWidget QGraphicsScene . 更多...

头: #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 。它在 2 对象之间转发事件和平移介于 QWidget 基于整数的几何体和 QGraphicsWidget 基于 qreal 的几何体。QGraphicsProxyWidget 支持所有核心特征对于 QWidget ,包括 Tab 焦点、键盘输入、拖放及弹出窗口。还可以嵌入复杂 Widget,如:带子小部件的小部件。

范例:

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. This only works if the popup is child of the embedded widget (for example QToolButton::setMenu () requires the QMenu instance to be child of the QToolButton ).

采用 QGraphicsProxyWidget 嵌入 Widget

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(groupBox);
layout->addRow(numberLabel, numberEdit);
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 () 以嵌入 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.

同步 Widget 状态

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.

范例:

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 状态 注意事项
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 ().

注意: 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.

警告: This class is provided for convenience when bridging QWidgets and QGraphicsItems, it should not be used for high-performance scenarios. In particular, embedding widgets into a scene that is then displayed through a QGraphicsView that uses an OpenGL viewport will not work for all combinations.

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

成员类型文档编制

enum QGraphicsProxyWidget:: anonymous

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

常量 描述
QGraphicsProxyWidget::Type 12 A graphics proxy widget

成员函数文档编制

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

构造新的 QGraphicsProxy 小部件。 parent and wFlags 被传递给 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 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.

另请参阅 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 of the widget contained in this proxy.

您不应该直接调用此函数。使用 QGraphicsProxyWidget::createProxyForChildWidget () 代替。

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.

另请参阅 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 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 visible elsewhere in the UI, at the same time.

widget 必须是顶层小部件,父级为 nullptr .

当 Widget 嵌入时,其状态 (如:可见、启用、几何体、尺寸提示) 会被拷贝到代理小部件。若嵌入小部件被明确隐藏或禁用,代理 Widget 将在嵌入完成后变为被明确隐藏或禁用。类文档编制对共享状态有完整概述。

QGraphicsProxyWidget 的窗口标志确定 Widget 在嵌入后,是否赋予其窗口装饰。

此函数返回后, QGraphicsProxyWidget 将同步保持其状态同 widget 每当可能时。

若 Widget 已嵌入此代理当调用此函数时,该 Widget 将首先自动取消嵌入。传递 nullptr widget 自变量仅取消嵌入 Widget,且目前嵌入 Widget 的所有权会被传递给调用者。每个嵌入子级 Widget 也被嵌入,且它们的代理 Widget 会被销毁。

注意:Widget 具有 Qt::WA_PaintOnScreen widget attribute set and widgets that wrap an external application or controller cannot be embedded. Examples are QOpenGLWidget and 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

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

若没有嵌入小部件, widget is nullptr ,或 widget is not a descendant of the embedded widget, this function returns an empty 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 ().