Qt之图形视图框架

Graphics View Framework 图形视图框架

Graphics View provides a surface for managing and interacting with a large number of custom-made 2D graphical items, and a view widget for visualizing the items, with support for zooming and rotation.
翻译:
图形视图 提供了一个平台 去管理和交互大量自定义的2D 图形项,和一个 视图窗体 去观察 这些项, 支持缩放和旋转

The framework includes an event propagation architecture that allows precise double-precision interaction capabilities for the items on the scene. Items can handle key events, mouse press, move, release and double click events, and they can also track mouse movement.
翻译:
这个框架包含了一个事件传播结构 允许在scene中的items 准确的double精度 的能力。 items可以操作键盘事件、鼠标事件、移动、释放和双击事件,并且也可以追踪 鼠标的移动。

Graphics View uses a BSP (Binary Space Partitioning) tree to provide very fast item discovery, and as a result of this, it can visualize large scenes in real-time, even with millions of items.
翻译:
Graphics View使用了BSP(二进制 空间 分割法)树 去提供非常快的查找item, 并且因为如此,它可以使大量的scenes实时显示, 甚至有上亿的items。

Graphics View was introduced in Qt 4.2, replacing its predecessor, QCanvas.
翻译:Graphics View在Qt 4.2中被引进, 替换它的前任 QCanvas

The Graphics View Architecture 图形视图体系

Graphics View provides an item-based approach to model-view programming, much like InterView’s convenience classes QTableView, QTreeView and QListView. Several views can observe a single scene, and the scene contains items of varying geometric shapes.
翻译:
Graphics View提供了一个 基于项的 方法去模型/视图编程, 很像InterView(模型/视图框架) 的方便类QTableView, QTreeView 和 QListView。 多个views可以观察一个scene, 这个scene包含各种几何图形的 项。

The Scene 场景

QGraphicsScene provides the Graphics View scene. The scene has the following responsibilities:
Providing a fast interface for managing a large number of items
Propagating events to each item
Managing item state, such as selection and focus handling
Providing untransformed rendering functionality; mainly for printing
翻译:
QGraphicsScene提供了Graphics View的场景。 场景有以下的责任
提供一个快速的接口 去管理大量数目的项
传播事件给每个项
管理项的状态,比如选中和焦点操作
提供无转换的渲染功能, 主要是打印

The scene serves as a container for QGraphicsItem objects. Items are added to the scene by calling QGraphicsScene::addItem(), and then retrieved by calling one of the many item discovery functions. QGraphicsScene::items() and its overloads return all items contained by or intersecting with a point, a rectangle, a polygon or a general vector path. QGraphicsScene::itemAt() returns the topmost item at a particular point. All item discovery functions return the items in descending stacking order (i.e., the first returned item is topmost, and the last item is bottom-most).
翻译
scenc担任一个容器 为QGraphicsItem对象。 items被添加到scene 通过调用 QGraphicsScene::addItem()函数,被检索通过调用 众多item查找函数的其中一个。 QGraphicsScene::items()和它的重载函数返回所有 被点、矩形、多边形或者一个普通的矢量路径 包含或者相交 的项。QGraphicsScene::itemAt()函数返回在一个特定的点上最顶层的项。 所有的 项查找 函数返回这些项按照 递减 的栈顺序(也就是:第一个返回项是最顶层的, 最后一个返回项是最底层的)

QGraphicsScene scene;
QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));

QGraphicsItem *item = scene.itemAt(50, 50);
// item == rect

QGraphicsScene’s event propagation architecture schedules scene events for delivery to items, and also manages propagation between items. If the scene receives a mouse press event at a certain position, the scene passes the event on to whichever item is at that position.
翻译:
QGraphicsScene的事件传播体系安排 传递给项的 场景事件 ,并且也管理项之间的传播。 如果场景收到了一个鼠标按下的事件在某个位置, 场景会传给在这个位置上所有的项。

QGraphicsScene also manages certain item states, such as item selection and focus. You can select items on the scene by calling QGraphicsScene::setSelectionArea(), passing an arbitrary shape. This functionality is also used as a basis for rubberband selection in QGraphicsView. To get the list of all currently selected items, call QGraphicsScene::selectedItems(). Another state handled by QGraphicsScene is whether or not an item has keyboard input focus. You can set focus on an item by calling QGraphicsScene::setFocusItem() or QGraphicsItem::setFocus(), or get the current focus item by calling QGraphicsScene::focusItem().
翻译:
QGraphicsScene也管理某个项的状态, 比如项的选择和焦点。 你可以在这个场景中选择项 通过调用QGraphicsScene::setSelectionArea()函数,传递一个任意的图形。 这个功能也被用作橡皮筋选择的基础在QGraphicsView中。 为了得到当前所有被选择的项,可以调用QGraphicsScene::selectedItems()函数。 另外一个被QGraphicsScene操作的状态是一个项是否有键盘输入的焦点。 你可以设置焦点在一个项上拖过调用QGraphicsScene::setFocusItem()或者QGraphicsItem::setFocus()函数, 获取当前的焦点项通过调用QGraphicsScene::focusItem()函数。

