QT Graphics-View圖元組使用


通過把一個item作為另一個item的孩子,你可以得到item組的大多數本質特性:這些items會一起移動,所有變換會從父到子傳遞。QGraphicsItem也可以為它的孩子處理所有的事件,這樣就允許以父親代表它所有的孩子,可以有效地把所有的items看作一個整體。

另外,QGraphicsItemGroup是一個特殊的item,它既對孩子事件進行處理又有一個接口把items從一個組中增加和刪除。把一個item加到 QGraphicsItemGroup仍會保留item的原始位置與變換,而給一個item重新指定父item則會讓item根據其新的父親重新定位。可以用QGraphicsScene::createItemGroup()建組。

 

1、通過父子關系-如果想要將 items 存儲在其他 item 內,可以直接將任何 QGraphicsItem 通過為 setParentItem() 傳遞一個合適的 parent。

注意: 對於該方式,QGraphicsItem 可以有自己的子 item 對象。但是,QGraphicsItem 沒有 API(例如:setItems()、addChild())添加孩子,它只能允許孩子附加到 parent (setParentItem()),想想也挺神奇的。

 // Item parent-children

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

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

pEllipseItemTmp->setParentItem(pRectItemTmp);

pScene->addItem(pRectItemTmp);

pRectItemTmp->setFlag(QGraphicsItem::ItemIsSelectable);

pRectItemTmp->setFlag(QGraphicsItem::ItemIsMovable);

 

2、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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM