QGraphicsView Class
QGraphicsView 是一个显示 QGraphicsScene内容的widget. 更多...
头文件: | #include <QGraphicsView> |
qmake: | QT += widgets |
开始支持版本: | Qt 4.2 |
基类: | QAbstractScrollArea |
公有类型
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 } |
属性
|
|
- 3 个继承自 QAbstractScrollArea属性
- 6 个继承自 QFrame属性
- 59 个继承自 QWidget属性
- 1 个继承自 QObject属性
公有函数
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 |
- 20 个继承自 QAbstractScrollArea公有函数
- 14 个继承自 QFrame公有函数
- 214 个继承自 QWidget公有函数
- 32 个继承自 QObject公有函数
- 14 个继承自 QPaintDevice公有函数
公有槽函数
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) |
- 20 个继承自 QAbstractScrollArea受保护的函数
- 4 个继承自 QFrame受保护的函数
- 35 个继承自 QWidget受保护的函数
- 9 个继承自 QObject受保护的函数
- 1 个继承自 QPaintDevice受保护的函数
受保护的槽函数
virtual void | setupViewport(QWidget *widget) |
- 1 个继承自 QWidget受保护的槽函数
其他继承的成员
- 1 个继承自 QObject公有变量
- 5 个继承自 QWidget静态公有成员
- 10 个继承自 QObject静态公有成员
- 2 个继承自 QObject受保护的变量
- 1 个继承自 QPaintDevice受保护的类型
详细描述
QGraphicsView 是一个可视化 QGraphicsScene内容的widget.
QGraphicsView 在一个滚动的视口中可视化 QGraphicsScene 的内容. 如何创建带有几何图形的场景参考 QGraphicsScene的文档. QGraphicsView 是 Graphics View Framework的一部分.
想要可视化一个场景, 首先, 你必须构建一个 QGraphicsView 对象, 并将QGraphicsScene对象地址传递给 QGraphicsView的构造函数. 或者, 在构造对象之后调用 setScene()函数设置QGraphicsScene. 默认情况下, 当你调用 show()函数后, 视图自动滚动到场景中心, 并显示场景中所有元素. 例如:
QGraphicsScene scene; scene.addText("Hello, world!"); QGraphicsView view(&scene); view.show();
你可以使用滚动条或者调用 centerOn()函数定位到场景的任意位置. 当调用 centerOn()函数设置一个位置时, QGraphicsView 将滚动视口以确保该位置是视口的中心. 为 QGraphicsItem对象实现一个重载, QGraphicsView 将使用QGraphicsItem的中心作为视口中心. 如果你想确保某个区域一直可见, (但是不需要居中,) 你可以调用 ensureVisible() 函数.
QGraphicsView 不仅能可视化整个场景, 也能可视化部分场景. 当QGraphicsView首次可视化整个场景时, 它通过调用 QGraphicsScene::itemsBoundingRect()函数自动检测可视化区域. 如果你想设置可视化区域为矩形, 你可以调用 setSceneRect()函数, QGraphicsView会自动调整滚动条的范围. 注意: 虽然QGraphicsView不限制场景的大小, 但是滚动条的范围受到整数的范围 (INT_MIN, INT_MAX)的限制.
QGraphicsView 通过调用 render()函数可视化场景. 默认情况下, 场景中的元素使用 QPainter和默认的渲染配置绘制到视口上. 如果想要修改默认的渲染配置, 你可以调用setRenderHints()函数. 这样, 当绘制场景元素时, QGraphicsView 会将设置的渲染配置传递给 QPainter .
默认情况下, QGraphicsView 提供一个常规的 QWidget 作为视口widget. 你可以调用 viewport()函数访问这个widget, 或者你可以调用 setViewport()替换这个视口widget. 如果你想使用OpenGL渲染, 你可以调用 setViewport(new QGLWidget)函数. QGraphicsView 持有视口widget的所有权(不能外部销毁widget).
QGraphicsView 支持仿射变换, 内部使用 QTransform. 你不仅能通过 setTransform()函数设置一个矩阵, 你也能调用这些简单函数 rotate(), scale(), translate() or shear(). 最常见的两种变换是缩放和旋转. 在转换时, QGraphicsView 保持试图中心不变. 由于场景对齐(setAligment()), 平移视图不会产生视觉变化.
你可以通过键盘和鼠标与场景中的元素交互. QGraphicsView 将鼠标和键盘事件转换成 场景 事件, (场景事件继承自 QGraphicsSceneEvent,), 并将其分发到可视化场景. 最后, 单个场景元素处理事件并作出反应. 例如, 如果你点选一个可选元素, 这个元素会通知场景, 并进行重绘, 显示一个矩形选择框. 与之类似, 如果你使用鼠标点击并拖拽一个可移动的元素, 这个元素会处理鼠标移动事件, 并移动自身. 默认情况下, 元素交互是开启的, 你可以调用 setInteractive()函数关闭元素交互功能.
通过子类化QGraphicsView, 你能自定义场景交互方式, 也能重新实现鼠标和键盘事件. 为了简化你编写与视图中的元素交互代码, QGraphicsView 提供了坐标映射函数mapToScene() 和 mapFromScene(), 元素访问函数 items() 和 itemAt(). 这些函数允许你在视图坐标系和场景坐标系映射点, 矩形, 多边形及路径的坐标, 并且使用视图坐标查询场景中的元素.
参见 QGraphicsScene, QGraphicsItem, and QGraphicsSceneEvent.
成员类型
enum QGraphicsView::CacheModeFlag
flags QGraphicsView::CacheMode
This enum describes the flags that you can set for a QGraphicsView's cache mode.
Constant | Value | Description |
---|---|---|
QGraphicsView::CacheNone | 0x0 | All painting is done directly onto the viewport. |
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. |
The CacheMode type is a typedef for QFlags<CacheModeFlag>. It stores an OR combination of CacheModeFlag values.
参见 cacheMode.
enum QGraphicsView::DragMode
This enum describes the default action for the view when pressing and dragging the mouse over the viewport.
Constant | Value | Description |
---|---|---|
QGraphicsView::NoDrag | 0 | Nothing happens; the mouse event is ignored. |
QGraphicsView::ScrollHandDrag | 1 | The cursor changes into a pointing hand, and dragging the mouse around will scroll the scrolbars. This mode works both in interactive and non-interactive mode. |
QGraphicsView::RubberBandDrag | 2 | A rubber band will appear. Dragging the mouse will set the rubber band geometry, and all items covered by the rubber band are selected. This mode is disabled for non-interactive views. |
参见 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.
Constant | Value | Description |
---|---|---|
QGraphicsView::DontClipPainter | 0x1 | This value is obsolete and has no effect. |
QGraphicsView::DontSavePainterState | 0x2 | When rendering, 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() or 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. |
This enum was introduced or modified in Qt 4.3.
The OptimizationFlags type is a typedef for QFlags<OptimizationFlag>. It stores an OR combination of OptimizationFlag values.
enum QGraphicsView::ViewportAnchor
This enums describe the possible anchors that QGraphicsView can use when the user resizes the view or when the view is transformed.
Constant | Value | Description |
---|---|---|
QGraphicsView::NoAnchor | 0 | No anchor, i.e. the view leaves the scene's position unchanged. |
QGraphicsView::AnchorViewCenter | 1 | The scene point at the center of the view is used as the anchor. |
QGraphicsView::AnchorUnderMouse | 2 | The point under the mouse is used as the anchor. |
参见 resizeAnchor and transformationAnchor.
enum QGraphicsView::ViewportUpdateMode
This enum describes how QGraphicsView updates its viewport when the scene contents change or are exposed.
Constant | Value | Description |
---|---|---|
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. |
This enum was introduced or modified in Qt 4.3.
属性
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
This property holds the background brush of the scene.
This property sets the background brush for the scene in this view. It is used to override the scene's own background, and defines the behavior of drawBackground(). To provide custom background drawing for this view, you can reimplement drawBackground() instead.
By default, this property contains a brush with the Qt::NoBrush pattern.
访问函数:
QBrush | backgroundBrush() const |
void | setBackgroundBrush(const QBrush &brush) |
参见 QGraphicsScene::backgroundBrush and foregroundBrush.
cacheMode : CacheMode
This property holds which parts of the view are cached
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() and QPixmapCache.
dragMode : DragMode
This property holds the behavior for dragging the mouse over the scene while the left mouse button is pressed.
This property defines what should happen when the user clicks on the scene background and drags the mouse (e.g., scrolling the viewport contents using a pointing hand cursor, or selecting multiple items with a rubber band). The default value, NoDrag, does nothing.
This behavior only affects mouse clicks that are not handled by any item. You can define a custom behavior by creating a subclass of QGraphicsView and reimplementing mouseMoveEvent().
访问函数:
DragMode | dragMode() const |
void | setDragMode(DragMode mode) |
foregroundBrush : QBrush
This property holds the foreground brush of the scene.
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() instead.
By default, this property contains a brush with the Qt::NoBrush pattern.
访问函数:
QBrush | foregroundBrush() const |
void | setForegroundBrush(const QBrush &brush) |
参见 QGraphicsScene::foregroundBrush and backgroundBrush.
interactive : bool
This property holds whether the view allows scene interaction.
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).
By default, this property is true
.
访问函数:
bool | isInteractive() const |
void | setInteractive(bool allowed) |
optimizationFlags : OptimizationFlags
flags that can be used to tune QGraphicsView's performance.
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 documentation for details.
By default, no optimization flags are enabled.
This property was introduced in Qt 4.3.
访问函数:
OptimizationFlags | optimizationFlags() const |
void | setOptimizationFlags(OptimizationFlags flags) |
参见 setOptimizationFlag().
renderHints : QPainter::RenderHints
This property holds the default render hints for the view
These hints are used to initialize QPainter before each visible item is drawn. QPainter uses render hints to toggle rendering features such as antialiasing and smooth pixmap transformation.
QPainter::TextAntialiasing is enabled by default.
Example:
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
how the view should position the scene when the view is resized.
QGraphicsView uses this property to decide how to position the scene in the viewport when the viewport widget's size changes. The default behavior, NoAnchor, leaves the scene's position unchanged during a resize; the top-left corner of the view will appear to be anchored while resizing.
Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.
访问函数:
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.
This property defines how items are selected when using the RubberBandDrag drag mode.
The default value is Qt::IntersectsItemShape; all items whose shape intersects with or is contained by the rubber band are selected.
This property was introduced in Qt 4.3.
访问函数:
Qt::ItemSelectionMode | rubberBandSelectionMode() const |
void | setRubberBandSelectionMode(Qt::ItemSelectionMode mode) |
参见 dragMode, items(), and rubberBandRect().
sceneRect : QRectF
This property holds the area of the scene visualized by this view.
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.
If unset, or if a 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) |
transformationAnchor : ViewportAnchor
how the view should position the scene during transformations.
QGraphicsView uses this property to decide how to position the scene in the viewport when the transformation matrix changes, and the coordinate system of the view is transformed. The default behavior, AnchorViewCenter, ensures that the scene point at the center of the view remains unchanged during transformations (e.g., when rotating, the scene will appear to rotate around the center of the view).
Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.
访问函数:
ViewportAnchor | transformationAnchor() const |
void | setTransformationAnchor(ViewportAnchor anchor) |
参见 alignment and resizeAnchor.
viewportUpdateMode : ViewportUpdateMode
how the viewport should update its contents.
QGraphicsView uses this property to decide how to update areas of the scene that have been reexposed or changed. Usually you do not need to modify this property, but there are some cases where doing so can improve rendering performance. See the ViewportUpdateMode documentation for specific details.
The default value is MinimalViewportUpdate, where QGraphicsView will update as small an area of the viewport as possible when the contents change.
This property was introduced in Qt 4.3.
访问函数:
ViewportUpdateMode | viewportUpdateMode() const |
void | setViewportUpdateMode(ViewportUpdateMode mode) |
参见 ViewportUpdateMode and cacheMode.
成员函数
QGraphicsView::QGraphicsView(QWidget *parent = Q_NULLPTR)
Constructs a QGraphicsView. parent is passed to QWidget's constructor.
QGraphicsView::QGraphicsView(QGraphicsScene *scene, QWidget *parent = Q_NULLPTR)
Constructs a QGraphicsView and sets the visualized scene to scene. parent is passed to QWidget's constructor.
QGraphicsView::~QGraphicsView()
Destructs the QGraphicsView object.
void QGraphicsView::centerOn(const QPointF &pos)
Scrolls the contents of the viewport to ensure that the scene coordinate pos, is centered in the view.
Because pos is a floating point coordinate, and the scroll bars operate on integer coordinates, the centering is only an approximation.
Note: If the item is close to or outside the border, it will be visible in the view, but not centered.
参见 ensureVisible().
void QGraphicsView::centerOn(qreal x, qreal y)
This is an overloaded function.
This function is provided for convenience. It's equivalent to calling centerOn(QPointF(x, y)).
void QGraphicsView::centerOn(const QGraphicsItem *item)
This is an overloaded function.
Scrolls the contents of the viewport to ensure that item is centered in the view.
参见 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)
Draws the background of the scene using 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() instead.
All painting is done in scene coordinates. rect is the exposed rectangle.
The default implementation fills rect using the view's backgroundBrush. If no such brush is defined (the default), the scene's drawBackground() function is called instead.
参见 drawForeground() and 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() instead.
All painting is done in scene coordinates. rect is the exposed rectangle.
The default implementation fills rect using the view's foregroundBrush. If no such brush is defined (the default), the scene's drawForeground() function is called instead.
参见 drawBackground() and QGraphicsScene::drawForeground().
[virtual protected]
void QGraphicsView::dropEvent(QDropEvent *event)
重新实现 QWidget::dropEvent().
void QGraphicsView::ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50)
Scrolls the contents of the viewport so that the scene rectangle rect is visible, with margins specified in pixels by xmargin and ymargin. If the specified rect cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.
参见 centerOn().
void QGraphicsView::ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50)
This is an overloaded function.
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)
This is an overloaded function.
Scrolls the contents of the viewport so that the center of item item is visible, with margins specified in pixels by xmargin and ymargin. If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.
参见 centerOn().
[virtual protected]
bool QGraphicsView::event(QEvent *event)
重新实现 QObject::event().
void QGraphicsView::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport. 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() and verticalScrollBarPolicy()).
If rect is empty, or if the viewport is too small, this function will do nothing.
参见 setTransform(), ensureVisible(), and centerOn().
void QGraphicsView::fitInView(qreal x, qreal y, qreal w, qreal h, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
This is an overloaded function.
This convenience function is equivalent to calling fitInView(QRectF(x, y, w, h), aspectRatioMode).
参见 ensureVisible() and centerOn().
void QGraphicsView::fitInView(const QGraphicsItem *item, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)
This is an overloaded function.
Ensures that item fits tightly inside the view, scaling the view according to aspectRatioMode.
参见 ensureVisible() and 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 is unconditionally invalidated and redrawn.
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.
Note that QGraphicsView currently supports background caching only (see QGraphicsView::CacheBackground). This function is equivalent to calling update() if any layer but QGraphicsScene::BackgroundLayer is passed.
参见 QGraphicsScene::invalidate() and update().
bool QGraphicsView::isTransformed() const
Returns true
if the view is transformed (i.e., a non-identity transform has been assigned, or the scrollbars are adjusted).
This function was introduced in Qt 4.6.
参见 setTransform(), horizontalScrollBar(), and 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.
Example:
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 is an overloaded function.
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() and Sorting.
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() and Sorting.
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
This is an overloaded function.
Returns a list of all the items that, depending on mode, are either contained by or intersect with rect. rect is in viewport coordinates.
The default value for mode is Qt::IntersectsItemShape; all items whose exact shape intersects with or is contained by rect are returned.
The items are sorted in descending stacking order (i.e., the first item in the returned list is the uppermost item).
参见 itemAt(), items(), mapToScene(), and Sorting.
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).
This function was introduced in Qt 4.3.
QList<QGraphicsItem *> QGraphicsView::items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
This is an overloaded function.
Returns a list of all the items that, depending on mode, are either contained by or intersect with polygon. polygon is in viewport coordinates.
The default value for mode is Qt::IntersectsItemShape; all items whose exact shape intersects with or is contained by polygon are returned.
The items are sorted by descending stacking order (i.e., the first item in the returned list is the uppermost item).
参见 itemAt(), items(), mapToScene(), and Sorting.
QList<QGraphicsItem *> QGraphicsView::items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const
This is an overloaded function.
Returns a list of all the items that, depending on mode, are either contained by or intersect with path. path is in viewport coordinates.
The default value for mode is Qt::IntersectsItemShape; all items whose exact shape intersects with or is contained by path are returned.
参见 itemAt(), items(), mapToScene(), and Sorting.
[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
Returns the scene coordinate point to viewport coordinates.
参见 mapToScene().
QPolygon QGraphicsView::mapFromScene(const QRectF &rect) const
Returns the scene rectangle rect to a viewport coordinate polygon.
参见 mapToScene().
QPolygon QGraphicsView::mapFromScene(const QPolygonF &polygon) const
Returns the scene coordinate polygon polygon to a viewport coordinate 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
Returns the viewport coordinate point mapped to scene coordinates.
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
Returns the viewport rectangle rect mapped to a scene coordinate polygon.
参见 mapFromScene().
QPolygonF QGraphicsView::mapToScene(const QPolygon &polygon) const
Returns the viewport polygon polygon mapped to a scene coordinate polygon.
参见 mapFromScene().
QPainterPath QGraphicsView::mapToScene(const QPainterPath &path) const
Returns the viewport painter path path mapped to a scene coordinate painter 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
Returns the current transformation matrix for the view. If no current transformation is set, the identity matrix is returned.
参见 setMatrix(), transform(), rotate(), scale(), shear(), and 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)
Renders the source rect, which is in view coordinates, from the scene into target, which is in paint device coordinates, using painter. This function is useful for capturing the contents of the view onto a paint device, such as a QImage (e.g., to take a screenshot), or for printing to QPrinter. For example:
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));
If source is a null rect, this function will use viewport()->rect() to determine what to draw. If target is a null rect, the full dimensions of painter's paint device (e.g., for a QPrinter, the page size) will be used.
The source rect contents will be transformed according to aspectRatioMode to fit into the target rect. By default, the aspect ratio is kept, and source is scaled to fit in 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 does nothing.
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() or 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() and 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(), and 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.
When rubberband selection ends this signal will be emitted with null vales.
This function was introduced in 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.
This function was introduced in Qt 5.1.
参见 rubberBandSelectionMode and rubberBandChanged().
void QGraphicsView::scale(qreal sx, qreal sy)
Scales the current view transformation by (sx, sy).
参见 setTransform(), transform(), rotate(), shear(), and 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)
Sets the view's current transformation matrix to matrix.
If combine is true, then matrix is combined with the current matrix; otherwise, matrix replaces 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).
Example:
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.
参见 matrix(), setTransform(), rotate(), scale(), shear(), and translate().
void QGraphicsView::setOptimizationFlag(OptimizationFlag flag, bool enabled = true)
Enables flag if enabled is true; otherwise disables flag.
void QGraphicsView::setRenderHint(QPainter::RenderHint hint, bool enabled = true)
If enabled is true, the render hint hint is enabled; otherwise it is disabled.
参见 renderHints.
void QGraphicsView::setScene(QGraphicsScene *scene)
Sets the current scene to scene. If 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.
The view does not take ownership of scene.
参见 scene().
void QGraphicsView::setTransform(const QTransform &matrix, bool combine = false)
Sets the view's current transformation matrix to matrix.
If combine is true, then matrix is combined with the current matrix; otherwise, matrix replaces 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).
Example:
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 coordiate to a floating point scene coordinate, or mapFromScene() to map from floating point scene coordinates to view coordinates.
参见 transform(), rotate(), scale(), shear(), and translate().
[virtual protected slot]
void QGraphicsView::setupViewport(QWidget *widget)
重新实现 QAbstractScrollArea::setupViewport().
This slot is called by QAbstractScrollArea after setViewport() has been called. Reimplement this function in a subclass of QGraphicsView to initialize the new viewport widget before it is used.
参见 setViewport().
void QGraphicsView::shear(qreal sh, qreal sv)
Shears the current view transformation by (sh, sv).
参见 setTransform(), transform(), rotate(), scale(), and translate().
[virtual protected]
void QGraphicsView::showEvent(QShowEvent *event)
重新实现 QWidget::showEvent().
[virtual]
QSize QGraphicsView::sizeHint() const
重新实现 QWidget::sizeHint().
QTransform QGraphicsView::transform() const
Returns the current transformation matrix for the view. If no current transformation is set, the identity matrix is returned.
参见 setTransform(), rotate(), scale(), shear(), and translate().
void QGraphicsView::translate(qreal dx, qreal dy)
Translates the current view transformation by (dx, dy).
参见 setTransform(), transform(), rotate(), and shear().
[slot]
void QGraphicsView::updateScene(const QList<QRectF> &rects)
Schedules an update of the scene rectangles rects.
参见 QGraphicsScene::changed().
[slot]
void QGraphicsView::updateSceneRect(const QRectF &rect)
Notifies QGraphicsView that the scene's scene rect has changed. 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
Returns a matrix that maps scene coordinates to viewport coordinates.
参见 mapToScene() and mapFromScene().
[virtual protected]
void QGraphicsView::wheelEvent(QWheelEvent *event)
重新实现 QWidget::wheelEvent().