QGraphicsView 类

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

头: #include <QGraphicsView>
qmake: QT += widgets
Since: Qt 4.2
继承: QAbstractScrollArea
继承者:

QChartView

公共类型

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

特性

公共函数

QGraphicsView (QWidget * parent = Q_NULLPTR)
QGraphicsView (QGraphicsScene * scene , QWidget * parent = Q_NULLPTR)
~QGraphicsView ()
Qt::Alignment alignment () const
QBrush backgroundBrush () const
CacheMode cacheMode () const
void centerOn (const QPointF & pos )
void centerOn (qreal x , qreal y )
void centerOn (const QGraphicsItem * item )
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 *> items () const
QList<QGraphicsItem *> items (const QPoint & pos ) const
QList<QGraphicsItem *> items (int x , int y ) const
QList<QGraphicsItem *> items (const QRect & rect , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> items (int x , int y , int w , int h , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> items (const QPolygon & polygon , Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> items (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
QMatrix matrix () const
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 resetMatrix ()
void resetTransform ()
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 (CacheMode mode )
void setDragMode (DragMode mode )
void setForegroundBrush (const QBrush & brush )
void setInteractive (bool allowed )
void setMatrix (const QMatrix & matrix , bool combine = false)
void setOptimizationFlag (OptimizationFlag flag , bool enabled = true)
void setOptimizationFlags (OptimizationFlags flags )
void setRenderHint (QPainter::RenderHint hint , bool enabled = true)
void setRenderHints (QPainter::RenderHints hints )
void setResizeAnchor (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 (ViewportAnchor anchor )
void setViewportUpdateMode (ViewportUpdateMode mode )
void shear (qreal sh , qreal sv )
QTransform transform () const
ViewportAnchor transformationAnchor () const
void translate (qreal dx , qreal dy )
QTransform viewportTransform () const
ViewportUpdateMode viewportUpdateMode () const

重实现公共函数

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

公共槽

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

保护槽

virtual void setupViewport (QWidget * widget )

额外继承成员

详细描述

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

QGraphicsView visualizes the contents of a QGraphicsScene 在可卷动视口。要创建带几何项的场景,见 QGraphicsScene 's documentation. QGraphicsView 属于 图形视图框架 .

To visualize a scene, you start by constructing a QGraphicsView object, passing the address of the scene you want to visualize to QGraphicsView 's constructor. Alternatively, you can call setScene () 以在稍后点设置场景。之后调用 show (),默认情况下,视图将卷动到场景中心并显示此时可见的任何项。例如:

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

可以明确卷动到场景中的任何位置通过使用滚动条,或通过调用 centerOn ()。通过将点传递给 centerOn (), QGraphicsView will scroll its viewport to ensure that the point is centered in the view. An overload is provided for scrolling to a QGraphicsItem ,在此情况下 QGraphicsView will see to that the center of the item is centered in the view. If all you want is to ensure that a certain area is visible, (but not necessarily centered,) you can call ensureVisible () 代替。

QGraphicsView can be used to visualize a whole scene, or only parts of it. The visualized area is by default detected automatically when the view is displayed for the first time (by calling QGraphicsScene::itemsBoundingRect ())。要自己设置可视化区域矩形,可以调用 setSceneRect ()。这将适当调节滚动条范围。注意:尽管场景支持几乎无限大小,但滚动条范围从不超过整数 (INT_MIN,INT_MAX) 范围。

QGraphicsView visualizes the scene by calling render ()。默认情况下,项被绘制到视口通过使用常规 QPainter , and using default render hints. To change the default render hints that QGraphicsView passes to QPainter 当描绘项时,可以调用 setRenderHints ().

默认情况下, QGraphicsView provides a regular QWidget 为视口 Widget。可以访问此 Widget 通过调用 viewport (),或者可以替换它通过调用 setViewport (). To render using OpenGL, simply call setViewport (new QGLWidget). QGraphicsView takes ownership of the viewport widget.

QGraphicsView supports affine transformations, using 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.

You can interact with the items on the scene by using the mouse and keyboard. QGraphicsView translates the mouse and key events into scene 事件 (事件继承自 QGraphicsSceneEvent ,), and forward them to the visualized scene. In the end, it's the individual item that handles the events and reacts to them. For example, if you click on a selectable item, the item will typically let the scene know that it has been selected, and it will also redraw itself to display a selection rectangle. Similiary, if you click and drag the mouse to move a movable item, it's the item that handles the mouse moves and moves itself. Item interaction is enabled by default, and you can toggle it by calling setInteractive ().

You can also provide your own custom scene interaction, by creating a subclass of QGraphicsView , and reimplementing the mouse and key event handlers. To simplify how you programmatically interact with items in the view, QGraphicsView provides the mapping functions mapToScene () 和 mapFromScene (),和项访问器 items () 和 itemAt ()。这些函数允许在视图坐标和场景坐标之间映射点、矩形、多边形及路径,并使用视图坐标在场景中查找项。

另请参阅 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::DontClipPainter 0x1 此值已过时且不起作用。
QGraphicsView::DontSavePainterState 0x2 当渲染时, 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 0x4 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 0x8 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.

该枚举在 Qt 4.3 引入或被修改。

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 QGLWidget, 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.

该枚举在 Qt 4.3 引入或被修改。

另请参阅 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.

访问函数:

CacheMode cacheMode () const
void setCacheMode (CacheMode mode )

另请参阅 resetCachedContent () 和 QPixmapCache .

dragMode : DragMode

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

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

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

访问函数:

DragMode dragMode () const
void setDragMode (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 文档编制了解细节。

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

该特性在 Qt 4.3 引入。

访问函数:

OptimizationFlags optimizationFlags () const
void setOptimizationFlags (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 以在视图中定位场景。

访问函数:

ViewportAnchor resizeAnchor () const
void setResizeAnchor (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 4.3 引入。

访问函数:

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

另请参阅 dragMode , items (),和 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 以在视图中定位场景。

访问函数:

ViewportAnchor transformationAnchor () const
void setTransformationAnchor (ViewportAnchor anchor )

另请参阅 alignment and resizeAnchor .

viewportUpdateMode : ViewportUpdateMode

视口应如何更新其内容。

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

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

该特性在 Qt 4.3 引入。

访问函数:

ViewportUpdateMode viewportUpdateMode () const
void setViewportUpdateMode (ViewportUpdateMode mode )

另请参阅 ViewportUpdateMode and cacheMode .

成员函数文档编制

QGraphicsView:: QGraphicsView ( QWidget * parent = Q_NULLPTR)

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

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

构造 QGraphicsView and sets the visualized scene to scene . parent 会被传递给 QWidget 的构造函数。

QGraphicsView:: ~QGraphicsView ()

销毁 QGraphicsView 对象。

void QGraphicsView:: centerOn (const QPointF & pos )

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

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

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

另请参阅 ensureVisible ().

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

这是重载函数。

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

void QGraphicsView:: centerOn (const QGraphicsItem * item )

这是重载函数。

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

另请参阅 ensureVisible ().

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

重实现自 QWidget::contextMenuEvent ().

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

重实现自 QWidget::dragEnterEvent ().

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

重实现自 QWidget::dragLeaveEvent ().

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

重实现自 QWidget::dragMoveEvent ().

[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 ().

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

重实现自 QWidget::dropEvent ().

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)

这是重载函数。

This function is provided for convenience. It's equivalent to calling 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 ().

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

重实现自 QObject::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)

这是重载函数。

此方便函数相当于调用 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 ().

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

重实现自 QWidget::focusInEvent ().

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

重实现自 QWidget::focusNextPrevChild ().

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

重实现自 QWidget::focusOutEvent ().

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

重实现自 QWidget::inputMethodEvent ().

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

重实现自 QWidget::inputMethodQuery ().

[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 ().

bool QGraphicsView:: isTransformed () const

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

该函数在 Qt 4.6 引入。

另请参阅 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.");
    }
}
					

另请参阅 items () 和 排序 .

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:: items () 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:: items (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:: items ( int x , int y ) const

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

QList < QGraphicsItem *> QGraphicsView:: items (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 (), items (), mapToScene (),和 排序 .

QList < QGraphicsItem *> QGraphicsView:: items ( 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 ).

该函数在 Qt 4.3 引入。

QList < QGraphicsItem *> QGraphicsView:: items (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 (), items (), mapToScene (),和 排序 .

QList < QGraphicsItem *> QGraphicsView:: items (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 (), items (), mapToScene (),和 排序 .

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

重实现自 QWidget::keyPressEvent ().

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

重实现自 QWidget::keyReleaseEvent ().

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

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

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

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

QMatrix QGraphicsView:: matrix () const

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

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

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

重实现自 QWidget::mouseDoubleClickEvent ().

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

重实现自 QWidget::mouseMoveEvent ().

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

重实现自 QWidget::mousePressEvent ().

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

重实现自 QWidget::mouseReleaseEvent ().

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

重实现自 QWidget::paintEvent ().

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

渲染 source 矩形按视图坐标,从场景到 target 按描绘设备坐标,使用 painter 。此函数对于将视图内容捕获到描绘设备很有用,譬如 QImage (e.g., to take a screenshot), or for printing to 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 's paint device (e.g., for a QPrinter , the page size) will be used.

将变换源矩形内容根据 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:: resetMatrix ()

Resets the view transformation matrix to the identity matrix.

另请参阅 resetTransform ().

void QGraphicsView:: resetTransform ()

Resets the view transformation to the identity matrix.

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

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

重实现自 QWidget::resizeEvent ().

void QGraphicsView:: rotate ( qreal angle )

Rotates the current view transformation angle degrees clockwise.

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

[signal] 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 ().

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, 0 is returned.

另请参阅 setScene ().

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

重实现自 QAbstractScrollArea::scrollContentsBy ().

void QGraphicsView:: setMatrix (const QMatrix & matrix , bool combine = false)

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

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

The transformation matrix tranforms 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 提供 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.

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

void QGraphicsView:: setOptimizationFlag ( 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 is true, then matrix is combined with the current matrix; otherwise, matrix 替换 the current matrix. combine is false by default.

The transformation matrix tranforms 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 提供 mapTo ... and mapFrom ... functions that can translate between scene and view coordinates. For example, you can call mapToScene () to map a view coordiate to a floating point scene coordinate, or mapFromScene () to map from floating point scene coordinates to view coordinates.

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

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

重实现自 QAbstractScrollArea::setupViewport ().

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

另请参阅 setViewport ().

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

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

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

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

重实现自 QWidget::showEvent ().

[virtual] QSize QGraphicsView:: sizeHint () const

重实现自 QWidget::sizeHint ().

QTransform QGraphicsView:: transform () const

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

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

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

翻译当前视图变换按 ( dx , dy ).

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

[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 ().

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

重实现自 QAbstractScrollArea::viewportEvent ().

QTransform QGraphicsView:: viewportTransform () const

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

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

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

重实现自 QWidget::wheelEvent ().