Finally, QGraphicsScene allows you to render parts of the scene into a paint device through the QGraphicsScene::render() function. You can read more about this in the Printing section later in this document.
翻译:
最后, QGraphicsScene允许你去渲染部分场景到绘图设备中通过QGraphicsScene::render()函数。 你可以阅读关于这个在这个文档后续的打印章节中。

The View 视图

QGraphicsView provides the view widget, which visualizes the contents of a scene. You can attach several views to the same scene, to provide several viewports into the same data set. The view widget is a scroll area, and provides scroll bars for navigating through large scenes. To enable OpenGL support, you can set a QGLWidget as the viewport by calling QGraphicsView::setViewport().
翻译:QGraphicsView提供view视图窗体, 来呈现场景。 你可以附加多个视图到相同的场景中, 在相同的数据集中提供多个视口。 视图窗体是一个滚动区域,并且提供滚动条去导航大型的场景。 想提供opengl的支持,你可以设置一个QGLWidget作为视口通过调用 QGraphicsView::setViewport()函数。

QGraphicsScene scene;
myPopulateScene(&scene);

QGraphicsView view(&scene);
view.show();

The view receives input events from the keyboard and mouse, and translates these to scene events (converting the coordinates used to scene coordinates where appropriate), before sending the events to the visualized scene.
翻译:
视图接收输入事件从键盘和鼠标, 并且把它们转化为场景事件(适当的转变为场景坐标), 在发送事件到场景之前。

Using its transformation matrix, QGraphicsView::transform(), the view can transform the scene’s coordinate system. This allows advanced navigation features such as zooming and rotation. For convenience, QGraphicsView also provides functions for translating between view and scene coordinates: QGraphicsView::mapToScene() and QGraphicsView::mapFromScene().
翻译:
使用它的转换矩阵,QGraphicsView::transform(),视图可以转换场景的坐标系统。 这允许更先进的导航特征比如缩放和旋转。 为了方便, QGraphicsView也提供了函数去转换在view和scene坐标系:QGraphicsView::mapToScene() 和 QGraphicsView::mapFromScene()函数.

在这里插入图片描述

The Item 项

QGraphicsItem is the base class for graphical items in a scene. Graphics View provides several standard items for typical shapes, such as rectangles (QGraphicsRectItem), ellipses (QGraphicsEllipseItem) and text items (QGraphicsTextItem), but the most powerful QGraphicsItem features are available when you write a custom item. Among other things, QGraphicsItem supports the following features:
Mouse press, move, release and double click events, as well as mouse hover events, wheel events, and context menu events.
Keyboard input focus, and key events
Drag and drop
Grouping, both through parent-child relationships, and with QGraphicsItemGroup
Collision detection
翻译:
QGraphicsItem是在场景中的 图形项的基类。 Graphics View提供了数个标准的item对于典型的形状, 比如矩形(QGraphicsRectItem),椭圆(QGraphicsEllipseItem)和文本item(QGraphicsTextItem), 但是最强大的特征是你可以自定义item。 与其他相比, QGraphicsItem支持以下的特征:
鼠标点击、移动、释放和双击事件,还有鼠标hover事件,滚轮事件,和上下文菜单事件。
键盘输入事件,和按键事件
拖拽和下落
分组,通过父子关系 和 QGraphicsItemGroup
碰撞检测

Items live in a local coordinate system, and like QGraphicsView, it also provides many functions for mapping coordinates between the item and the scene, and from item to item. Also, like QGraphicsView, it can transform its coordinate system using a matrix: QGraphicsItem::transform(). This is useful for rotating and scaling individual items.
翻译:
items生存在一个本地的坐标系统, 并且像QGraphicsView, 它也提供了许多函数函数去映射坐标系在item和scene之间,和 项和项之间。 同样的,像QGraphicsView一样,它可以转换它的坐标系统使用一个矩阵: QGraphicsItem::transform()。 这对于缩放和旋转个别的项是有用的。

