QGraphic学习笔记 - 图文

发布时间 : 星期三 文章QGraphic学习笔记 - 图文更新完毕开始阅读

hoverEnterEvent(), hoverMoveEvent(), and hoverLeaveEvent() 接受鼠标悬浮 移动和离开事件

inputMethodEvent()函数处理输入法事件,

keyPressEvent() and keyReleaseEvent()事件处理键盘按下和松开事件 mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), and mouseDoubleClickEvent()处理鼠标按下,移动,松开,双击事件

也可以为一些item过滤一些事件通过安装过滤器,这个和QT一般的事件过滤器不一样,一般的过滤器只工作在Qobject和它的子类,通过调用installSceneEventFilter()为item安装了过滤器后,被过滤的事件将会被虚函数sceneEventFilter().捕捉到,可以通过调用函数removeSceneEventFilter().来去除掉事件过滤器

Custom Data数据

有些时候为item注册一些数值很有用,做一个普通的item或者标准的item,可以调用setData()来为任一个item设置值,这个值使用key-value对,(key是整形,value是变种数据Qvarient)来得到item的数据,通过调用data(),

1、QVariant QGraphicsItem::itemChange ( GraphicsItemChange change, const QVariant & value ) [virtual protected]

这个函数被QGraphicsItem调用用来标明items的一些状态改变了,通过重载这个函数,可以对自己定义事件响应,在一些情况下,可以做一些调整。

参数change是改变的那个item的改变状态参数,value是一个新的数据,他的类型取决于change,

change是QGraphicsItem::GraphicsItemChange的枚举变量

enum GraphicsItemChange { ItemEnabledChange, ItemEnabledHasChanged, ItemMatrixChange, ItemPositionChange, ..., ItemScenePositionHasChanged } 例如:

QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)

{

if (change == ItemPositionChange && scene()) { // value is the new position. QPointF newPos = value.toPointF(); QRectF rect = scene()->sceneRect(); if (!rect.contains(newPos)) {

// Keep the item inside the scene rect.

newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); return newPos; } }

return QGraphicsItem::itemChange(change, value); }

默认的函数什么都不做,只返回value

注意:在使用这个函数时候,在函数内部调用函数时候要小心,因为这可能导致一些意想不到的结果,例如:你不能再这个函数里面调用setPos()在change是ItemPositionChange时候,由于setPos()函数将会再次调用itemChange(ItemPositionChange),如此就一直循环下去了。

2、void QGraphicsItem::setFlag ( GraphicsItemFlag flag, bool enabled = true ) void QGraphicsItem::setFlags ( GraphicsItemFlags flags )

把flags设置为item的属性,如果item获得了光标,但是flags没有使能ItemsFocusable,这个item

将会丢失光标,同样的,当item被选择到,但是没有使能ItemsSelectable,这个item会自动的失去 选择。

默认的,所有的flags都是不可用的。(QGraphicsWidget 为了获得位置变化默认使能了 ItemSendsGeometryChanges) 相近的函数

GraphicsItemFlags QGraphicsItem::flags () const

返回item的所有使能的flags,例如,如果flags里面包含了ItemIsFocusable,这个item可以接受 输入光标

3、QPainterPath QGraphicsItem::shape () const [virtual]

以QPainterPath返回item在local坐标中的形状,这个形状可以用来做很多事情,包括碰撞侦测,

打击测试,还有用来 QGraphicsScene::items() 函数

默认的函数调用boundingRect()返回一个简单的矩形形状,但是子类可以重载这个函数,为非矩形

的item返回一个更加精准的形状,例如一个圆形的item可以选择返回一个椭圆形,用来获得更好的

碰撞侦测效果。代码:

QPainterPath RoundItem::shape() const {

QPainterPath path;

path.addEllipse(boundingRect()); return path; }

形状的轮廓线可以通过绘制时候的pen来变化

4、QRectF QGraphicsItem::boundingRect () const [pure virtual]

这个纯虚函数用矩形声明了item的边界轮廓,所有的绘制都必须限定在item的矩形边框内。 QGraphicsView使用这个方法来决定item是否需要重绘

尽管item的形状可以是任意的,但是边框一直都是矩形,不影响items的变换

如果想改变items的边框,应该首先调用prepareGeometryChange(),这将通知场景scene即将发生的变化,这样场景可以刷新item的位置下标。否则,场景将不会察觉到item的变化,结果也未知。

如果要重绘item时候,重载这个函数来让QGraphicsView来决定item的边界区域, 注意:由于绘制边界时候的边界轮廓线,在这个矩形区域内包含画笔pen宽度的一半很重要,不需

要补偿画图走样 例如

QRectF CircleItem::boundingRect() const {

qreal penWidth = 1;

return QRectF(-radius - penWidth / 2, -radius - penWidth / 2, diameter + penWidth, diameter + penWidth); }

联系合同范文客服:xxxxx#qq.com(#替换为@)