QT Graphics-View图元组使用

经过把一个item做为另外一个item的孩子,你能够获得item组的大多数本质特性:这些items会一块儿移动,全部变换会从父到子传递。QGraphicsItem也能够为它的孩子处理全部的事件,这样就容许以父亲表明它全部的孩子,能够有效地把全部的items看做一个总体。app

另外,QGraphicsItemGroup是一个特殊的item,它既对孩子事件进行处理又有一个接口把items从一个组中增长和删除。把一个item加到 QGraphicsItemGroup仍会保留item的原始位置与变换,而给一个item从新指定父item则会让item根据其新的父亲从新定位。能够用QGraphicsScene::createItemGroup()建组。ide

 

一、经过父子关系-若是想要将 items 存储在其余 item 内,能够直接将任何 QGraphicsItem 经过为 setParentItem() 传递一个合适的 parent。this

注意: 对于该方式,QGraphicsItem 能够有本身的子 item 对象。可是,QGraphicsItem 没有 API(例如:setItems()、addChild())添加孩子,它只能容许孩子附加到 parent (setParentItem()),想一想也挺神奇的。spa

 // Item parent-children.net

QGraphicsRectItem*  pRectItemTmp  = new QGraphicsRectItem(QRectF(-100.0, -100.0, 50.0, 50.0));code

QGraphicsEllipseItem*   pEllipseItemTmp = new QGraphicsEllipseItem(QRectF(-100.0, -100.0, 50.0, 50.0));对象

pEllipseItemTmp->setParentItem(pRectItemTmp);blog

pScene->addItem(pRectItemTmp);接口

pRectItemTmp->setFlag(QGraphicsItem::ItemIsSelectable);事件

pRectItemTmp->setFlag(QGraphicsItem::ItemIsMovable);

 

二、QGraphicsItemGroup(图元组)是一个容器,它的做用是将加入到该组里的图元当成一个图元来看待。QGraphicsItemGroup的父类是QGraphicsItem,因此它本质上也是一个图元,只是这个图元自己是不可见的。

QGraphicsItemGroup有两种建立方法:

一种是手动建立QGraphicsItemGroup对象而后再加入到场景中。

另外一种是使用场景类的createItemGroup方法建立,该方法返回一个QGraphicsItemGroup对象。

// Item Group

// [1]

QGraphicsRectItem* pRectItem  = new QGraphicsRectItem(QRectF(-30.0, -30.0, 60.0, 60.0));

QGraphicsEllipseItem* pEllipseItem = new QGraphicsEllipseItem(QRectF(-30.0, -30.0, 60.0, 60.0));

QGraphicsItemGroup* pItemGroup = new QGraphicsItemGroup();

pItemGroup->setFlag(QGraphicsItem::ItemIsSelectable);

pItemGroup->setFlag(QGraphicsItem::ItemIsMovable);

pItemGroup->addToGroup(pRectItem);

pItemGroup->addToGroup(pEllipseItem);

pScene->addItem(pItemGroup);

 

// [2]

QGraphicsRectItem* pRectItemEx  = new QGraphicsRectItem(QRectF(-30.0, -30.0, 60.0, 60.0));

QGraphicsEllipseItem* pEllipseItemEx = new QGraphicsEllipseItem(QRectF(-30.0, -30.0, 60.0, 60.0));

QList<QGraphicsItem*> pItemList;

pItemList.append(pRectItemEx);

pItemList.append(pEllipseItemEx);

QGraphicsItemGroup* pItemGroupEx = pScene->createItemGroup(pItemList);

pItemGroupEx->setFlag(QGraphicsItem::ItemIsSelectable);

pItemGroupEx->setFlag(QGraphicsItem::ItemIsMovable);

 

 

图元组能够使用addToGroup将图元添加到组里,使用removeFromGroup将图元从组里移除。其余的操做就把它当成QGraphicsItem来看待。若是想要销毁组,能够使用场景类的destroyItemGroup方法便可。

添加和删除Item的操做保留了Item的场景相对位置和转换,相反,调用setParentItem(),其中仅保留子项目的父项相对位置和转换。

 

QGraphicsItem 分组比较简单,但在分组以后 group 中的 QGraphicsItem 没法捕获本身的相关事件(例如:鼠标事件、键盘事件),实际接受消息对象为 QGraphicsItemGroup。

让 QGraphicsItemGroup 中的 item 处理本身的事件

查看QGraphicsItemGroup源码

QGraphicsItemGroup::QGraphicsItemGroup(QGraphicsItem *parent)

                 : QGraphicsItem(*new QGraphicsItemGroupPrivate, parent)

{

         setHandlesChildEvents(true);

}

 

setHandlesChildEvents

This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

If enabled is true, this item is set to handle all events for all its children (i.e., all events intented for any of its children are instead sent to this item); otherwise, if enabled is false, this item will only handle its own events. The default value is false.

This property is useful for item groups; it allows one item to handle events on behalf of its children, as opposed to its children handling their events individually.

If a child item accepts hover events, its parent will receive hover move events as the cursor passes through the child, but it does not receive hover enter and hover leave events on behalf of its child.

相关文章
相关标签/搜索