Items can contain other items (children). Parent items’ transformations are inherited by all its children. Regardless of an item’s accumulated transformation, though, all its functions (e.g., QGraphicsItem::contains(), QGraphicsItem::boundingRect(), QGraphicsItem::collidesWith()) still operate in local coordinates.
翻译:
item可以包含其他的items(孩子)。 父亲 item的转换 被所有的儿子继承了。 不管一个项的累积转换, 尽管,所有的它的函数(例如:QGraphicsItem::contains(), QGraphicsItem::boundingRect(), QGraphicsItem::collidesWith() )仍然在本地坐标系中操作。

QGraphicsItem supports collision detection through the QGraphicsItem::shape() function, and QGraphicsItem::collidesWith(), which are both virtual functions. By returning your item’s shape as a local coordinate QPainterPath from QGraphicsItem::shape(), QGraphicsItem will handle all collision detection for you. If you want to provide your own collision detection, however, you can reimplement QGraphicsItem::collidesWith().
翻译:
QGraphicsItem支持碰撞检测通过QGraphicsItem::shape()函数,和 QGraphicsItem::collidesWith()函数, 他们都是虚函数。 通过返回你的item的形状作为一个本地的坐标QPainterPath 从QGraphicsItem::shape()函数, QGraphicsItem将操作所有碰撞检测对于你。 如果你想要去提供你自己的碰撞检测, 你可以重写QGraphicsItem::collidesWith()函数。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iEBDYodR-1585993589866)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1585129637708.png)]

Classes in the Graphics View Framework 模型视图框架的类

These classes provide a framework for creating interactive applications.
翻译:
这些类提供了一个框架去创建交互的应用程序

QGraphicsEffect
The base class for all graphics effects
翻译:所有 图形效果 的基类

QGraphicsAnchor
Represents an anchor between two items in a QGraphicsAnchorLayout
翻译:代表在QGraphicsAnchorLayout中的两个item之间的anchor

QGraphicsAnchorLayout
Layout where one can anchor widgets together in Graphics View
翻译:布局可以anchor部件到图形视图中

QGraphicsGridLayout
Grid layout for managing widgets in Graphics View
管理在 Graphics View 中的窗体 的栅格布局

QAbstractGraphicsShapeItem
Common base for all path items
翻译:所有path项 的普通基类

QGraphicsEllipseItem
Ellipse item that you can add to a QGraphicsScene
翻译:
可以添加到QGraphicsScene中的椭圆项

QGraphicsItem
The base class for all graphical items in a QGraphicsScene
翻译:
在QGraphicsScene中所有图像项的 基类

QGraphicsItemGroup
Container that treats a group of items as a single item
把一组items看做一个一个item 的容器

QGraphicsLineItem
Line item that you can add to a QGraphicsScene
翻译:
可以添加到QGraphicsScene中的 线项

QGraphicsObject
Base class for all graphics items that require signals, slots and properties
需要信号、槽特性的所有图形项 的基类

QGraphicsPathItem
Path item that you can add to a QGraphicsScene
可以添加到QGraphicsScene中的 path(矢量图形)项

QGraphicsPixmapItem
Pixmap item that you can add to a QGraphicsScene
可以添加到QGraphicsScene中的pixmap项

QGraphicsPolygonItem
Polygon item that you can add to a QGraphicsScene
可以添加到QGraphicsScene中的 多边形 项

QGraphicsRectItem
Rectangle item that you can add to a QGraphicsScene
可以添加到QGraphicsScene中的 矩形 项

QGraphicsSimpleTextItem
Simple text path item that you can add to a QGraphicsScene
可以添加到QGraphicsScene中的 简单的文本path 项

QGraphicsTextItem
Text item that you can add to a QGraphicsScene to display formatted text
可以添加到QGraphicsScene中的 文本项(可以显示有格式的文本)

QGraphicsLayout
The base class for all layouts in Graphics View
在Graphics View中所有布局的基类

QGraphicsLayoutItem
Can be inherited to allow your custom items to be managed by layouts
可以被继承 去 允许你的自定义item被布局管理

QGraphicsLinearLayout
Horizontal or vertical layout for managing widgets in Graphics View
Graphics View中水平或者垂直的布局

QGraphicsProxyWidget
Proxy layer for embedding a QWidget in a QGraphicsScene
在QGraphicsScene中嵌入一个QWidget的代理层

QGraphicsScene
Surface for managing a large number of 2D graphical items
管理大量2D图形item的表面

QGraphicsSceneContextMenuEvent
Context menu events in the graphics view framework
graphics view框架中的 上下文菜单 事件

QGraphicsSceneDragDropEvent
Events for drag and drop in the graphics view framework
graphics view框架中的 拖拽和下落 事件

QGraphicsSceneEvent
Base class for all graphics view related events
graphics view中 所有有关的事件 的基类

QGraphicsSceneHelpEvent
Events when a tooltip is requested
当请求一个 工具提示 的事件

