找回密码
 立即注册
搜索
查看: 372|回复: 0

Qt 图形视图框架(Graphics View Framework)

[复制链接]

266

主题

0

回帖

1119

积分

管理员

积分
1119
发表于 2023-12-29 00:24:49 | 显示全部楼层 |阅读模式

简介

Qt 的图形视图提供了支持大量自定义的 2D 图形项(QGraphicsItem)交互的管理器(QGraphicsScene),以及一个支持缩放和旋转操作的视图部件(QGraphicsView)用于显示这些图形。框架还包含了支持对图形项精确交互的事件传播架构。图形视图使用 BSP( Binary Space Partitioning ,二叉空间分割) 树提供对图形项的快速查找,正因如此,即使是包含数以百万计对象的超大场景,也能够实时地显示。图形视图提供了基于图形项的 model-view 编程模式,这有点类似 QTableView、QTreeView 和 QListView。同一个场景可以由多个视图来显示,一个场景中则可包含多种不同形状的图形项。

演员、道具 Ready

QGraphicsItem(图形项) 是 QGraphicsScene 场景中的基础图形类,好比是电影场景中的演员、道具。

Qt 视图体系提供了一些派生于 QGraphicsItem 的标准图形项:矩形(QGraphicsRectItem)、椭圆(QGraphicsEllipseItem)、文本(QGraphicsTextItem)等。QGraphicsItem 支持如下功能:

  • 鼠标的按下、移动、释放、双击事件,此外还有鼠标的悬浮、滚轮和上下文菜单事件;
  • 键盘的输入焦点和按键事件;
  • 拖放;
  • 通过父子关系或使用 QGraphicsItemGroup 的组合;
  • 碰撞检测。

如果要实现自定义的图形,需要继承 QGraphicsItem 并重写两个纯虚公共函数 boundingRect() 和 paint(),boundingRect() 用于返回图形的大致轮廓(矩形),paint() 则用于绘制图形的实际内容。图形的轮廓用于定于绘制的范围、碰撞检测、判断是否处于事件(鼠标按下、经过等)的接收范围内等。如果要精确定义图形的轮廓则还需要重写虚函数 shape() 。当存在 shape() 定义的轮廓时,碰撞检测、事件相应范围等检测将取决于 shape() 的返回值。

Action

QGraphicsScene(图形场景)可以理解为电影的场景,场景也就是场面,一般是指在一个特定的空间内发生的故事的画面。比如《林冲夜宿山神庙》是一个故事,林冲、山神庙、纷飞的大雪等人物以及所发生的故事就是一个场景了。

QGraphicsScene 是 QGraphicsItems 2D 图形的容器,提供图形的操作接口、传递事件和管理各个图形的的状态,它有如下职责:

+ 提供一个快速的接口用来管理大量的图形项;

  • 向每个图形项传递事件;
  • 管理图形的状态,如选中状态、焦点的处理;
  • 提供无变形的展示功能,主要为了打印。

Cut! 收工了

QGraphicsView(图形视图)是一个可视部件,好比是电影荧幕,是一个面向观众的视口(viewport),有了它我们才能看到 QGraphicsScene 场景中所演出的内容。与电影荧幕的痛的是,QGraphicsView 还提供了观众与电影中的演员、道具的交互的方法,或许用舞台剧代替电影来比喻更贴切些。

QGraphicsView 实现了 QGraphicsItem 的可视化,还提供了缩放和旋转得支持。

那谁,盒饭钱总该给报销掉吧~

图形视图坐标系

图形视图基于笛卡尔坐标系,在场景中,图形项的位置和形状使用 x 轴坐标与 y 轴坐标来表示。当使用未经变形的视图来观察场景时,场景中的一个单位等于屏幕上的一个像素。在图形视图系统中有三种坐标系:图形项坐标系、场景坐标系与视图坐标系。为了便于使用,提供了用于坐标系映射的转换函数。

框架中的类

下面这些类提供了创建交互式应用程序的框架:

类名 说明
QAbstractGraphicsShapeItem Common base for all path items
QGraphicsAnchor Represents an anchor between two items in a QGraphicsAnchorLayout
QGraphicsAnchorLayout Layout where one can anchor widgets together in Graphics View
QGraphicsEffect The base class for all graphics effects
QGraphicsEllipseItem Ellipse item that you can add to a QGraphicsScene
QGraphicsGridLayout Grid layout for managing widgets in Graphics View
QGraphicsItem The base class for all graphical items in a QGraphicsScene
QGraphicsItemGroup Container that treats a group of items as a single item
QGraphicsLayout The base class for all layouts in Graphics View
QGraphicsLayoutItem Can be inherited to allow your custom items to be managed by layouts
QGraphicsLineItem Line item that you can add to a QGraphicsScene
QGraphicsLinearLayout Horizontal or vertical layout for managing widgets in Graphics View
QGraphicsObject Base class for all graphics items that require signals, slots and properties
QGraphicsPathItem Path item that you can add to a QGraphicsScene
QGraphicsPixmapItem Pixmap item that you can add to a QGraphicsScene
QGraphicsPolygonItem Polygon item that you can add to a QGraphicsScene
QGraphicsProxyWidget Proxy layer for embedding a QWidget in a QGraphicsScene
QGraphicsRectItem Rectangle item that you can add to a QGraphicsScene
QGraphicsScene Surface for managing a large number of 2D graphical items
QGraphicsSceneContextMenuEvent Context menu events in the graphics view framework
QGraphicsSceneDragDropEvent Events for drag and drop in the graphics view framework
QGraphicsSceneEvent Base class for all graphics view related events
QGraphicsSceneHelpEvent Events when a tooltip is requested
QGraphicsSceneHoverEvent Hover events in the graphics view framework
QGraphicsSceneMouseEvent Mouse events in the graphics view framework
QGraphicsSceneMoveEvent Events for widget moving in the graphics view framework
QGraphicsSceneResizeEvent Events for widget resizing in the graphics view framework
QGraphicsSceneWheelEvent Wheel events in the graphics view framework
QGraphicsSimpleTextItem Simple text path item that you can add to a QGraphicsScene
QGraphicsTextItem Text item that you can add to a QGraphicsScene to display formatted text
QGraphicsTransform Abstract base class for building advanced transformations on QGraphicsItems
QGraphicsView Widget for displaying the contents of a QGraphicsScene
QGraphicsWidget The base class for all widget items in a QGraphicsScene
QStyleOptionGraphicsItem Used to describe the parameters needed to draw a QGraphicsItem

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|棱讯科技 ( 粤ICP备2024228160号-2|粤公网安备44030002003510号 )

GMT+8, 2024-7-27 17:41 , Processed in 0.018796 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表