qgraphicsscene(qGraphicsscene动态画线)

http://www.itjxue.com  2023-01-25 06:31  来源:未知  点击次数: 

QT:QGraphicsView QGraphicsScene QGraphicsItem理解

最近使用QT来编写一个可局域网联机对战的小游戏,主要使用GraphicsView来实现游戏界面,游戏动画方面由于坐标系和函数机制方面的不了解,做起来有点蛋疼,用了一下午和同伴一起写demo、查资料,大致理清了头绪,整理一下理解。

QGraphicsView一般用法是要 setScene(QGraphicsScene) 绑定一个Scene对象。QGraphicsScene的坐标系是这样的:

Item坐标系的x轴、y轴、原点的关系跟Scene一样。在QGraphicsItem中有几个重要的函数,分别是 boundingRect() 、 shape() 、 paint() ,这函数绘图都是基于Item的坐标系,着重强调,基于的原点是Item坐标系的原点,使用 paint() 来绘制Item时需要注意。而Item坐标系的原点又是一个大坑。

在Scene使用 addItem() 添加Item时,添加的Item的坐标系原点默认在画面的中央,可以调用 setPos(x,y) 函数来重设Item坐标系原点,这时Item坐标系原点处于设置的x、y处。

Item的重绘可以在Scene中调用 Invalidate(flag) 来重绘Item层,也可以在Item中调用 update() 。此时的重绘会发生在 boundingRect() 函数返回的矩形区域中,同时这个矩形区域也是响应事件的区域,在没有重载 shape() 的情况下还会用于碰撞检测。

使用 prepareGeometryChange() 函数可以修改 boudingRect() 返回的矩形区域大小,同时还会根据情况来重绘视图。

github项目

qt中怎么设置QGraphicsScene *scene 的大小啊,不是QGraphicsView

可以使用setSceneRect()设置QGraphicsScene的大小。如果不设置,则默认为scene中包含所有子元素的边界区域( itemsBoundingRect()函数的返回值)。

更详细的说明参看QGraphicsScene的文档,讲解很详细,看下面这段:

The scene's bounding rect is set by calling setSceneRect(). Items can be placed at any position on the scene, and the size of the scene is by default unlimited. The scene rect is used only for internal bookkeeping, maintaining the scene's item index. If the scene rect is unset, QGraphicsScene will use the bounding area of all items, as returned by itemsBoundingRect(), as the scene rect. However, itemsBoundingRect() is a relatively time consuming function, as it operates by collecting positional information for every item on the scene. Because of this, you should always set the scene rect when operating on large scenes.

Qt中的QGraphicsScene等对象的坐标系是怎么设置的

可以使用setSceneRect()设置QGraphicsScene的大小。如果不设置,则默认为scene中包含所有子元素的边界区域( itemsBoundingRect()函数的返回值)。

更详细的说明参看QGraphicsScene的文档,讲解很详细,看下面这段:

The scene's bounding rect is set by calling setSceneRect(). Items can be placed at any position on the scene, and the size of the scene is by default unlimited. The scene rect is used only for internal bookkeeping, maintaining the scene's item index. If the scene rect is unset, QGraphicsScene will use the bounding area of all items, as returned by itemsBoundingRect(), as the scene rect. However, itemsBoundingRect() is a relatively time consuming function, as it operates by collecting positional information for every item on the scene. Because of this, you should always set the scene rect when operating on large scenes.

QGraphicsScene的存储,该怎么解决

QGraphicsScene的存储解决办法:

到控制达到事件循环的时候,QGraphicsScene类就会发送changed消息,然后region参数存储的是发生改变的scene rectangles。

(责任编辑:IT教学网)

更多

相关网络赚钱文章

推荐网络赚钱文章