QGraphicsSceneHoverEvent
Hover events in the graphics view framework
graphics view框架中的hover事件

QGraphicsSceneMouseEvent
Mouse events in the graphics view framework
graphics view框架中的 鼠标事件

QGraphicsSceneMoveEvent
Events for widget moving in the graphics view framework
graphics view框架中 窗体移动事件

QGraphicsSceneResizeEvent
Events for widget resizing in the graphics view framework
graphics view框架中 窗体resizing事件

QGraphicsSceneWheelEvent
Wheel events in the graphics view framework
graphics view框架中 滚轮 事件

QGraphicsTransform
Abstract base class for building advanced transformations on QGraphicsItems
在QGraphicsItems上 创建先进的转换 的抽象类

QGraphicsView
Widget for displaying the contents of a QGraphicsScene
显示一个场景内容的窗体

QGraphicsWidget
The base class for all widget items in a QGraphicsScene
在场景中的所有widget项的基类

QStyleOptionGraphicsItem
Used to describe the parameters needed to draw a QGraphicsItem
用于描述画一个QGraphicsItem需要的参数

QGraphicsSvgItem
QGraphicsItem that can be used to render the contents of SVG files
渲染SVG文件内容的QGraphicsItem

The Graphics View Coordinate System 图形视图框架坐标系统

Graphics View is based on the Cartesian coordinate system; items’ position and geometry on the scene are represented by sets of two numbers: the x-coordinate, and the y-coordinate. When observing a scene using an untransformed view, one unit on the scene is represented by one pixel on the screen.
翻译:图形视图框架基于笛卡尔坐标系; item在场景中的 的位置和geometry 被一套两个数字代表 :x坐标和y坐标。 当使用未转换的视图 观察一个场景时, 场景中的一个单元 代表在 屏幕中的一个像素。

Note: The inverted Y-axis coordinate system (where y grows upwards) is unsupported as Graphics Views uses Qt’s coordinate system.
注意:反向的y轴坐标系统(y向上生长)是不支持的 因为图形视图使用Qt的坐标系统。

There are three effective coordinate systems in play in Graphics View: Item coordinates, scene coordinates, and view coordinates. To simplify your implementation, Graphics View provides convenience functions that allow you to map between the three coordinate systems.
在图形视图中 有3种有效的坐标系统:item坐标系,scene坐标系,view坐标系。 为了简化你的使用, Graphics View提供了方便的函数 允许你去在这3中坐标系中映射。

When rendering, Graphics View’s scene coordinates correspond to QPainter’s logical coordinates, and view coordinates are the same as device coordinates. In the Coordinate System documentation, you can read about the relationship between logical coordinates and device coordinates.
当渲染时,Graphics View的场景坐标系和 QPainter的逻辑坐标系一致, 视图坐标系与设备坐标系。 在Coordinate System章节,你可以阅读关于逻辑坐标系和设备坐标系的关系。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9EXqigz3-1585993589867)(E:\md文件资源%5CUsers%5CWGJ%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5C1585364157775.png)]

Item Coordinates 项的坐标系

Items live in their own local coordinate system. Their coordinates are usually centered around its center point (0, 0), and this is also the center for all transformations. Geometric primitives in the item coordinate system are often referred to as item points, item lines, or item rectangles.
项生存在他们自己的坐标系中。 他们的坐标系通常 居中在中心点(0,0), 这也是转换的中心。 在项坐标系中的 几何图元 通常被涉及到 点/线/矩形项。

When creating a custom item, item coordinates are all you need to worry about; QGraphicsScene and QGraphicsView will perform all transformations for you. This makes it very easy to implement custom items. For example, if you receive a mouse press or a drag enter event, the event position is given in item coordinates. The QGraphicsItem::contains() virtual function, which returns true if a certain point is inside your item, and false otherwise, takes a point argument in item coordinates. Similarly, an item’s bounding rect and shape are in item coordinates.
当创建一个自定义项,项指标是你需要担心的所有; QGraphicsScene和QGraphicsView将执行所有的转换为你。 这使得实现自定义的项非常容易。 例如, 如果你接受了一个鼠标按下事件或者拖拽事件, 事件位置在 项坐标中 被给出。 QGraphicsItem::contains()虚函数 返回true 如果某个点在你的项中,否则返回false, 传一个点的参数在 项坐标中。 相似的 项的 bounding rect和shape 在项坐标系中。

At item’s position is the coordinate of the item’s center point in its parent’s coordinate system; sometimes referred to as parent coordinates. The scene is in this sense regarded as all parent-less items’ “parent”. Top level items’ position are in scene coordinates.
项的位置 是 项的中心点 在 父坐标系中的坐标; 有时被称为父坐标系。 场景在这个意义上被认作所有 没有父亲 项的父亲。 顶层的项的位置 在场景坐标中。

