Public Types
flags |
|
enum |
CacheModeFlag { CacheNone, CacheBackground } |
enum |
DragMode { NoDrag, ScrollHandDrag, RubberBandDrag } |
enum |
OptimizationFlag { DontClipPainter, DontSavePainterState, DontAdjustForAntialiasing, IndirectPainting } |
flags |
|
enum |
ViewportAnchor { NoAnchor, AnchorViewCenter, AnchorUnderMouse } |
enum |
ViewportUpdateMode { FullViewportUpdate, MinimalViewportUpdate, SmartViewportUpdate, BoundingRectViewportUpdate, NoViewportUpdate } |
Properties
|
|
- 3 properties inherited from QAbstractScrollArea
- 6 properties inherited from QFrame
- 59 properties inherited from QWidget
- 1 property inherited from QObject
Public Functions
QGraphicsView(QWidget *parent = nullptr) |
|
QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr) |
|
virtual |
|
Qt::Alignment |
alignment() const |
QBrush |
backgroundBrush() const |
QGraphicsView::CacheMode |
cacheMode() const |
void |
centerOn(const QPointF &pos) |
void |
centerOn(qreal x, qreal y) |
void |
centerOn(const QGraphicsItem *item) |
QGraphicsView::DragMode |
dragMode() const |
void |
ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50) |
void |
ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50) |
void |
ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50) |
void |
fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio) |
void |
fitInView(qreal x, qreal y, qreal w, qreal h, Qt::AspectRatioMode aspectRatioMode = ...) |
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 |
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 |
|
void |
|
void |
|
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 |
setMatrix(const QMatrix &matrix, bool combine = false) |
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 |
Reimplemented Public Functions
virtual QVariant |
inputMethodQuery(Qt::InputMethodQuery query) const override |
virtual QSize |
sizeHint() const override |
- 20 public functions inherited from QAbstractScrollArea
- 14 public functions inherited from QFrame
- 214 public functions inherited from QWidget
- 31 public functions inherited from QObject
- 14 public functions inherited from QPaintDevice
Public Slots
void |
invalidateScene(const QRectF &rect, QGraphicsScene::SceneLayers layers) |
void |
updateScene(const QList<QRectF> &rects) |
void |
updateSceneRect(const QRectF &rect) |
Signals
void |
rubberBandChanged(QRect rubberBandRect, QPointF fromScenePoint, QPointF toScenePoint) |
Static Public Members
const QMetaObject |
Protected Functions
virtual void |
drawBackground(QPainter *painter, const QRectF &rect) |
virtual void |
drawForeground(QPainter *painter, const QRectF &rect) |
Reimplemented Protected Functions
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 |
- 20 protected functions inherited from QAbstractScrollArea
- 4 protected functions inherited from QFrame
- 35 protected functions inherited from QWidget
- 9 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice
Protected Slots
virtual void |
setupViewport(QWidget *widget) override |
- 1 protected slot inherited from QWidget
详细说明(Detailed Description)
QGraphicsView图形视图类提供了一个QGraphicsScene图形场景窗口显示内容。
QGraphicsView可以将QGraphicsScene中的内容在一个可滚动的视口中进行可视化。如何创建一个包含几何图形的场景可以参考QGraphicsScene。 QGraphicsView是Qt图形视图框架Graphics View Framework中的一个部件。
可视化一个场景首先需要构建一个QGraphicsView对象,将要进行可视化的场景QGraphicsScene的地址传递给QGraphicsView的构造函数,或者稍后调用
void QGraphicsView::setScene(QGraphicsScene *scene)
进行场景设置,在调用
[slot] void QWidget::show()
后,视图将默认滚动到场景的中心,并显示任何可视的项目,例如:
QGraphicsScene scene; scene.addText("Hello, world!"); QGraphicsView view(&scene); view.show();
可以通过使用滚动条或调用
void QGraphicsView::centerOn(const QPointF &pos)
显式的滚动到场景中的任何位置。将一个坐标位置传递给CenterOn(),QGraphicsView会滚动视口使该点在视图窗口上居中,CenterOn()的重载
void QGraphicsView::centerOn(const QGraphicsItem *item)
可以使某一个QGraphicsItem滚动并显示在视图的中心。如果只想确保某个区域在视图中是可见的(不一定需要在视图中心)时可以调用
void QGraphicsView::ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50)
QGraphicsView可以用来可视化整个场景scene或者只显示其中的一部分。默认情况下当视图首次显示时调用
QRectF QGraphicsScene::itemsBoundingRect() const
//Calculates and returns the bounding rect of all items on the scene.
//This function works by iterating over all items, and because of this, it can be slow for large scenes.
自动检测可视化区域,若要自己设置可视化区域可以调用
QRectF sceneRect() const void setSceneRect(const QRectF &rect) void setSceneRect(qreal x, qreal y, qreal w, qreal h)
这会适当的调整滚动条的范围。需要注意的是,场景几乎支持无限大小,但滚动条的范围不能超过整数的范围(INT_MIN, INT_MAX)。
QGraphicsView通过调用render()对场景进行可视化。
void QGraphicsView::render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio)
默认情况下,这些项目使用常规的QPainter和使用默认渲染方式呈现在视图窗口上,若要更改QGraphicsView在绘制项目时传递给QPainter的渲染方式,可以调用
QPainter::RenderHints renderHints() const void setRenderHints(QPainter::RenderHints hints)
默认情况下,QGraphicsView为视口小部件viewport提供了一个常规的QWidget。可以通过调用viewport()
QWidget *QAbstractScrollArea::viewport() const
访问这个窗口部件,或者调用
void QAbstractScrollArea::setViewport(QWidget *widget)
设置新的视图窗口若要使用OpenGL进行渲染,只需调用setViewport(new QGLWidget),QGraphicsView是拥有视图窗口的所有权。
QGraphicsView支持使用QTransform进行仿射变换。也可以调用
void QGraphicsView::setTransform(const QTransform &matrix, bool combine = false)
通过矩阵进行转换,或者调用以下几个函数
void QGraphicsView::rotate(qreal angle)
void QGraphicsView::scale(qreal sx, qreal sy)
void QGraphicsView::translate(qreal dx, qreal dy)
void QGraphicsView::shear(qreal sh, qreal sv)
最常见的两个转换是缩放和旋转。QGraphicsView在转换过程中视图的中心是保持固定不变的。由于场景对齐((setAligment()),转换视图不会产生视觉上的影响。
在QGraphicsView中,可以使用鼠标和键盘与场景中的项目交互,QGraphicsView将鼠标和键盘事件转换为scene事件(继承自QGraphicsSceneEvent)并将它们转换到可视化场景中,最后由单独的项目来处理这些事件并对它们作出响应。比如,单击一个可选项,该项目通常会通知场景它已被选中,并且会重绘自己以高亮显示选择矩形,如果你单击并使用鼠标拖动一个可移动的项目时,该项目会处理鼠标移动事件并移动它自己。默认情况下项目的交互是启用的,可以调用
bool isInteractive() const void setInteractive(bool allowed)
进行切换。
我们可以通过创建QGraphicsView的子类并重新实现鼠标和按键处理事件自定义场景交互。QGraphicsView提供了映射函数
QPointF QGraphicsView::mapToScene(const QPoint &point) const //Returns the viewport coordinate point mapped to scene coordinates.
QPoint QGraphicsView::mapFromScene(const QPointF &point) const //Returns the scene coordinate point to viewport coordinates.
以及项目访问函数。
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).
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.
这些项目可以让我们在视图坐标和场景坐标之间映射点、矩形、多边形和路径,并使用视图坐标在场景中查找项目。