QGraphicsView 类

QGraphicsView 类提供用于显示内容的 Widget 为 QGraphicsScene . 更多...

头: #include <QGraphicsView>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QAbstractScrollArea

公共类型

flags CacheMode
enum CacheModeFlag { CacheNone, CacheBackground }
enum DragMode { NoDrag, ScrollHandDrag, RubberBandDrag }
enum OptimizationFlag { DontSavePainterState, DontAdjustForAntialiasing, IndirectPainting }
flags OptimizationFlags
enum ViewportAnchor { NoAnchor, AnchorViewCenter, AnchorUnderMouse }
enum ViewportUpdateMode { FullViewportUpdate, MinimalViewportUpdate, SmartViewportUpdate, BoundingRectViewportUpdate, NoViewportUpdate }

特性

公共函数

  QGraphicsView (QGraphicsScene * scene , QWidget * parent = nullptr)
  QGraphicsView (QWidget * parent = nullptr)
virtual ~QGraphicsView ()
Qt::Alignment alignment () const
QBrush backgroundBrush () const
QGraphicsView::CacheMode cacheMode () const
void centerOn (const QPointF & pos )
void centerOn (qreal x , qreal y )
void centerOn (const QGraphicsItem * item )
QGraphicsView::DragMode dragMode () const
void ensureVisible (const QRectF & rect , int xmargin = 50, int ymargin = 50)
void ensureVisible (qreal x , qreal y , qreal w , qreal h , int xmargin = 50, int ymargin = 50)
void ensureVisible (const QGraphicsItem * item , int xmargin = 50, int ymargin = 50)
void fitInView (const QRectF & rect , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
void fitInView (qreal x , qreal y , qreal w , qreal h , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
void fitInView (const QGraphicsItem * item , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
QBrush foregroundBrush () const
bool isInteractive () const
bool isTransformed () const
QGraphicsItem * itemAt (const QPoint & pos ) const
QGraphicsItem * itemAt (int x , int y ) const
QList<QGraphicsItem *> () const
QList<QGraphicsItem *> (const QPoint & pos ) const
QList<QGraphicsItem *> (int x , int y ) const
QList<QGraphicsItem *> (const QRect & rect , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> (int x , int y , int w , int h , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> (const QPolygon & polygon , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> (const QPainterPath & path , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QPoint mapFromScene (const QPointF & point ) const
QPolygon mapFromScene (const QRectF & rect ) const
QPolygon mapFromScene (const QPolygonF & polygon ) const
QPainterPath mapFromScene (const QPainterPath & path ) const
QPoint mapFromScene (qreal x , qreal y ) const
QPolygon mapFromScene (qreal x , qreal y , qreal w , qreal h ) const
QPointF mapToScene (const QPoint & point ) const
QPolygonF mapToScene (const QRect & rect ) const
QPolygonF mapToScene (const QPolygon & polygon ) const
QPainterPath mapToScene (const QPainterPath & path ) const
QPointF mapToScene (int x , int y ) const
QPolygonF mapToScene (int x , int y , int w , int h ) const
QGraphicsView::OptimizationFlags optimizationFlags () const
void render (QPainter * painter , const QRectF & target = QRectF(), const QRect & source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio)
QPainter::RenderHints renderHints () const
void resetCachedContent ()
void resetTransform ()
QGraphicsView::ViewportAnchor resizeAnchor () const
void rotate (qreal angle )
QRect rubberBandRect () const
Qt::ItemSelectionMode rubberBandSelectionMode () const
void scale (qreal sx , qreal sy )
QGraphicsScene * scene () const
QRectF sceneRect () const
void setAlignment (Qt::Alignment alignment )
void setBackgroundBrush (const QBrush & brush )
void setCacheMode (QGraphicsView::CacheMode mode )
void setDragMode (QGraphicsView::DragMode mode )
void setForegroundBrush (const QBrush & brush )
void setInteractive (bool allowed )
void setOptimizationFlag (QGraphicsView::OptimizationFlag flag , bool enabled = true)
void setOptimizationFlags (QGraphicsView::OptimizationFlags flags )
void setRenderHint (QPainter::RenderHint hint , bool enabled = true)
void setRenderHints (QPainter::RenderHints hints )
void setResizeAnchor (QGraphicsView::ViewportAnchor anchor )
void setRubberBandSelectionMode (Qt::ItemSelectionMode mode )
void setScene (QGraphicsScene * scene )
void setSceneRect (const QRectF & rect )
void setSceneRect (qreal x , qreal y , qreal w , qreal h )
void setTransform (const QTransform & matrix , bool combine = false)
void setTransformationAnchor (QGraphicsView::ViewportAnchor anchor )
void setViewportUpdateMode (QGraphicsView::ViewportUpdateMode mode )
void shear (qreal sh , qreal sv )
QTransform transform () const
QGraphicsView::ViewportAnchor transformationAnchor () const
void translate (qreal dx , qreal dy )
QTransform viewportTransform () const
QGraphicsView::ViewportUpdateMode viewportUpdateMode () const

重实现公共函数

virtual QVariant inputMethodQuery (Qt::InputMethodQuery query ) const override
virtual QSize sizeHint () const override

公共槽

void invalidateScene (const QRectF & rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers)
void updateScene (const QList<QRectF> & rects )
void updateSceneRect (const QRectF & rect )

信号

void rubberBandChanged (QRect rubberBandRect , QPointF fromScenePoint , QPointF toScenePoint )

保护函数

virtual void drawBackground (QPainter * painter , const QRectF & rect )
virtual void drawForeground (QPainter * painter , const QRectF & rect )

重实现保护函数

virtual void contextMenuEvent (QContextMenuEvent * event ) override
virtual void dragEnterEvent (QDragEnterEvent * event ) override
virtual void dragLeaveEvent (QDragLeaveEvent * event ) override
virtual void dragMoveEvent (QDragMoveEvent * event ) override
virtual void dropEvent (QDropEvent * event ) override
virtual bool event (QEvent * event ) override
virtual void focusInEvent (QFocusEvent * event ) override
virtual bool focusNextPrevChild (bool next ) override
virtual void focusOutEvent (QFocusEvent * event ) override
virtual void inputMethodEvent (QInputMethodEvent * event ) override
virtual void keyPressEvent (QKeyEvent * event ) override
virtual void keyReleaseEvent (QKeyEvent * event ) override
virtual void mouseDoubleClickEvent (QMouseEvent * event ) override
virtual void mouseMoveEvent (QMouseEvent * event ) override
virtual void mousePressEvent (QMouseEvent * event ) override
virtual void mouseReleaseEvent (QMouseEvent * event ) override
virtual void paintEvent (QPaintEvent * event ) override
virtual void resizeEvent (QResizeEvent * event ) override
virtual void scrollContentsBy (int dx , int dy ) override
virtual void showEvent (QShowEvent * event ) override
virtual bool viewportEvent (QEvent * event ) override
virtual void wheelEvent (QWheelEvent * event ) override

保护槽

virtual void setupViewport (QWidget * widget ) override

详细描述

QGraphicsView 可视化内容为 QGraphicsScene 在可卷动视口。要创建带几何项的场景,见 QGraphicsScene 的文档编制。QGraphicsView 属于 图形视图框架 .

要可视化场景,开始从构造 QGraphicsView 对象,将想要可视化的场景地址传递给 QGraphicsView 构造函数。另外,可以调用 setScene () 稍后设置场景。之后调用 show (),默认情况下,视图将卷动到场景中心并显示此时可见的任何项。例如:

QGraphicsScene scene;
scene.addText("Hello, world!");
QGraphicsView view(&scene);
view.show();
					

可以明确卷动到场景中的任何位置通过使用滚动条,或通过调用 centerOn ()。通过将点传递给 centerOn (),QGraphicsView 将卷动其视口以确保点居中视图。提供重载为卷动到 QGraphicsItem ,在这种情况下,QGraphicsView 将看到项中心居中视图。若只想确保某区域可见 (但不必居中),可以调用 ensureVisible () 代替。

QGraphicsView 可以用于可视化整个场景或仅部分场景。默认情况下,会自动检测可视化区域当首次显示视图时 (通过调用 QGraphicsScene::itemsBoundingRect ())。要自己设置可视化区域矩形,可以调用 setSceneRect ()。这将适当调节滚动条范围。注意:尽管场景支持几乎无限大小,但滚动条范围从不超过整数 (INT_MIN,INT_MAX) 范围。

QGraphicsView 可视化场景通过调用 render ()。默认情况下,项被绘制到视口通过使用常规 QPainter ,并使用默认渲染提示。要改变默认渲染提示,将 QGraphicsView 传递给 QPainter 当描绘项时,可以调用 setRenderHints ().

默认情况下,QGraphicsView 提供常规 QWidget 为视口 Widget。可以访问此 Widget 通过调用 viewport (),或者可以替换它通过调用 setViewport ()。要使用 OpenGL 进行渲染,只需调用 setViewport(new QOpenGLWidget)。QGraphicsView 拥有视口 Widget 的所有权。

QGraphicsView 支持仿射变换,使用 QTransform 。可以将矩阵传递给 setTransform (),或者可以调用某一方便函数 rotate (), scale (), translate () 或 shear (). The most two common transformations are scaling, which is used to implement zooming, and rotation. QGraphicsView keeps the center of the view fixed during a transformation. Because of the scene alignment (setAligment()), translating the view will have no visual impact.

可以通过使用鼠标和键盘与场景中的项进行交互。QGraphicsView 将鼠标和键事件翻译成 scene 事件 (事件继承自 QGraphicsSceneEvent ),并将它们转发给可视化场景。最后,处理事件并应答它们的是单个项。例如,若点击可选择项,项通常会让场景知道它已被选中,且它还会重新绘制自身以显示选定矩形。同样,若点击并拖拽鼠标以移动可移动项,它是处理鼠标移动和自身移动的项。项交互默认是启用的,可以触发它通过调用 setInteractive ().

还可以提供自己的自定义场景交互,通过创建 QGraphicsView 子类并重实现鼠标和键事件处理程序。为简化以编程方式与视图项交互的方式,QGraphicsView 有提供映射函数 mapToScene () 和 mapFromScene (),和项访问器 () 和 itemAt ()。这些函数允许在视图坐标和场景坐标之间映射点、矩形、多边形及路径,并使用视图坐标在场景中查找项。

注意: 使用 OpenGL 视口会限制能力,可以使用 QGraphicsProxyWidget 。采用这种设置不支持所有 Widget 和样式的组合。应小心谨慎测试您的 UI 并做出必要调节。

另请参阅 QGraphicsScene , QGraphicsItem ,和 QGraphicsSceneEvent .

成员类型文档编制

enum QGraphicsView:: CacheModeFlag
flags QGraphicsView:: CacheMode

此枚举描述可以设置的标志为 QGraphicsView 的缓存模式。

常量 描述
QGraphicsView::CacheNone 0x0 所有描绘直接在视口中完成。
QGraphicsView::CacheBackground 0x1 The background is cached. This affects both custom backgrounds, and backgrounds based on the backgroundBrush property. When this flag is enabled, QGraphicsView will allocate one pixmap with the full size of the viewport.

CacheMode 类型是 typedef 对于 QFlags <CacheModeFlag>。它存储 CacheModeFlag 值的 OR 组合。

另请参阅 cacheMode .

enum QGraphicsView:: DragMode

此枚举描述视图的默认动作,在视口中按下并拖拽鼠标时。

常量 描述
QGraphicsView::NoDrag 0 什么都不发生;鼠标事件被忽略。
QGraphicsView::ScrollHandDrag 1 光标变成指向手,四处拖拽鼠标将卷动滚动条。此模式工作于 interactive 和非交互模式。
QGraphicsView::RubberBandDrag 2 将出现橡皮筋。拖拽鼠标将设置橡皮筋几何体,并选中橡皮筋涵盖的所有项。非交互式视图禁用此模式。

另请参阅 dragMode and QGraphicsScene::setSelectionArea ().

enum QGraphicsView:: OptimizationFlag
flags QGraphicsView:: OptimizationFlags

This enum describes flags that you can enable to improve rendering performance in QGraphicsView . By default, none of these flags are set. Note that setting a flag usually imposes a side effect, and this effect can vary between paint devices and platforms.

常量 描述
QGraphicsView::DontSavePainterState 0x1 当渲染时, QGraphicsView protects the painter state (see QPainter::save ()) when rendering the background or foreground, and when rendering each item. This allows you to leave the painter in an altered state (i.e., you can call QPainter::setPen () 或 QPainter::setBrush () without restoring the state after painting). However, if the items consistently do restore the state, you should enable this flag to prevent QGraphicsView from doing the same.
QGraphicsView::DontAdjustForAntialiasing 0x2 Disables QGraphicsView 's antialiasing auto-adjustment of exposed areas. Items that render antialiased lines on the boundaries of their QGraphicsItem::boundingRect () can end up rendering parts of the line outside. To prevent rendering artifacts, QGraphicsView expands all exposed regions by 2 pixels in all directions. If you enable this flag, QGraphicsView will no longer perform these adjustments, minimizing the areas that require redrawing, which improves performance. A common side effect is that items that do draw with antialiasing can leave painting traces behind on the scene as they are moved.
QGraphicsView::IndirectPainting 0x4 Since Qt 4.6, restore the old painting algorithm that calls QGraphicsView::drawItems() and QGraphicsScene::drawItems(). To be used only for compatibility with old code.

OptimizationFlags 类型是 typedef 对于 QFlags <OptimizationFlag>。它存储 OptimizationFlag 值的 OR 组合。

enum QGraphicsView:: ViewportAnchor

此枚举描述可能的锚点 QGraphicsView 可以使用,当用户重置视图大小 (或变换视图) 时。

常量 描述
QGraphicsView::NoAnchor 0 无锚点 (即:视图使场景的位置保持不变)。
QGraphicsView::AnchorViewCenter 1 视图中心的场景点用作锚点。
QGraphicsView::AnchorUnderMouse 2 鼠标下点用作锚点。

另请参阅 resizeAnchor and transformationAnchor .

enum QGraphicsView:: ViewportUpdateMode

此枚举描述如何 QGraphicsView 更新其视口,当场景内容改变 (或暴露) 时。

常量 描述
QGraphicsView::FullViewportUpdate 0 When any visible part of the scene changes or is reexposed, QGraphicsView will update the entire viewport. This approach is fastest when QGraphicsView spends more time figuring out what to draw than it would spend drawing (e.g., when very many small items are repeatedly updated). This is the preferred update mode for viewports that do not support partial updates, such as QOpenGLWidget, and for viewports that need to disable scroll optimization.
QGraphicsView::MinimalViewportUpdate 1 QGraphicsView will determine the minimal viewport region that requires a redraw, minimizing the time spent drawing by avoiding a redraw of areas that have not changed. This is QGraphicsView 's default mode. Although this approach provides the best performance in general, if there are many small visible changes on the scene, QGraphicsView might end up spending more time finding the minimal approach than it will spend drawing.
QGraphicsView::SmartViewportUpdate 2 QGraphicsView will attempt to find an optimal update mode by analyzing the areas that require a redraw.
QGraphicsView::BoundingRectViewportUpdate 4 The bounding rectangle of all changes in the viewport will be redrawn. This mode has the advantage that QGraphicsView searches only one region for changes, minimizing time spent determining what needs redrawing. The disadvantage is that areas that have not changed also need to be redrawn.
QGraphicsView::NoViewportUpdate 3 QGraphicsView will never update its viewport when the scene changes; the user is expected to control all updates. This mode disables all (potentially slow) item visibility testing in QGraphicsView , and is suitable for scenes that either require a fixed frame rate, or where the viewport is otherwise updated externally.

另请参阅 viewportUpdateMode .

特性文档编制

alignment : Qt::Alignment

This property holds the alignment of the scene in the view when the whole scene is visible.

If the whole scene is visible in the view, (i.e., there are no visible scroll bars,) the view's alignment will decide where the scene will be rendered in the view. For example, if the alignment is Qt::AlignCenter , which is default, the scene will be centered in the view, and if the alignment is ( Qt::AlignLeft | Qt::AlignTop ), the scene will be rendered in the top-left corner of the view.

访问函数:

Qt::Alignment alignment () const
void setAlignment (Qt::Alignment alignment )

backgroundBrush : QBrush

此特性保持场景的背景画笔。

此特性设置在此视图中的场景背景笔刷。用于覆盖场景的自身背景,并定义行为为 drawBackground ()。要为此视图提供自定义背景绘图,可以重实现 drawBackground () 代替。

默认情况下,此特性包含的笔刷带有 Qt::NoBrush 模式。

访问函数:

QBrush backgroundBrush () const
void setBackgroundBrush (const QBrush & brush )

另请参阅 QGraphicsScene::backgroundBrush and foregroundBrush .

cacheMode : CacheMode

此特性保持被缓存视图的哪些部分

QGraphicsView can cache pre-rendered content in a QPixmap , which is then drawn onto the viewport. The purpose of such caching is to speed up the total rendering time for areas that are slow to render. Texture, gradient and alpha blended backgrounds, for example, can be notibly slow to render; especially with a transformed view. The CacheBackground flag enables caching of the view's background. For example:

QGraphicsView view;
view.setBackgroundBrush(QImage(":/images/backgroundtile.png"));
view.setCacheMode(QGraphicsView::CacheBackground);
					

The cache is invalidated every time the view is transformed. However, when scrolling, only partial invalidation is required.

By default, nothing is cached.

访问函数:

QGraphicsView::CacheMode cacheMode () const
void setCacheMode (QGraphicsView::CacheMode mode )

另请参阅 resetCachedContent () 和 QPixmapCache .

dragMode : DragMode

此特性保持在场景中拖拽鼠标的行为,当按下鼠标左键时。

此特性定义应发生什么,当用户点击场景背景并拖拽鼠标时 (如:采用指向手光标卷动视口内容,或采用橡皮筋选择多个项)。默认值 NoDrag ,什么都不做。

此行为仅影响未被任何项所处理的鼠标点击。可以定义自定义行为通过创建子类化的 QGraphicsView 并重实现 mouseMoveEvent ().

访问函数:

QGraphicsView::DragMode dragMode () const
void setDragMode (QGraphicsView::DragMode mode )

foregroundBrush : QBrush

此特性保持场景的前景笔刷。

This property sets the foreground brush for the scene in this view. It is used to override the scene's own foreground, and defines the behavior of drawForeground (). To provide custom foreground drawing for this view, you can reimplement drawForeground () 代替。

默认情况下,此特性包含的笔刷带有 Qt::NoBrush 模式。

访问函数:

QBrush foregroundBrush () const
void setForegroundBrush (const QBrush & brush )

另请参阅 QGraphicsScene::foregroundBrush and backgroundBrush .

interactive : bool

此特性保持视图是否允许场景交互。

If enabled, this view is set to allow scene interaction. Otherwise, this view will not allow interaction, and any mouse or key events are ignored (i.e., it will act as a read-only view).

默认情况下,此特性为 true .

访问函数:

bool isInteractive () const
void setInteractive (bool allowed )

optimizationFlags : OptimizationFlags

标志可以用于微调 QGraphicsView 的性能。

QGraphicsView uses clipping, extra bounding rect adjustments, and certain other aids to improve rendering quality and performance for the common case graphics scene. However, depending on the target platform, the scene, and the viewport in use, some of these operations can degrade performance.

The effect varies from flag to flag; see the OptimizationFlags 文档编制了解细节。

默认情况下,未启用优化标志。

访问函数:

QGraphicsView::OptimizationFlags optimizationFlags () const
void setOptimizationFlags (QGraphicsView::OptimizationFlags flags )

另请参阅 setOptimizationFlag ().

renderHints : QPainter::RenderHints

此特性保持视图的默认渲染提示

这些提示用于初始化 QPainter 在绘制每个可见项之前。 QPainter 使用渲染提示来触发渲染特征,譬如:抗锯齿和平滑像素图变换。

QPainter::TextAntialiasing 是启用的,默认情况下。

范例:

QGraphicsScene scene;
scene.addRect(QRectF(-10, -10, 20, 20));
QGraphicsView view(&scene);
view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
view.show();
					

访问函数:

QPainter::RenderHints renderHints () const
void setRenderHints (QPainter::RenderHints hints )

resizeAnchor : ViewportAnchor

视图应如何定位场景,当重置视图大小时。

QGraphicsView 使用此特性决定如何在视口中定位场景,当视口 Widget 的大小变化时。默认行为 NoAnchor ,在重置大小期间场景位置保持不变;视图左上角似乎是锚定的,当重置大小时。

注意,此特性的效果是显而易见的,当场景只有一部分可见 (即:当存在滚动条) 时。否则,若整个场景都拟合在视图中, QGraphicsScene 使用视图 alignment 以在视图中定位场景。

访问函数:

QGraphicsView::ViewportAnchor resizeAnchor () const
void setResizeAnchor (QGraphicsView::ViewportAnchor anchor )

另请参阅 alignment and transformationAnchor .

rubberBandSelectionMode : Qt::ItemSelectionMode

This property holds the behavior for selecting items with a rubber band selection rectangle.

此特性定义如何选择项当使用 RubberBandDrag 拖拽模式。

默认值为 Qt::IntersectsItemShape ; all items whose shape intersects with or is contained by the rubber band are selected.

访问函数:

Qt::ItemSelectionMode rubberBandSelectionMode () const
void setRubberBandSelectionMode (Qt::ItemSelectionMode mode )

另请参阅 dragMode , (),和 rubberBandRect ().

sceneRect : QRectF

此特性保持由此视图可视化的场景区域。

The scene rectangle defines the extent of the scene, and in the view's case, this means the area of the scene that you can navigate using the scroll bars.

若未设置,或者若 null QRectF is set, this property has the same value as QGraphicsScene::sceneRect , and it changes with QGraphicsScene::sceneRect . Otherwise, the view's scene rect is unaffected by the scene.

Note that, although the scene supports a virtually unlimited size, the range of the scroll bars will never exceed the range of an integer (INT_MIN, INT_MAX). When the scene is larger than the scroll bars' values, you can choose to use translate () to navigate the scene instead.

By default, this property contains a rectangle at the origin with zero width and height.

访问函数:

QRectF sceneRect () const
void setSceneRect (const QRectF & rect )
void setSceneRect (qreal x , qreal y , qreal w , qreal h )

另请参阅 QGraphicsScene::sceneRect .

transformationAnchor : ViewportAnchor

视图在变换期间应如何定位场景。

QGraphicsView 使用此特性决定如何在视口中定位场景,当变换矩阵变化和视图坐标系变换时。默认行为 AnchorViewCenter ,确保视图中心场景点在变换期间仍然不变 (如:当旋转时,场景似乎围绕视图中心旋转)。

注意,此特性的效果是显而易见的,当场景只有一部分可见 (即:当存在滚动条) 时。否则,若整个场景都拟合在视图中, QGraphicsScene 使用视图 alignment 以在视图中定位场景。

访问函数:

QGraphicsView::ViewportAnchor transformationAnchor () const
void setTransformationAnchor (QGraphicsView::ViewportAnchor anchor )

另请参阅 alignment and resizeAnchor .

viewportUpdateMode : ViewportUpdateMode

视口应如何更新其内容。

QGraphicsView 使用此特性以决定如何更新已重新暴露 (或改变) 的场景区域。通常不需要修改此特性,但在某些情况下,这样做可以改进渲染性能。见 ViewportUpdateMode 文档编制了解特定细节。

默认值为 MinimalViewportUpdate ,其中 QGraphicsView 将更新尽可能小的视口区域,当内容改变时。

访问函数:

QGraphicsView::ViewportUpdateMode viewportUpdateMode () const
void setViewportUpdateMode (QGraphicsView::ViewportUpdateMode mode )

另请参阅 ViewportUpdateMode and cacheMode .

成员函数文档编制

QGraphicsView:: QGraphicsView ( QGraphicsScene * scene , QWidget * parent = nullptr)

构造 QGraphicsView 并将可视化场景设为 scene . parent 会被传递给 QWidget 的构造函数。

QGraphicsView:: QGraphicsView ( QWidget * parent = nullptr)

构造 QGraphicsView。 parent 会被传递给 QWidget 的构造函数。

[slot] void QGraphicsView:: invalidateScene (const QRectF & rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers)

Invalidates and schedules a redraw of layers inside rect . rect is in scene coordinates. Any cached content for layers inside rect 无条件不验证并重新绘制。

You can call this function to notify QGraphicsView of changes to the background or the foreground of the scene. It is commonly used for scenes with tile-based backgrounds to notify changes when QGraphicsView has enabled background caching.

注意, QGraphicsView 目前仅支持后台缓存 (见 QGraphicsView::CacheBackground )。此函数相当于调用 update () 若有任何层但 QGraphicsScene::BackgroundLayer 被传递。

另请参阅 QGraphicsScene::invalidate () 和 update ().

[signal, since 5.1] void QGraphicsView:: rubberBandChanged ( QRect rubberBandRect , QPointF fromScenePoint , QPointF toScenePoint )

This signal is emitted when the rubber band rect is changed. The viewport Rect is specified by rubberBandRect . The drag start position and drag end position are provided in scene points with fromScenePoint and toScenePoint .

当橡皮筋选择结束时,将采用 null 值发射此信号。

该函数在 Qt 5.1 引入。

另请参阅 rubberBandRect ().

[override virtual protected slot] void QGraphicsView:: setupViewport ( QWidget * widget )

重实现: QAbstractScrollArea::setupViewport (QWidget *viewport).

此槽被调用通过 QAbstractScrollArea after setViewport () has been called. Reimplement this function in a subclass of QGraphicsView to initialize the new viewport widget 在使用它之前。

另请参阅 setViewport ().

[slot] void QGraphicsView:: updateScene (const QList < QRectF > & rects )

调度更新场景矩形 rects .

另请参阅 QGraphicsScene::changed ().

[slot] void QGraphicsView:: updateSceneRect (const QRectF & rect )

通知 QGraphicsView 场景,场景矩形已改变。 rect is the new scene rect. If the view already has an explicitly set scene rect, this function does nothing.

另请参阅 sceneRect and QGraphicsScene::sceneRectChanged ().

[虚拟] QGraphicsView:: ~QGraphicsView ()

销毁 QGraphicsView 对象。

void QGraphicsView:: centerOn (const QPointF & pos )

卷动视口内容以确保场景坐标 pos ,居中视图。

因为 pos 是浮点坐标,而滚动条运转于整数坐标,居中只是近似。

注意: 如果项靠近 (或超出) 边框,它在视图中可见,但不居中。

另请参阅 ensureVisible ().

void QGraphicsView:: centerOn ( qreal x , qreal y )

这是重载函数。

此函数为方便起见提供。它相当于调用 centerOn( QPointF ( x , y )).

void QGraphicsView:: centerOn (const QGraphicsItem * item )

这是重载函数。

滚动视口内容以确保 item 居中视图。

另请参阅 ensureVisible ().

[override virtual protected] void QGraphicsView:: contextMenuEvent ( QContextMenuEvent * event )

重实现: QAbstractScrollArea::contextMenuEvent (QContextMenuEvent *e).

[override virtual protected] void QGraphicsView:: dragEnterEvent ( QDragEnterEvent * event )

重实现: QAbstractScrollArea::dragEnterEvent (QDragEnterEvent *event).

[override virtual protected] void QGraphicsView:: dragLeaveEvent ( QDragLeaveEvent * event )

重实现: QAbstractScrollArea::dragLeaveEvent (QDragLeaveEvent *event).

[override virtual protected] void QGraphicsView:: dragMoveEvent ( QDragMoveEvent * event )

重实现: QAbstractScrollArea::dragMoveEvent (QDragMoveEvent *event).

[virtual protected] void QGraphicsView:: drawBackground ( QPainter * painter , const QRectF & rect )

绘制场景背景使用 painter , before any items and the foreground are drawn. Reimplement this function to provide a custom background for this view.

If all you want is to define a color, texture or gradient for the background, you can call setBackgroundBrush () 代替。

All painting is done in scene 坐标。 rect is the exposed rectangle.

默认实现填充 rect using the view's backgroundBrush . If no such brush is defined (the default), the scene's drawBackground() function is called instead.

另请参阅 drawForeground () 和 QGraphicsScene::drawBackground ().

[virtual protected] void QGraphicsView:: drawForeground ( QPainter * painter , const QRectF & rect )

Draws the foreground of the scene using painter , after the background and all items are drawn. Reimplement this function to provide a custom foreground for this view.

If all you want is to define a color, texture or gradient for the foreground, you can call setForegroundBrush () 代替。

All painting is done in scene 坐标。 rect is the exposed rectangle.

默认实现填充 rect using the view's foregroundBrush . If no such brush is defined (the default), the scene's drawForeground() function is called instead.

另请参阅 drawBackground () 和 QGraphicsScene::drawForeground ().

[override virtual protected] void QGraphicsView:: dropEvent ( QDropEvent * event )

重实现: QAbstractScrollArea::dropEvent (QDropEvent *event).

void QGraphicsView:: ensureVisible (const QRectF & rect , int xmargin = 50, int ymargin = 50)

卷动视口内容以便场景矩形 rect 可见,采用指定边距以像素为单位按 xmargin and ymargin 。若无法到达指定矩形,内容卷动到最近有效位置。2 边距的默认值为 50 像素。

另请参阅 centerOn ().

void QGraphicsView:: ensureVisible ( qreal x , qreal y , qreal w , qreal h , int xmargin = 50, int ymargin = 50)

这是重载函数。

此函数为方便起见提供。它相当于调用 ensureVisible( QRectF ( x , y , w , h ), xmargin , ymargin ).

void QGraphicsView:: ensureVisible (const QGraphicsItem * item , int xmargin = 50, int ymargin = 50)

这是重载函数。

滚动视口内容以便居中项 item 可见,采用指定边距以像素为单位按 xmargin and ymargin 。若无法到达指定点,内容卷动到最近有效位置。2 边距的默认值为 50 像素。

另请参阅 centerOn ().

[override virtual protected] bool QGraphicsView:: event ( QEvent * event )

重实现: QAbstractScrollArea::event (QEvent *event).

void QGraphicsView:: fitInView (const QRectF & rect , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)

比例缩放视图矩阵并卷动滚动条以确保场景矩形 rect 拟合在视口内。 rect must be inside the scene rect; otherwise, fitInView() cannot guarantee that the whole rect is visible.

This function keeps the view's rotation, translation, or shear. The view is scaled according to aspectRatioMode . rect will be centered in the view if it does not fit tightly.

It's common to call fitInView() from inside a reimplementation of resizeEvent (), to ensure that the whole scene, or parts of the scene, scales automatically to fit the new size of the viewport as the view is resized. Note though, that calling fitInView() from inside resizeEvent () can lead to unwanted resize recursion, if the new transformation toggles the automatic state of the scrollbars. You can toggle the scrollbar policies to always on or always off to prevent this (see horizontalScrollBarPolicy () 和 verticalScrollBarPolicy ()).

rect is empty, or if the viewport is too small, this function will do nothing.

另请参阅 setTransform (), ensureVisible (),和 centerOn ().

void QGraphicsView:: fitInView ( qreal x , qreal y , qreal w , qreal h , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)

这是重载函数。

This convenience function is equivalent to calling fitInView( QRectF ( x , y , w , h ), aspectRatioMode ).

另请参阅 ensureVisible () 和 centerOn ().

void QGraphicsView:: fitInView (const QGraphicsItem * item , Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)

这是重载函数。

Ensures that item fits tightly inside the view, scaling the view according to aspectRatioMode .

另请参阅 ensureVisible () 和 centerOn ().

[override virtual protected] void QGraphicsView:: focusInEvent ( QFocusEvent * event )

重实现: QWidget::focusInEvent (QFocusEvent *event).

[override virtual protected] bool QGraphicsView:: focusNextPrevChild ( bool next )

重实现: QWidget::focusNextPrevChild (bool next).

[override virtual protected] void QGraphicsView:: focusOutEvent ( QFocusEvent * event )

重实现: QWidget::focusOutEvent (QFocusEvent *event).

[override virtual protected] void QGraphicsView:: inputMethodEvent ( QInputMethodEvent * event )

重实现: QWidget::inputMethodEvent (QInputMethodEvent *event).

[override virtual] QVariant QGraphicsView:: inputMethodQuery ( Qt::InputMethodQuery query ) const

重实现: QWidget::inputMethodQuery (Qt::InputMethodQuery query) const.

bool QGraphicsView:: isTransformed () const

返回 true if the view is transformed (i.e., a non-identity transform has been assigned, or the scrollbars are adjusted).

另请参阅 setTransform (), horizontalScrollBar (),和 verticalScrollBar ().

QGraphicsItem *QGraphicsView:: itemAt (const QPoint & pos ) const

Returns the item at position pos , which is in viewport coordinates. If there are several items at this position, this function returns the topmost item.

范例:

void CustomView::mousePressEvent(QMouseEvent *event)
{
    if (QGraphicsItem *item = itemAt(event->pos())) {
        qDebug() << "You clicked on item" << item;
    } else {
        qDebug("You didn't click on an item.");
    }
}
					

另请参阅 () 和 排序 .

QGraphicsItem *QGraphicsView:: itemAt ( int x , int y ) const

这是重载函数。

This function is provided for convenience. It's equivalent to calling itemAt( QPoint ( x , y )).

QList < QGraphicsItem *> QGraphicsView:: () const

Returns a list of all the items in the associated scene, in descending stacking order (i.e., the first item in the returned list is the uppermost item).

另请参阅 QGraphicsScene::items () 和 排序 .

QList < QGraphicsItem *> QGraphicsView:: (const QPoint & pos ) const

Returns a list of all the items at the position pos in the view. The items are listed in descending stacking order (i.e., the first item in the list is the uppermost item, and the last item is the lowermost item). pos is in viewport coordinates.

This function is most commonly called from within mouse event handlers in a subclass in QGraphicsView . pos is in untransformed viewport coordinates, just like QMouseEvent::pos ().

void CustomView::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "There are" << items(event->pos()).size()
             << "items at position" << mapToScene(event->pos());
}
					

另请参阅 QGraphicsScene::items () 和 排序 .

QList < QGraphicsItem *> QGraphicsView:: ( int x , int y ) const

This function is provided for convenience. It's equivalent to calling items( QPoint ( x , y )).

QList < QGraphicsItem *> QGraphicsView:: (const QRect & rect , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

这是重载函数。

Returns a list of all the items that, depending on mode , are either contained by or intersect with rect . rect is in viewport coordinates.

默认值对于 mode is Qt::IntersectsItemShape ; all items whose exact shape intersects with or is contained by rect 被返回。

The items are sorted in descending stacking order (i.e., the first item in the returned list is the uppermost item).

另请参阅 itemAt (), (), mapToScene (),和 排序 .

QList < QGraphicsItem *> QGraphicsView:: ( int x , int y , int w , int h , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

This convenience function is equivalent to calling items( QRectF ( x , y , w , h ), mode ).

QList < QGraphicsItem *> QGraphicsView:: (const QPolygon & polygon , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

这是重载函数。

Returns a list of all the items that, depending on mode , are either contained by or intersect with polygon . polygon is in viewport coordinates.

默认值对于 mode is Qt::IntersectsItemShape ; all items whose exact shape intersects with or is contained by polygon 被返回。

The items are sorted by descending stacking order (i.e., the first item in the returned list is the uppermost item).

另请参阅 itemAt (), (), mapToScene (),和 排序 .

QList < QGraphicsItem *> QGraphicsView:: (const QPainterPath & path , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const

这是重载函数。

Returns a list of all the items that, depending on mode , are either contained by or intersect with path . path is in viewport coordinates.

默认值对于 mode is Qt::IntersectsItemShape ; all items whose exact shape intersects with or is contained by path 被返回。

另请参阅 itemAt (), (), mapToScene (),和 排序 .

[override virtual protected] void QGraphicsView:: keyPressEvent ( QKeyEvent * event )

重实现: QAbstractScrollArea::keyPressEvent (QKeyEvent *e).

[override virtual protected] void QGraphicsView:: keyReleaseEvent ( QKeyEvent * event )

重实现: QWidget::keyReleaseEvent (QKeyEvent *event).

QPoint QGraphicsView:: mapFromScene (const QPointF & point ) const

返回场景坐标 point 到视口坐标。

另请参阅 mapToScene ().

QPolygon QGraphicsView:: mapFromScene (const QRectF & rect ) const

返回场景矩形 rect 到视口坐标多边形。

另请参阅 mapToScene ().

QPolygon QGraphicsView:: mapFromScene (const QPolygonF & polygon ) const

返回场景坐标多边形 polygon 到视口坐标多边形。

另请参阅 mapToScene ().

QPainterPath QGraphicsView:: mapFromScene (const QPainterPath & path ) const

Returns the scene coordinate painter path path to a viewport coordinate painter path.

另请参阅 mapToScene ().

QPoint QGraphicsView:: mapFromScene ( qreal x , qreal y ) const

This function is provided for convenience. It's equivalent to calling mapFromScene( QPointF ( x , y )).

QPolygon QGraphicsView:: mapFromScene ( qreal x , qreal y , qreal w , qreal h ) const

This function is provided for convenience. It's equivalent to calling mapFromScene( QRectF ( x , y , w , h )).

QPointF QGraphicsView:: mapToScene (const QPoint & point ) const

返回的视口坐标 point 被映射到场景坐标。

Note: It can be useful to map the whole rectangle covered by the pixel at point instead of the point itself. To do this, you can call mapToScene( QRect ( point , QSize (2, 2))).

另请参阅 mapFromScene ().

QPolygonF QGraphicsView:: mapToScene (const QRect & rect ) const

返回视口矩形 rect 被映射到场景坐标多边形。

另请参阅 mapFromScene ().

QPolygonF QGraphicsView:: mapToScene (const QPolygon & polygon ) const

返回的视口多边形 polygon 被映射到场景坐标多边形。

另请参阅 mapFromScene ().

QPainterPath QGraphicsView:: mapToScene (const QPainterPath & path ) const

返回的视口描绘器路径 path 被映射到场景坐标描绘器路径。

另请参阅 mapFromScene ().

QPointF QGraphicsView:: mapToScene ( int x , int y ) const

此函数为方便起见提供。它相当于调用 mapToScene( QPoint ( x , y )).

QPolygonF QGraphicsView:: mapToScene ( int x , int y , int w , int h ) const

此函数为方便起见提供。它相当于调用 mapToScene( QRect ( x , y , w , h )).

[override virtual protected] void QGraphicsView:: mouseDoubleClickEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mouseDoubleClickEvent (QMouseEvent *e).

[override virtual protected] void QGraphicsView:: mouseMoveEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mouseMoveEvent (QMouseEvent *e).

[override virtual protected] void QGraphicsView:: mousePressEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mousePressEvent (QMouseEvent *e).

[override virtual protected] void QGraphicsView:: mouseReleaseEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mouseReleaseEvent (QMouseEvent *e).

[override virtual protected] void QGraphicsView:: paintEvent ( QPaintEvent * event )

重实现: QAbstractScrollArea::paintEvent (QPaintEvent *event).

void QGraphicsView:: render ( QPainter * painter , const QRectF & target = QRectF(), const QRect & source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio)

渲染 source 矩形按视图坐标,从场景到 target 按描绘设备坐标,使用 painter 。此函数对于将视图内容捕获到描绘设备很有用,譬如 QImage (例如,截取屏幕截图),或用于打印到 QPrinter。例如:

QGraphicsScene scene;
scene.addItem(...
...
QGraphicsView view(&scene);
view.show();
...
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
QPainter painter(&printer);
// print, fitting the viewport contents into a full page
view.render(&painter);
// print the upper half of the viewport into the lower.
// half of the page.
QRect viewport = view.viewport()->rect();
view.render(&painter,
            QRectF(0, printer.height() / 2,
                   printer.width(), printer.height() / 2),
            viewport.adjusted(0, 0, 0, -viewport.height() / 2));
					

source 是 null 矩形,此函数将使用 viewport ()-> rect () 来确定要绘制什么。若 target 是 null 矩形,完整尺度的 painter 描绘设备 (如:对于 QPrinter 是页面大小) 会被使用。

将变换源矩形内容根据 aspectRatioMode 以拟合到目标矩形。默认情况下,宽高比保持不变,且 source 被比例缩放以拟合到 target .

另请参阅 QGraphicsScene::render ().

void QGraphicsView:: resetCachedContent ()

Resets any cached content. Calling this function will clear QGraphicsView 's cache. If the current cache mode is CacheNone ,此函数什么都不做。

This function is called automatically for you when the backgroundBrush or QGraphicsScene::backgroundBrush properties change; you only need to call this function if you have reimplemented QGraphicsScene::drawBackground () 或 QGraphicsView::drawBackground () to draw a custom background, and need to trigger a full redraw.

另请参阅 cacheMode ().

void QGraphicsView:: resetTransform ()

Resets the view transformation to the identity matrix.

另请参阅 transform () 和 setTransform ().

[override virtual protected] void QGraphicsView:: resizeEvent ( QResizeEvent * event )

重实现: QAbstractScrollArea::resizeEvent (QResizeEvent *event).

void QGraphicsView:: rotate ( qreal angle )

Rotates the current view transformation angle degrees clockwise.

另请参阅 setTransform (), transform (), scale (), shear (),和 translate ().

[since 5.1] QRect QGraphicsView:: rubberBandRect () const

This functions returns the current rubber band area (in viewport coordinates) if the user is currently doing an itemselection with rubber band. When the user is not using the rubber band this functions returns (a null) QRectF().

Notice that part of this QRect can be outise the visual viewport. It can e.g contain negative values.

该函数在 Qt 5.1 引入。

另请参阅 rubberBandSelectionMode and rubberBandChanged ().

void QGraphicsView:: scale ( qreal sx , qreal sy )

比例缩放当前视图变换按 ( sx , sy ).

另请参阅 setTransform (), transform (), rotate (), shear (),和 translate ().

QGraphicsScene *QGraphicsView:: scene () const

Returns a pointer to the scene that is currently visualized in the view. If no scene is currently visualized, nullptr 被返回。

另请参阅 setScene ().

[override virtual protected] void QGraphicsView:: scrollContentsBy ( int dx , int dy )

重实现: QAbstractScrollArea::scrollContentsBy (int dx, int dy).

void QGraphicsView:: setOptimizationFlag ( QGraphicsView::OptimizationFlag flag , bool enabled = true)

Enables flag if enabled is true; otherwise disables flag .

另请参阅 optimizationFlags .

void QGraphicsView:: setRenderHint ( QPainter::RenderHint hint , bool enabled = true)

enabled 为 true,渲染提示 hint 被启用;否则被禁用。

另请参阅 renderHints .

void QGraphicsView:: setScene ( QGraphicsScene * scene )

把当前场景设为 scene 。若 scene is already being viewed, this function does nothing.

When a scene is set on a view, the QGraphicsScene::changed () signal is automatically connected to this view's updateScene () slot, and the view's scroll bars are adjusted to fit the size of the scene.

视图未拥有所有权对于 scene .

另请参阅 scene ().

void QGraphicsView:: setTransform (const QTransform & matrix , bool combine = false)

将视图的当前变换矩阵设为 matrix .

combine 为 true,则 matrix is combined with the current matrix; otherwise, matrix 替换 the current matrix. combine is false by default.

The transformation matrix transforms the scene into view coordinates. Using the default transformation, provided by the identity matrix, one pixel in the view represents one unit in the scene (e.g., a 10x10 rectangular item is drawn using 10x10 pixels in the view). If a 2x2 scaling matrix is applied, the scene will be drawn in 1:2 (e.g., a 10x10 rectangular item is then drawn using 20x20 pixels in the view).

范例:

QGraphicsScene scene;
scene.addText("GraphicsView rotated clockwise");
QGraphicsView view(&scene);
view.rotate(90); // the text is rendered with a 90 degree clockwise rotation
view.show();
					

To simplify interation with items using a transformed view, QGraphicsView provides mapTo... and mapFrom... functions that can translate between scene and view coordinates. For example, you can call mapToScene () to map a view coordinate to a floating point scene coordinate, or mapFromScene () to map from floating point scene coordinates to view coordinates.

另请参阅 transform (), rotate (), scale (), shear (),和 translate ().

void QGraphicsView:: shear ( qreal sh , qreal sv )

剪切当前视图变换按 ( sh , sv ).

另请参阅 setTransform (), transform (), rotate (), scale (),和 translate ().

[override virtual protected] void QGraphicsView:: showEvent ( QShowEvent * event )

重实现: QWidget::showEvent (QShowEvent *event).

[override virtual] QSize QGraphicsView:: sizeHint () const

重实现: QAbstractScrollArea::sizeHint () const.

QTransform QGraphicsView:: transform () const

返回用于视图的当前变换矩阵。若当前变换未设置,返回恒等矩阵。

另请参阅 setTransform (), rotate (), scale (), shear (),和 translate ().

void QGraphicsView:: translate ( qreal dx , qreal dy )

平移当前视图变换按 ( dx , dy ).

另请参阅 setTransform (), transform (), rotate (),和 shear ().

[override virtual protected] bool QGraphicsView:: viewportEvent ( QEvent * event )

重实现: QAbstractScrollArea::viewportEvent (QEvent *event).

QTransform QGraphicsView:: viewportTransform () const

返回将场景坐标映射到视口坐标的矩阵。

另请参阅 mapToScene () 和 mapFromScene ().

[override virtual protected] void QGraphicsView:: wheelEvent ( QWheelEvent * event )

重实现: QAbstractScrollArea::wheelEvent (QWheelEvent *e).