Child coordinates are relative to the parent’s coordinates. If the child is untransformed, the difference between a child coordinate and a parent coordinate is the same as the distance between the items in parent coordinates. For example: If an untransformed child item is positioned precisely in its parent’s center point, then the two items’ coordinate systems will be identical. If the child’s position is (10, 0), however, the child’s (0, 10) point will correspond to its parent’s (10, 10) point.
子坐标系与父坐标系相关。 如果子坐标系没有旋转,子坐标和父坐标的区别 和 项在父坐标之间的距离 是一样的。 例如:如果一个未转换的子项恰好在父亲的中心点, 那么这两个项坐标系统将是一致的。 如果孩子的坐标是 (10, 0),孩子的(0, 10)点与父亲中的(10, 10)点一致。

Because items’ position and transformation are relative to the parent, child items’ coordinates are unaffected by the parent’s transformation, although the parent’s transformation implicitly transforms the child. In the above example, even if the parent is rotated and scaled, the child’s (0, 10) point will still correspond to the parent’s (10, 10) point. Relative to the scene, however, the child will follow the parent’s transformation and position. If the parent is scaled (2x, 2x), the child’s position will be at scene coordinate (20, 0), and its (10, 0) point will correspond to the point (40, 0) on the scene.
子项的坐标不会被 父项的转换所影响, 因为项的位置和转换 和父亲的相关,尽管如此 父亲的转换暗示的转换了孩子。 在上面的例子中, 尽管父亲旋转和缩放,孩子的(0, 10) 点仍然与父亲的 (10, 10)点相一致。 关系到场景, 无论如何, 孩子将跟随父亲的转换和位置。 如果父亲缩放了2倍, 孩子的位置将在场景坐标系中 (20, 0),它的(10, 0)点将在场景中的 (40, 0)点。

With QGraphicsItem::pos() being one of the few exceptions, QGraphicsItem’s functions operate in item coordinates, regardless of the item, or any of its parents’ transformation. For example, an item’s bounding rect (i.e. QGraphicsItem::boundingRect()) is always given in item coordinates.
QGraphicsItem的函数执行在项坐标系中, 不管项, 或者任何父亲的转换。 例如一个项的环绕矩形(也就是QGraphicsItem::boundingRect())在项坐标系中, 除了像QGraphicsItem::pos()极少的函数。

Scene Coordinates场景坐标系

The scene represents the base coordinate system for all its items. The scene coordinate system describes the position of each top-level item, and also forms the basis for all scene events delivered to the scene from the view. Each item on the scene has a scene position and bounding rectangle (QGraphicsItem::scenePos(), QGraphicsItem::sceneBoundingRect()), in addition to its local item pos and bounding rectangle. The scene position describes the item’s position in scene coordinates, and its scene bounding rect forms the basis for how QGraphicsScene determines what areas of the scene have changed. Changes in the scene are communicated through the QGraphicsScene::changed() signal, and the argument is a list of scene rectangles.

场景代表所有项的基础坐标系统。 场景坐标系描述了每个顶层项的位置, 野形成了所有场景事件的基础。 场景中的每个项 除了它们本地的pos和bounding rectangle 还有一个场景pos和bounding rectangle(QGraphicsItem::scenePos(), QGraphicsItem::sceneBoundingRect())。 场景位置描述了在场景坐标系中项的位置,它和场景bounding rect一起组成了基础 让QGraphicsScene决定场景中的那些区域发生了改变。 场景中的改变通过QGraphicsScene::changed()信号交流,参数是一个 场景矩形 的list。

View Coordinates 视图坐标

View coordinates are the coordinates of the widget. Each unit in view coordinates corresponds to one pixel. What’s special about this coordinate system is that it is relative to the widget, or viewport, and unaffected by the observed scene. The top left corner of QGraphicsView’s viewport is always (0, 0), and the bottom right corner is always (viewport width, viewport height). All mouse events and drag and drop events are originally received as view coordinates, and you need to map these coordinates to the scene in order to interact with items.

视图坐标系是widget坐标系。 每个单元在视图坐标系中对应一个pix像素点。 这个坐标系的特殊点是它与widget或viewport(视口)相关, 并且不被观察的场景影响。 QGraphicsView的viewport(视口)的左上角总是(0, 0),右下角总是(视口的宽度,视口的高度)。 所有的鼠标事件和拖拽事件本来被视图坐标系接收, 你需要吧这些坐标映射到场景中 以便 和项进行联系。

Coordinate Mapping 坐标映射

