• 3616阅读
  • 2回复

[提问]新手请教一个QGraphicsItem的问题! [复制链接]

上一主题 下一主题
离线dqy0582
 

只看楼主 倒序阅读 楼主  发表于: 2015-10-16
我在界面设计器ui里加入了一个Tabwidget,又在tab1里加了一个QGraphicsView,自定义了一个QGraphicsItem图形,但是加入scene以后要不就不显示,要不就显示不完整,有时候将窗口最小化以后再打开又能完整显示了,以前不用ui编辑器不加Tabwidget只有一个graphicsview是一直都可以完整显示的,在绘制函数中加入update()也没有改善,这是怎么回事啊,求高手赐教啊,万分感谢!


离线feipeng1985

只看该作者 1楼 发表于: 2015-10-16
你自定义的图形肯定是boundingRect没有实现好,所以造成刷新有问题,这个是你的图形的重绘区域
QRectF QGraphicsItem::boundingRect() const [pure virtual]

This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted to inside an item's bounding rect. QGraphicsView uses this to determine whether the item requires redrawing.

Although the item's shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items' transformation.

If you want to change the item's bounding rectangle, you must first call prepareGeometryChange(). This notifies the scene of the imminent change, so that it can update its item geometry index; otherwise, the scene will be unaware of the item's new geometry, and the results are undefined (typically, rendering artifacts are left within the view).

Reimplement this function to let QGraphicsView determine what parts of the widget, if any, need to be redrawn.

Note: For shapes that paint an outline / stroke, it is important to include half the pen width in the bounding rect. It is not necessary to compensate for antialiasing, though.

离线dqy0582

只看该作者 2楼 发表于: 2015-10-19
回 feipeng1985 的帖子
feipeng1985:你自定义的图形肯定是boundingRect没有实现好,所以造成刷新有问题,这个是你的图形的重绘区域
QRectF QGraphicsItem::boundingRect() const [pure virtual]
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted  .. (2015-10-16 16:59) 

多谢,我自己也发现了,我为了防止这个图元不和别的图元发生碰撞被collidingItems()检测出来,把boudingrect的大小设为了(0,0),结果就出问题了。那我顺便问一下,如果我有多个图元,我只要这个图元不被检测出碰撞,有什么办法么?谢谢
快速回复
限100 字节
 
上一个 下一个