QGraphicsView 框架学习(二),组合与撤销组合

 QGraphicsItemGroup 是Qt二维图形框架提供的对图元进行组合操作的类。它提供了了void addToGroup(QGraphicsItem * item) 和 void removeFromGroup(QGraphicsItem *);两个方法。实际使用中可以通过 scene的createItemGroup()方法来创建组合对象,通过destroyItemGroup方法撤销组合。看上去就这么简单,但是事情没这么简单。如果组合对象中的图元都是简单的单体对象,那么情况的确和上面说的一样很简单,如果组合对象中包含组合对象,情况就变的复杂了。

      在一个交互式绘图程序中,对任意图形对象的组合与撤销组合都是不可缺少的操作。QGraphicsView框架中,提供了通过鼠标框选对象的功能,如果我们框选一个组合对象,你回发现,组合对象中的子对象也被包括在selectedItems当中,如果我们这个时候需要再次组合对象,就出现问题了,新的组合对象会把被包含的组合对象的子对象再次组合一次,结果就是被组合的组合对象被破坏了,实际上只剩下一个boundingRect了。没办法,找不到解决的方法,好像用它的人很少,我只好用一个笨办法解决了,那就是重写 scene的createItemGroup()方法。在向组合对象添加子对象的时候判断一下对象的父对象是不是一个组合,QGraphicsItem 提供了一个group()方法,说是可以通过它判断对象是否被组合,实际情况是没用。所以只能用 qgraphicsitem_cast方法判断了。代码如下:

[cpp]  view plain  copy
  1. GraphicsItemGroup *DrawScene::createGroup(const QList<QGraphicsItem *> &items,bool isAdd)  
  2. {  
  3.     // Build a list of the first item's ancestors  
  4.     QList<QGraphicsItem *> ancestors;  
  5.     int n = 0;  
  6.     if (!items.isEmpty()) {  
  7.         QGraphicsItem *parent = items.at(n++);  
  8.         while ((parent = parent->parentItem()))  
  9.             ancestors.append(parent);  
  10.     }  
  11.   
  12.     // Find the common ancestor for all items  
  13.     QGraphicsItem *commonAncestor = 0;  
  14.     if (!ancestors.isEmpty()) {  
  15.         while (n < items.size()) {  
  16.             int commonIndex = -1;  
  17.             QGraphicsItem *parent = items.at(n++);  
  18.             do {  
  19.                 int index = ancestors.indexOf(parent, qMax(0, commonIndex));  
  20.                 if (index != -1) {  
  21.                     commonIndex = index;  
  22.                     break;  
  23.                 }  
  24.             } while ((parent = parent->parentItem()));  
  25.   
  26.             if (commonIndex == -1) {  
  27.                 commonAncestor = 0;  
  28.                 break;  
  29.             }  
  30.   
  31.             commonAncestor = ancestors.at(commonIndex);  
  32.         }  
  33.     }  
  34.   
  35.     // Create a new group at that level  
  36.     GraphicsItemGroup *group = new GraphicsItemGroup(commonAncestor);  
  37.     if (!commonAncestor && isAdd )  
  38.         addItem(group);  
  39.     foreach (QGraphicsItem *item, items){  
  40.         item->setSelected(false);  
  41.         QGraphicsItemGroup *g = dynamic_cast<QGraphicsItemGroup*>(item->parentItem()); //判断父对象是否为组合对象。  
  42.         if ( !g )  
  43.              group->addToGroup(item);  
  44.     }  
  45.     return group;  
  46. }  

撤销组合

[cpp]  view plain  copy
  1. void DrawScene::destroyGroup(QGraphicsItemGroup *group)  
  2. {  
  3.     group->setSelected(false);  
  4.     foreach (QGraphicsItem *item, group->childItems()){  
  5.         item->setSelected(true);  
  6.         group->removeFromGroup(item);  
  7.     }  
  8.     removeItem(group);  
  9.     delete group;  
  10. }  



http://blog.csdn.net/firebolt2002/article/details/46761333

猜你喜欢

转载自blog.csdn.net/wishfly/article/details/79355652