Often when dealing with items in a scene, it can be useful to map coordinates and arbitrary shapes from the scene to an item, from item to item, or from the view to the scene. For example, when you click your mouse in QGraphicsView’s viewport, you can ask the scene what item is under the cursor by calling QGraphicsView::mapToScene(), followed by QGraphicsScene::itemAt(). If you want to know where in the viewport an item is located, you can call QGraphicsItem::mapToScene() on the item, then QGraphicsView::mapFromScene() on the view. Finally, if you use want to find what items are inside a view ellipse, you can pass a QPainterPath to mapToScene(), and then pass the mapped path to QGraphicsScene::items().
通常处理场景中的项, 可能是有用的 映射坐标和任意的图形 从场景到项,或者从视图到场景。 例如,当我们点击鼠标在QGraphicsView的视口上, 你可以问场景在光标下有什么项 通过调用QGraphicsView::mapToScene(),接着调用QGraphicsScene::itemAt()。 如果你想知道项在视口中的位置, 你可以调用QGraphicsItem::mapToScene()在这个项上, 然后调用QGraphicsView::mapFromScene()在视图上。 最后,如果你想要发现在 视图椭圆 里有什么项, 你可以传递一个QPainterPath到mapToScene(), 然后传递这个 被映射的path到QGraphicsScene::items()。

You can map coordinates and shapes to and from an item’s scene by calling QGraphicsItem::mapToScene() and QGraphicsItem::mapFromScene(). You can also map to an item’s parent item by calling QGraphicsItem::mapToParent() and QGraphicsItem::mapFromParent(), or between items by calling QGraphicsItem::mapToItem() and QGraphicsItem::mapFromItem(). All mapping functions can map both points, rectangles, polygons and paths.
你也可以映射坐标和形状 到/从 项的场景 通过调用QGraphicsItem::mapToScene() 和 QGraphicsItem::mapFromScene()。 你也可以 映射到项的父项通过调用QGraphicsItem::mapToParent() 和 QGraphicsItem::mapFromParent(), 或者在项之间调用QGraphicsItem::mapToItem() 和 QGraphicsItem::mapFromItem()。 所有的映射函数可以映射点,矩形,多边形和path。

The same mapping functions are available in the view, for mapping to and from the scene. QGraphicsView::mapFromScene() and QGraphicsView::mapToScene(). To map from a view to an item, you first map to the scene, and then map from the scene to the item.
同样的映射函数在视图中可用, 为了 转换到/从 场景。 QGraphicsView::mapFromScene() 和 QGraphicsView::mapToScene()。 映射从视图到项, 你首先映射到场景,然后再从场景映射到项。

Key Features 主要特征

Zooming and rotating

QGraphicsView supports the same affine transformations as QPainter does through QGraphicsView::setMatrix(). By applying a transformation to the view, you can easily add support for common navigation features such as zooming and rotating.
QGraphicsView支持与 QPainter相同的仿射转换 通过QGraphicsView::setMatrix()函数。 通过 把转换应用到视图上 你可以更加简单的支持普遍的导航功能比如缩放和旋转。

Here is an example of how to implement zoom and rotate slots in a subclass of QGraphicsView:
这儿是如何在QGraphicsView的子类中实现缩放和旋转槽函数。

class View : public QGraphicsView
{
Q_OBJECT

public slots:
void zoomIn() { scale(1.2, 1.2); }
void zoomOut() { scale(1 / 1.2, 1 / 1.2); }
void rotateLeft() { rotate(-10); }
void rotateRight() { rotate(10); }

};

The slots could be connected to QToolButtons with autoRepeat enabled.
槽函数可以被连接到QToolButtons 使能autoRepeat属性

QGraphicsView keeps the center of the view aligned when you transform the view.
QGraphicsView保持视图中心对齐当你转换视图。

See also the Elastic Nodes example for code that shows how to implement basic zooming features.
也可以看Elastic Nodes例子 展示如何去实现基本的缩放特征。

Printing 打印(暂时没翻译)

Drag and Drop 拖放

Because QGraphicsView inherits QWidget indirectly, it already provides the same drag and drop functionality that QWidget provides. In addition, as a convenience, the Graphics View framework provides drag and drop support for the scene, and for each and every item. As the view receives a drag, it translates the drag and drop events into a QGraphicsSceneDragDropEvent, which is then forwarded to the scene. The scene takes over scheduling of this event, and sends it to the first item under the mouse cursor that accepts drops.
因为QGraphicsView间接的继承自QWidget, 它也提供了QWidget提供的拖拽功能。 另外,为了方便, 模型视图框架提供了对于场景的拖拽支持,以及每个item。 若视图接受了拖拽,它把拖拽事件翻译成QGraphicsSceneDragDropEvent,然后发送到场景。 场景掌控了事件的调度,并且把它发送给鼠标光标下接受拖拽的第一个item。

