重载了QGraphicsItemGroup的两个函数总算搞定了,就差最后一个问题了,就是需要刷新一下,这个问题是老问题了,调用update不管用,目的是触发那个重载的boundingRect()函数。
// 显示区域
QPainterPath GraphicsLineItemGroup::shape() const
{
QPainterPathStroker stroker;
QList<QGraphicsItem *> phead = childItems();
int i, num;
QPainterPath lpath;
num = phead.count();
stroker.setWidth(4.0);
for (i=0; i<num; i++)
{
lpath.addPath(phead->shape());
}
return stroker.createStroke(lpath);
}
// 可选择的区域
QRectF GraphicsLineItemGroup::boundingRect() const
{
QList<QGraphicsItem *> phead = childItems();
int i, num, ix1, ix2, iy1, iy2;
num = phead.count();
if (num==0)
{
return QGraphicsItemGroup::boundingRect();
}
ix1 = phead[0]->boundingRect().left();
iy1 = phead[0]->boundingRect().top();
ix2 = phead[0]->boundingRect().right();
iy2 = phead[0]->boundingRect().bottom();
for (i=0; i<num; i++)
{
if (phead->boundingRect().left()<ix1)
{
ix1 = phead->boundingRect().left();
}
if (phead->boundingRect().top()<iy1)
{
iy1 = phead->boundingRect().top();
}
if (phead->boundingRect().right()>ix2)
{
ix2 = phead->boundingRect().right();
}
if (phead->boundingRect().bottom()>iy2)
{
iy2 = phead->boundingRect().bottom();
}
}
QPointF p(3,3);
return QRectF(QPointF(ix1, iy1)-p, QPointF(ix2, iy2)+p);
}
有什么办法吗??