QGraphicsView Class

QGraphicsView 类提供了一个用于显示QGraphicsScene内容的部件。

Header: #include <QGraphicsView>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
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(QWidget *parent = nullptr)
QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr)
virtual ~QGraphicsView()
Qt::Alignment alignment() const
QBrush backgroundBrush() const
QGraphicsView::CacheMode cacheMode() const
void centerOn(const QPointF &pos)
void centerOn(const QGraphicsItem *item)
void centerOn(qreal x, qreal y)
QGraphicsView::DragMode dragMode() const
void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50)
void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50)
void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50)
void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
void fitInView(const QGraphicsItem *item, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
void fitInView(qreal x, qreal y, qreal w, qreal h, 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(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
QPainterPath mapFromScene(const QPainterPath &path) const
QPoint mapFromScene(const QPointF &point) const
QPolygon mapFromScene(const QPolygonF &polygon) const
QPolygon mapFromScene(const QRectF &rect) const
QPoint mapFromScene(qreal x, qreal y) const
QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const
QPainterPath mapToScene(const QPainterPath &path) const
QPointF mapToScene(const QPoint &point) const
QPolygonF mapToScene(const QPolygon &polygon) const
QPolygonF mapToScene(const QRect &rect) 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 。您可以调用viewport() 访问该窗口部件,也可以调用setViewport() 替换该窗口部件。要使用 OpenGL 渲染,只需调用 setViewport(newQOpenGLWidget) 即可。QGraphicsView 拥有视口部件的所有权。

QGraphicsView 支持仿射变换,使用QTransform 。您可以向setTransform() 传递一个矩阵,也可以调用rotate(),scale(),translate() 或shear() 中的一个便捷函数。最常见的两种变换是缩放(用于实现缩放)和旋转。在变换过程中,QGraphicsView 会保持视图中心固定不变。由于场景对齐(setAlignment()) ,平移视图不会产生视觉影响。

您可以使用鼠标和键盘与场景中的项目进行交互。QGraphicsView 会将鼠标和键盘事件转化为场景事件(继承QGraphicsSceneEvent, 的事件),并将它们转发到可视化场景中。最终,处理事件并对其做出反应的是单个项目。例如,如果您点击一个可选择的项目,该项目通常会让场景知道它已被选中,同时它也会重绘自身以显示一个选择矩形。同样,如果您单击并拖动鼠标移动一个可移动的项目,该项目会处理鼠标移动并移动自身。默认情况下,项目交互是启用的,你可以通过调用setInteractive() 来切换它。

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

QOpenGLWidget 用作视口时,可支持立体渲染。使用的模式与QOpenGLWidget::paintGL 相同。要启用立体渲染,请启用QSurfaceFormat::StereoBuffers 标志。由于内部是如何处理该标记的,因此应在使用QSurfaceFormat::setDefaultFormat() 创建窗口之前全局设置QSurfaceFormat::StereoBuffers 标记。如果启用了该标志,并且硬件支持立体渲染,那么drawBackground() 和drawForeground() 将在每帧触发两次。调用QOpenGLWidget::currentTargetBuffer() 可查询当前绘制到哪个缓冲区。

注意: 使用 OpenGL 视口限制了使用QGraphicsProxyWidget 的能力。这种设置无法支持所有部件和样式的组合。您应该仔细测试您的用户界面,并进行必要的调整。

另请参阅 QGraphicsScene,QGraphicsItem, 和QGraphicsSceneEvent

成员类型文档

枚举 QGraphicsView::CacheModeFlag
flags QGraphicsView::CacheMode

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

常量说明
QGraphicsView::CacheNone0x0所有绘画都直接在视口中完成。
QGraphicsView::CacheBackground0x1背景会被缓存。这既影响自定义背景,也影响基于backgroundBrush 属性的背景。启用此标志后,QGraphicsView 将分配一个视口全尺寸的像素图。

CacheMode 类型是QFlags<CacheModeFlag> 的类型定义。它存储 CacheModeFlag 值的 OR 组合。

另请参见 cacheMode

enum QGraphicsView::DragMode

该枚举描述了在视口中按下并拖动鼠标时视图的默认操作。

常量说明
QGraphicsView::NoDrag0什么也不会发生;鼠标事件被忽略。
QGraphicsView::ScrollHandDrag1光标将变为指向手,拖动鼠标将滚动滚动条。该模式在interactive 和非交互模式下均有效。
QGraphicsView::RubberBandDrag2会出现一条橡皮筋。拖动鼠标会设置橡皮筋的几何形状,橡皮筋覆盖的所有项目都会被选中。非交互式视图中禁用此模式。

另请参阅 dragModeQGraphicsScene::setSelectionArea()。

枚举 QGraphicsView::OptimizationFlag
flags QGraphicsView::OptimizationFlags

该枚举描述了可以启用的标志,以提高QGraphicsView 中的渲染性能。默认情况下,这些标志均未设置。请注意,设置标志通常会产生副作用,而且这种作用会因绘画设备和平台而异。

常量说明
QGraphicsView::DontSavePainterState0x1渲染时,QGraphicsView 会在渲染背景或前景以及渲染每个项目时保护绘制器状态(请参阅QPainter::save() )。这样就可以让绘制器处于改变的状态(即可以调用QPainter::setPen() 或QPainter::setBrush() 而无需在绘制后恢复状态)。不过,如果项目一直在恢复状态,则应启用此标记,以防止QGraphicsView 也这样做。
QGraphicsView::DontAdjustForAntialiasing0x2禁用QGraphicsView 的暴露区域抗锯齿自动调整功能。在QGraphicsItem::boundingRect() 边界上渲染抗锯齿线条的项目最终可能会在外部渲染部分线条。为防止出现渲染假象,QGraphicsView 会将所有暴露区域向各个方向扩展 2 个像素。如果启用此标记,QGraphicsView 将不再执行这些调整,从而最大限度地减少需要重绘的区域,从而提高性能。一个常见的副作用是,使用抗锯齿绘制的项目在移动时会在场景中留下绘制痕迹。
QGraphicsView::IndirectPainting0x4自 Qt 4.6 起,恢复了调用 QGraphicsView::drawItems() 和 QGraphicsScene::drawItems() 的旧绘画算法。仅用于与旧代码兼容。

OptimizationFlags 类型是QFlags<OptimizationFlag> 的类型定义。它存储 OptimizationFlag 值的 OR 组合。

enum QGraphicsView::ViewportAnchor

该枚举描述了QGraphicsView 在用户调整视图大小或转换视图时可以使用的锚点。

常量说明
QGraphicsView::NoAnchor0无锚点,即视图保持场景位置不变。
QGraphicsView::AnchorViewCenter1视图中心的场景点被用作锚点。
QGraphicsView::AnchorUnderMouse2鼠标下方的点被用作锚点。

另请参阅 resizeAnchortransformationAnchor

enum QGraphicsView::ViewportUpdateMode

该枚举描述了QGraphicsView 在场景内容发生变化或显示时如何更新视口。

常量描述
QGraphicsView::FullViewportUpdate0当场景的任何可见部分发生变化或重新曝光时,QGraphicsView 将更新整个视口。当QGraphicsView 需要花费比绘制更多的时间来确定要绘制的内容时(例如,当许多小项目被重复更新时),这种方法是最快的。对于不支持部分更新的视口(如QOpenGLWidget )和需要禁用滚动优化的视口,这是首选的更新模式。
QGraphicsView::MinimalViewportUpdate1QGraphicsView 将确定需要重绘的最小视口区域,通过避免重绘未发生变化的区域来减少绘制时间。这是 的默认模式。虽然这种方法在一般情况下能提供最佳性能,但如果场景中有许多可见的微小变化, 最终可能会花费比绘制更多的时间来寻找最小方法。QGraphicsView QGraphicsView
QGraphicsView::SmartViewportUpdate2QGraphicsView 因此,我们将尝试通过分析需要重绘的区域来找到最佳更新模式。
QGraphicsView::BoundingRectViewportUpdate4视口中所有变化的边界矩形都将被重绘。这种模式的优点是QGraphicsView 只搜索一个区域的变化,从而最大限度地减少了确定哪些区域需要重绘所花费的时间。缺点是没有变化的区域也需要重新绘制。
QGraphicsView::NoViewportUpdate3QGraphicsView 当场景发生变化时,"渲染 "模式永远不会更新视口;用户需要控制所有更新。该模式在 中禁用了所有(可能很慢)的项目可见性测试,适用于需要固定帧频的场景,或需要从外部更新视口的场景。QGraphicsView

另请参阅 viewportUpdateMode

属性文档

alignment : Qt::Alignment

当整个场景可见时,该属性表示场景在视图中的对齐方式。

如果整个场景在视图中可见(即没有可见的滚动条),视图的对齐方式将决定场景在视图中的渲染位置。例如,如果对齐方式为Qt::AlignCenter (默认值),场景将在视图中居中显示;如果对齐方式为 (Qt::AlignLeft |Qt::AlignTop),场景将在视图的左上角显示。

访问功能:

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

backgroundBrush : QBrush

该属性保存场景的背景画笔。

此属性设置此视图中场景的背景画笔。它用于覆盖场景自身的背景,并定义drawBackground() 的行为。要为该视图提供自定义背景画笔,可以重新实现drawBackground() 来代替。

默认情况下,该属性包含一个带有Qt::NoBrush 图案的画笔。

访问函数:

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

另请参见 QGraphicsScene::backgroundBrushforegroundBrush

cacheMode : CacheMode

该属性用于缓存视图的哪些部分

QGraphicsView 可以在 中缓存预先渲染的内容,然后将其绘制到视口上。这种缓存的目的是加快渲染速度较慢区域的总渲染时间。例如,纹理、渐变和 Alpha 混合背景的渲染速度会明显较慢,特别是在转换视图的情况下。 标志可以缓存视图的背景。例如QPixmap CacheBackground

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

每次转换视图时,缓存都会失效。但在滚动时,只需要部分失效。

默认情况下,不缓存任何内容。

访问函数:

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

该属性保存场景的前景笔刷。

此属性设置此视图中场景的前景画笔。它用于覆盖场景自身的前景,并定义drawForeground() 的行为。要为该视图提供自定义前景绘图,可以重新实现drawForeground() 来代替。

默认情况下,该属性包含一个带有Qt::NoBrush 图案的画笔。

访问功能:

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

另请参见 QGraphicsScene::foregroundBrushbackgroundBrush

interactive : bool

此属性表示视图是否允许场景交互。

如果启用,该视图将被设置为允许场景交互。否则,该视图将不允许交互,任何鼠标或按键事件都将被忽略(即视图将作为只读视图)。

默认情况下,此属性为true

访问功能:

bool isInteractive() const
void setInteractive(bool allowed)

optimizationFlags : OptimizationFlags

标记,可用于调整QGraphicsView 的性能。

QGraphicsView 在常见的图形场景中,渲染引擎使用剪裁、额外的边界矩形调整和某些其他辅助工具来提高渲染质量和性能。不过,根据目标平台、场景和使用中的视口的不同,其中一些操作可能会降低性能。

不同的优化标记会产生不同的效果,详情请查看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 当视口部件的大小发生变化时,"视口"(viewport)会使用该属性来决定场景在视口中的位置。默认行为( )是在调整大小时保持场景位置不变;调整大小时,视图的左上角将被锚定。NoAnchor

请注意,只有部分场景可见时(即有滚动条时),此属性的效果才会明显。否则,如果整个场景都在视图中,QGraphicsScene 将使用视图对齐方式在视图中定位场景。

访问功能:

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

另请参阅 对齐transformationAnchor

rubberBandSelectionMode : Qt::ItemSelectionMode

该属性用于保存使用橡皮筋矩形选区选择项目的行为。

该属性定义了使用RubberBandDrag 拖动模式时选择项的方式。

默认值为Qt::IntersectsItemShape ;形状与橡皮筋相交或被橡皮筋包含的所有项目都会被选中。

访问功能:

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

另请参阅 dragModeitems() 和rubberBandRect()。

sceneRect : QRectF

此属性表示此视图可视化的场景区域。

场景矩形定义了场景的范围,在视图中,这意味着可以使用滚动条浏览的场景区域。

如果未设置或设置的QRectF 为空,则此属性的值与QGraphicsScene::sceneRect 相同,并随QGraphicsScene::sceneRect 的变化而变化。否则,视图的场景矩形不受场景影响。

请注意,虽然场景支持几乎无限的大小,但滚动条的范围永远不会超过整数(INT_MIN,INT_MAX)的范围。当场景大于滚动条的值时,您可以选择使用translate() 来导航场景。

默认情况下,该属性在原点处包含一个宽度和高度均为零的矩形。

访问功能:

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 将使用视图对齐方式在视图中定位场景。

访问功能:

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

另请参阅 对齐resizeAnchor

viewportUpdateMode : ViewportUpdateMode

视口应如何更新其内容。

QGraphicsView 使用此属性可决定如何更新已重新曝光或更改的场景区域。通常情况下,您不需要修改此属性,但在某些情况下,修改此属性可以提高渲染性能。具体详情请参见 文档。ViewportUpdateMode

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

访问功能:

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

另请参阅 ViewportUpdateModecacheMode

成员函数文档

QGraphicsView::QGraphicsView(QWidget *parent = nullptr)

构造一个 QGraphicsView。parent 传递给QWidget 的构造函数。

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

构造一个 QGraphicsView 并将可视化场景设置为sceneparent 传递给QWidget 的构造函数。

[virtual noexcept] QGraphicsView::~QGraphicsView()

销毁QGraphicsView 对象。

void QGraphicsView::centerOn(const QPointF &pos)

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

由于pos 是浮点坐标,而滚动条是在整数坐标上操作的,因此居中只是一个近似值。

注意: 如果项目接近或超出边界,它将在视图中可见,但不会居中。

另请参阅 ensureVisible()。

void QGraphicsView::centerOn(const QGraphicsItem *item)

这是一个重载函数。

滚动视口内容,确保item 位于视图中心。

另请参阅 ensureVisible().

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

这是一个重载函数。

提供该函数是为了方便使用。它等同于调用 centerOn(QPointF(x,y))。

[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 绘制场景的背景。重新实现此函数可为该视图提供自定义背景。

如果只想为背景定义颜色、纹理或渐变,可以调用setBackgroundBrush() 代替。

所有绘制都是在场景坐标中完成的。rect 是暴露的矩形。

默认实现是使用视图的backgroundBrush 填充rect 。如果没有定义此类画笔(默认情况),则会调用场景的 drawBackground() 函数。

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

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

在绘制完背景和所有项目后,使用painter 绘制场景的前景。重新实现此函数可为该视图提供自定义前景。

如果只想为前景定义颜色、纹理或渐变,可以调用setForegroundBrush() 代替。

所有绘制都是在场景坐标中完成的。rect 是暴露的矩形。

默认实现是使用视图的foregroundBrush 填充rect 。如果没有定义此类画笔(默认情况),则会调用场景的 drawForeground() 函数。

另请参阅 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 可见,边距由xmarginymargin 以像素为单位指定。如果无法达到指定的矩形,则将内容滚动到最近的有效位置。两个边距的默认值都是 50 像素。

另请参阅 centerOn() 。

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

这是一个重载函数。

滚动视口的内容,使item 项目的中心可见,边距由xmarginymargin 以像素为单位指定。如果无法到达指定点,则将内容滚动到最近的有效位置。两个边距的默认值都是 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)。

[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 必须位于场景矩形内;否则,fitInView() 无法保证整个矩形可见。

该函数会保持视图的旋转、平移或剪切。视图将根据aspectRatioMode 进行缩放。rect ,如果不能紧密贴合,视图将居中。

在重新实现resizeEvent() 时,通常会调用 fitInView(),以确保在调整视图大小时,整个场景或部分场景会自动缩放以适应新的视口大小。不过需要注意的是,如果新的变换会切换滚动条的自动状态,那么在resizeEvent() 中调用 fitInView() 可能会导致不必要的大小调整递归。您可以将滚动条策略切换为始终打开或始终关闭,以防止出现这种情况(请参阅horizontalScrollBarPolicy() 和verticalScrollBarPolicy() )。

如果rect 为空,或者视口太小,此函数将不起任何作用。

另请参见 setTransform()、ensureVisible() 和centerOn()。

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

这是一个重载函数。

确保item 与视图紧密贴合,并根据aspectRatioMode 缩放视图。

另请参阅 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() 。

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

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

使rect 内的layers 失效并安排重绘。rect 处于场景坐标中。layersrect 内的任何缓存内容都将无条件失效并重新绘制。

您可以调用此函数来通知QGraphicsView 场景背景或前景的变化。当QGraphicsView 启用背景缓存时,它通常用于基于磁贴背景的场景,以通知更改。

请注意,QGraphicsView 目前仅支持背景缓存(请参阅QGraphicsView::CacheBackground )。如果传递了除QGraphicsScene::BackgroundLayer 以外的任何图层,则此函数等同于调用update() 。

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

bool QGraphicsView::isTransformed() const

如果视图已变换(即已分配非同一变换,或滚动条已调整),则返回true

另请参阅 setTransform()、horizontalScrollBar() 和verticalScrollBar()。

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

返回位于pos 位置的项目,该位置以视口坐标表示。如果该位置有多个项目,该函数将返回最上面的项目。

示例

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

另请参见 items() 和Sorting

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

这是一个重载函数。

提供该函数是为了方便使用。它等同于调用 itemAt(QPoint(x,y))。

QList<QGraphicsItem *> QGraphicsView::items() const

返回关联场景中所有项目的列表,按降序堆叠(即返回列表中的第一个项目是最上面的项目)。

另请参阅 QGraphicsScene::items() 和Sorting

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

返回视图中pos 位置的所有项目列表。项目按降序堆叠排列(即列表中的第一个项目是最上面的项目,最后一个项目是最下面的项目)。pos 是视口坐标。

该函数最常在QGraphicsView 子类中的鼠标事件处理程序中调用。pos 与 QMouseEvent::pos() 一样,是以未转换的视口坐标显示的。

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

另请参见 QGraphicsScene::items() 和Sorting

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

提供该函数是为了方便使用。它等同于调用 items(QPoint(x,y)) 。

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

此方便函数等同于调用 items(QRectF(x,y,w,h),mode)。

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

这是一个重载函数。

根据mode ,返回被path 包含或与 相交的所有项目的列表。path 是视口坐标。

mode 的默认值是Qt::IntersectsItemShape ;所有与path 相交或被 包含的项目都会被返回。

另请参阅 itemAt()、items()、mapToScene() 和Sorting

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

这是一个重载函数。

根据mode ,返回被polygon 包含或与 相交的所有项目的列表。polygon 是视口坐标。

mode 的默认值是Qt::IntersectsItemShape ;所有与polygon 相交或被 包含的项目都会被返回。

项目按降序堆叠排序(即返回列表中的第一个项目为最上一个项目)。

另请参阅 itemAt()、items()、mapToScene() 和Sorting

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

这是一个重载函数。

根据mode ,返回被rect 包含或与 相交的所有项目的列表。rect 是视口坐标。

mode 的默认值是Qt::IntersectsItemShape ;所有与rect 相交或被 包含的项目都会被返回。

项目按降序堆叠排序(即返回列表中的第一个项目为最上一个项目)。

另请参见 itemAt()、items()、mapToScene() 和Sorting

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

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

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

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

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

返回场景坐标绘制路径path 到视口坐标绘制路径。

另请参阅 mapToScene()。

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

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

另请参阅 mapToScene().

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

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

另请参阅 mapToScene()。

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

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

另请参阅 mapToScene()。

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

提供该函数是为了方便使用。它等同于调用 mapFromScene(QPointF(x,y)) 。

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

提供该函数是为了方便使用。它等同于调用 mapFromScene(QRectF(x,y,w,h)) 。

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

返回视口绘制路径path 与场景坐标绘制路径的映射。

另请参阅 mapFromScene()。

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

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

注:映射像素覆盖的整个矩形(point )而不是点本身可能会很有用。为此,可以调用 mapToScene(QRect(point,QSize(2, 2))) 。

另请参见 mapFromScene().

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

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

另请参阅 mapFromScene()。

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

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

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

使用painter 将以视图坐标表示的source 矩形从场景渲染到以绘画设备坐标表示的target 中。该函数可用于将视图内容捕捉到绘画设备上,如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 是一个空矩形,该函数将使用viewport()->rect() 来确定要绘制的内容。如果target 是空矩形,则将使用painter'绘图设备的完整尺寸(例如,对于 QPrinter,页面尺寸)。

源矩形内容将根据aspectRatioMode 进行转换,以适应目标矩形。默认情况下,长宽比将保持不变,而source 将按比例调整以适合target

另请参阅 QGraphicsScene::render() 。

void QGraphicsView::resetCachedContent()

重置缓存内容。调用此函数将清除QGraphicsView 的缓存。如果当前缓存模式为CacheNone ,则此函数不会执行任何操作。

backgroundBrushQGraphicsScene::backgroundBrush 属性发生变化时,将自动调用此函数;只有在重新实现QGraphicsScene::drawBackground() 或QGraphicsView::drawBackground() 以绘制自定义背景,并需要触发完全重绘时,才需要调用此函数。

另请参见 cacheMode()。

void QGraphicsView::resetTransform()

将视图变换重置为同矩阵。

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

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

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

void QGraphicsView::rotate(qreal angle)

顺时针旋转当前视图变换angle 度。

另请参阅 setTransform()、transform()、scale()、shear() 和translate()。

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

当橡皮筋矩形发生变化时会发出该信号。视口矩形由rubberBandRect 指定。拖动起始位置和拖动结束位置通过fromScenePointtoScenePoint 以场景点的形式提供。

当橡皮筋选择结束时,该信号将以空值发出。

另请参阅 rubberBandRect() 。

QRect QGraphicsView::rubberBandRect() const

如果用户当前正在使用橡皮筋进行项目选择,此函数将返回当前橡皮筋区域(视口坐标)。如果用户没有使用橡皮筋,该函数将返回(空)QRectF()。

请注意,该QRect 的部分区域可能位于可视视口之外。例如,它可以包含负值。

另请参见 rubberBandSelectionModerubberBandChanged()。

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

通过 (sx,sy) 来缩放当前视图变换。

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

QGraphicsScene *QGraphicsView::scene() const

返回视图中当前可视化场景的指针。如果当前没有可视化场景,则返回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)

如果enabled 为 true,则启用flag ;否则禁用flag

另请参阅 optimizationFlags

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

如果enabled 为 true,则启用呈现提示hint ;否则禁用。

另请参阅 renderHints

void QGraphicsView::setScene(QGraphicsScene *scene)

将当前场景设置为scene 。如果scene 已在浏览,则此函数不会执行任何操作。

在视图上设置场景后,QGraphicsScene::changed() 信号会自动连接到该视图的updateScene() 插槽,视图的滚动条也会调整为适合场景的大小。

视图不拥有scene 的所有权。

另请参阅 scene()。

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

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

如果combine 为真,则matrix 与当前矩阵合并;否则,matrix 取代当前矩阵。默认情况下,combine 为假。

转换矩阵将场景转换为视图坐标。使用由标识矩阵提供的默认变换,视图中的一个像素代表场景中的一个单位(例如,在视图中使用 10x10 像素绘制一个 10x10 的矩形项目)。如果应用 2x2 缩放矩阵,则场景将以 1:2 的比例绘制(例如,一个 10x10 的矩形项目在视图中将以 20x20 的像素绘制)。

举例说明:

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

为了简化与使用转换视图的项目的交互,QGraphicsView 提供了 mapTo... 和 mapFrom... 函数,可以在场景和视图坐标之间进行转换。例如,您可以调用mapToScene() 将视图坐标映射到浮点场景坐标,或调用mapFromScene() 将浮点场景坐标映射到视图坐标。

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

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

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

QAbstractScrollArea 在调用setViewport() 后调用此槽。在QGraphicsView 的子类中重新实现此函数,以便在使用新视口widget 之前对其进行初始化。

另请参阅 setViewport()。

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

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

安排更新场景矩形rects

另请参阅 QGraphicsScene::changed() 。

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

通知QGraphicsView 场景的场景矩形已更改。rect 是新的场景矩形。如果视图已经明确设置了场景矩形,则此函数不会执行任何操作。

另请参阅 sceneRectQGraphicsScene::sceneRectChanged()。

[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)。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.