To start a drag from an item, create a QDrag object, passing a pointer to the widget that starts the drag. Items can be observed by many views at the same time, but only one view can start the drag. Drags are in most cases started as a result of pressing or moving the mouse, so in mousePressEvent() or mouseMoveEvent(), you can get the originating widget pointer from the event. For example:
为了从项中开始拖拽, 创建一个QDrag对象, 传递一个指向widget的指针来开始这个拖拽。 项可以被许多视图观察在同一时刻, 但是仅一个视图可以开始拖拽。 拖拽大多数情况作为 点击和移动鼠标的结果 开始, 因此, 在mousePressEvent() 或 mouseMoveEvent(),你可以获取原生widget的指针从这个事件中,例如

void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QMimeData *data = new QMimeData;
data->setColor(Qt::green);
QDrag *drag = new QDrag(event->widget());
drag->setMimeData(data);
drag->start();
}

To intercept drag and drop events for the scene, you reimplement QGraphicsScene::dragEnterEvent() and whichever event handlers your particular scene needs, in a QGraphicsItem subclass. You can read more about drag and drop in Graphics View in the documentation for each of QGraphicsScene’s event handlers.
为了截取 场景中的 拖拽事件,你重新实现QGraphicsScene::dragEnterEvent() 和 任何一个你的特殊场景需要的 事件处理函数, 在QGraphicsItem子类中。 你可以阅读更多关于拖拽在图形视图 在这个文档中 每个QGraphicsScene的事件处理函数。

Items can enable drag and drop support by calling QGraphicsItem::setAcceptDrops(). To handle the incoming drag, reimplement QGraphicsItem::dragEnterEvent(), QGraphicsItem::dragMoveEvent(), QGraphicsItem::dragLeaveEvent(), and QGraphicsItem::dropEvent().
项可以使能拖拽支持通过调用QGraphicsItem::setAcceptDrops()函数。 为了操作即将到来的拖拽, 重写QGraphicsItem::dragEnterEvent(), QGraphicsItem::dragMoveEvent(), QGraphicsItem::dragLeaveEvent(), 和 QGraphicsItem::dropEvent()。

See also the Drag and Drop Robot example for a demonstration of Graphics View’s support for drag and drop operations.
也可以看Drag and Drop Robot例子 模型视图对于拖拽操作的支持。

Cursors and Tooltips 光标和工具提示

Like QWidget, QGraphicsItem also supports cursors (QGraphicsItem::setCursor()), and tooltips (QGraphicsItem::setToolTip()). The cursors and tooltips are activated by QGraphicsView as the mouse cursor enters the item’s area (detected by calling QGraphicsItem::contains()).
像QWidget一样,QGraphicsItem也提供了光标(QGraphicsItem::setCursor())和提示工具(QGraphicsItem::setToolTip())。 你的光标和提示被QGraphicsView激活 当光标进入item的区域(通过调用QGraphicsItem::contains()检查)

You can also set a default cursor directly on the view by calling QGraphicsView::setCursor().
你也可以直接在视图上设置一个默认的焦点通过调用QGraphicsView::setCursor()。

See also the Drag and Drop Robot example for code that implements tooltips and cursor shape handling.
查看Drag and Drop Robot例子的代码 实现了光标和提示状态操作。

Animation动画

Graphics View supports animation at several levels. You can easily assemble animation by using the Animation Framework. For that you’ll need your items to inherit from QGraphicsObject and associate QPropertyAnimation with them. QPropertyAnimation allows to animate any QObject property.
Graphics View支持动画在几哥标准上。 你可以简单的组合动画通过使用动画框架。 为此你需要把item从QGraphicsObject继承 并且把QPropertyAnimation和它连接起来。 QPropertyAnimation允许动画任何QObject属性。

Another option is to create a custom item that inherits from QObject and QGraphicsItem. The item can the set up its own timers, and control animations with incremental steps in QObject::timerEvent().
另一个选项是去创建一个自定义的item 继承自QObject 和 QGraphicsItem。 item可以设置自己的定时器, 并且在QObject::timerEvent()中控制增量步骤。

A third option, which is mostly available for compatibility with QCanvas in Qt 3, is to advance the scene by calling QGraphicsScene::advance(), which in turn calls QGraphicsItem::advance().
第三个选项,是为了兼容Qt3中的QCanvas, 调用QGraphicsScene::advance()会相应的调用QGraphicsItem::advance()。

OpenGL Rendering OpenGL 渲染

Item Groups 项分组

By making an item a child of another, you can achieve the most essential feature of item grouping: the items will move together, and all transformations are propagated from parent to child.
通过让一个项成为另一个的孩子, 你可以获取到非常必要的特征:这个项将一起移动, 并且所有的转换从父亲传给儿子。

In addition, QGraphicsItemGroup is a special item that combines child event handling with a useful interface for adding and removing items to and from a group. Adding an item to a QGraphicsItemGroup will keep the item’s original position and transformation, whereas reparenting items in general will cause the child to reposition itself relative to its new parent. For convenience, you can create QGraphicsItemGroups through the scene by calling QGraphicsScene::createItemGroup().
另外,QGraphicsItemGroup是一个特殊的item 用一个有用的接口把孩子的事件操作融合 这个接口可以添加和删除从/到组。 添加一个项到QGraphicsItemGroup将保持项的初始位置和转换, 而让项改变父亲将导致孩子重新定位它和新的父亲的联系。 为了方便, 你可以创建QGraphicsItemGroups通过调用QGraphicsScene::createItemGroup()函数。

Widgets and Layouts窗体和布局

Qt 4.4 introduced support for geometry and layout-aware items through QGraphicsWidget. This special base item is similar to QWidget, but unlike QWidget, it doesn’t inherit from QPaintDevice; rather from QGraphicsItem instead. This allows you to write complete widgets with events, signals & slots, size hints and policies, and you can also manage your widgets geometries in layouts through QGraphicsLinearLayout and QGraphicsGridLayout.
Qt4.4引进了几何和布局项通过QGraphicsWidget。 这个基于项的窗体和QWidget类似, 但是和QWidget不一样, 它不是继承自QPaintDevice,而是QGraphicsItem。 这允许你去写完整的窗体用信号和槽, 尺寸和策略, 并且你也可以管理在布局中的窗体通过QGraphicsLinearLayout 和 QGraphicsGridLayout。

QGraphicsWidget

Building on top of QGraphicsItem’s capabilities and lean footprint, QGraphicsWidget provides the best of both worlds: extra functionality from QWidget, such as the style, font, palette, layout direction, and its geometry, and resolution independence and transformation support from QGraphicsItem. Because Graphics View uses real coordinates instead of integers, QGraphicsWidget’s geometry functions also operate on QRectF and QPointF. This also applies to frame rects, margins and spacing. With QGraphicsWidget it’s not uncommon to specify contents margins of (0.5, 0.5, 0.5, 0.5), for example. You can create both subwidgets and “top-level” windows; in some cases you can now use Graphics View for advanced MDI applications.
基于QGraphicsItem的功能,QGraphicsWidget提供了2个最好的世界:比QWidget额外的功能,例如样式,调色板,布局和位置, 和 独立于分辨率 和转换 支持从QGraphicsItem。 因为图形视图框架使用真正的坐标而不是整数, QGraphicsWidget的位置函数也操作在QRectF 和 QPointF。 这也应用于矩形, 边距和空间。 指定内容边距为 (0.5, 0.5, 0.5, 0.5)实不罕见的。有时候你可以使用图形视图为了先进的MDI应用。

Some of QWidget’s properties are supported, including window flags and attributes, but not all. You should refer to QGraphicsWidget’s class documentation for a complete overview of what is and what is not supported. For example, you can create decorated windows by passing the Qt::Window window flag to QGraphicsWidget’s constructor, but Graphics View currently doesn’t support the Qt::Sheet and Qt::Drawer flags that are common on macOS.
一些QWidget的属性是支持的,包括window flags 和 attributes,但是不是所有的。 你应该查阅QGraphicsWidget的类文档 有一个概观 什么支持什么不支持。 例如,你可以创建一个装饰的窗体通过传递Qt::Window窗体标志给QGraphicsWidget的构造函数, 但是图形视图当前不支持 Qt::Sheet 和 Qt::Drawer 在macOS上的。

QGraphicsLayout

QGraphicsLayout is part of a second-generation layout framework designed specifically for QGraphicsWidget. Its API is very similar to that of QLayout. You can manage widgets and sublayouts inside either QGraphicsLinearLayout and QGraphicsGridLayout. You can also easily write your own layout by subclassing QGraphicsLayout yourself, or add your own QGraphicsItem items to the layout by writing an adaptor subclass of QGraphicsLayoutItem.
QGraphicsLayout是专为QGraphicsWidget设计的第二低布局框架。它的API和QLayout非常相似。 你可以用QGraphicsLinearLayout 和 QGraphicsGridLayout管理窗体和子布局。 你也可以简单的写你自己的布局通过继承QGraphicsLayout, 或者添加你的QGraphicsItem项到布局中 通过继承自QGraphicsLayoutItem。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值