我有一个自定义QGraphicsItemGroup,带有一组固定的子项。我需要使它可以调整大小(已经完成),但是,在调整大小时,boundingRect不会更新 - 但是,所有子项都是正确绘制的。我已经尝试在程序的不同位置添加prepareGeometryChange()和update(),但这没有帮助。
即使这个实现返回默认的boundingRect:
QRectF FigureGraphicsItemGroup::boundingRect() const
{
return childrenBoundingRect().adjusted(0, 0, 50, 0);
}
和一些必要的代码(NB没有自定义绘画):
m_right:正确控制调整大小。
QRectF FigureGraphicsItemGroup::boundingRect() const
{
return childrenBoundingRect();
}
void FigureGraphicsItemGroup::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
QPointF pos = event->pos();
if (m_right->contains(pos) && m_right->isVisible())
{
m_isResized = true;
}
else
QGraphicsItemGroup::mousePressEvent(event);
}
void FigureGraphicsItemGroup::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPointF pos = event->pos();
if (m_isResized)
{
if (m_previous.x() == -1 && m_previous.y() == -1)
m_previous = pos;
m_delta = pos.x() - m_previous.x();
m_previous = pos;
QGraphicsItem::prepareGeometryChange();
recalculatePositions();
}
else
{
QGraphicsItemGroup::mouseMoveEvent(event);
}
}
void FigureGraphicsItemGroup::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
QPointF pos = event->pos();
if (m_right->contains(pos) && m_right->isVisible())
{
setCursor(Qt::SizeHorCursor);
}
else
{
setCursor(Qt::ArrowCursor);
QGraphicsItemGroup::hoverMoveEvent(event);
}
}
void FigureGraphicsItemGroup::reset()
{
m_isResized = false;
m_delta = 0;
m_previous = QPointF(-1 , -1);
}
void FigureGraphicsItemGroup::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event)
reset();
QGraphicsItemGroup::mouseReleaseEvent(event);
}
QVariant FigureGraphicsItemGroup::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change)
{
case ItemSelectedHasChanged:
m_isSelected = isSelected();
m_right->setVisible(m_isSelected);
break;
}
return QGraphicsItem::itemChange(change, value);
}
void FigureGraphicsItemGroup::recalculatePositions()
{
int sumX = 1;
QFontMetrics fm(m_zoneNumberSimpleTextItem->font());
m_routeNumberSimpleTextItem->setPos(sumX, m_middle - fm.height());
sumX += m_routeNumberSimpleTextItem->boundingRect().width() * SPACING;
//-------------------------------------------------------------------------------------------
int diameter = fm.height() * SPACING;
int pixLength = fm.width(m_zoneNumberSimpleTextItem->text());
if (pixLength > diameter)
diameter = pixLength;
QPen pen(Qt::red);
pen.setWidth(1);
m_zoneCircleEllipseItem->setRect(sumX, m_middle - diameter, diameter, diameter);
m_zoneCircleEllipseItem->setPen(pen);
m_zoneNumberSimpleTextItem->setPos(sumX + fm.width(m_zoneNumberSimpleTextItem->text()) / 2,
m_middle - fm.height());
sumX += m_zoneCircleEllipseItem->boundingRect().width() * SPACING;
//-------------------------------------------------------------------------------------------
m_fuelSimpleTextItem->setPos(sumX, m_middle * 1.1);
//-------------------------------------------------------------------------------------------
m_leftCornerSvgItem->setPos(sumX, m_middle - m_leftCornerSvgItem->boundingRect().height() / 4);
sumX += m_leftCornerSvgItem->boundingRect().width() * m_scale;
//-------------------------------------------------------------------------------------------
pen.setColor(m_blue);
pen.setWidth(4);
m_leftLineItem->setPen(pen);
int length = m_leftLineItem->line().length() + m_delta / 2;
length = length < 0 ? 0 : length;
m_leftLineItem->setLine(sumX, m_middle, sumX + length, m_middle);
sumX += m_leftLineItem->boundingRect().width();
//-------------------------------------------------------------------------------------------
m_exerciseSvgItem->setPos(sumX - 2, m_middle - m_exerciseSvgItem->boundingRect().height() / 4);
sumX += m_exerciseSvgItem->boundingRect().width() * m_scale;
//-------------------------------------------------------------------------------------------
pen.setColor(m_blue);
pen.setWidth(4);
m_rightLineItem->setPen(pen);
length = m_rightLineItem->line().length() + m_delta / 2;
length = length < 0 ? 0 : length;
m_rightLineItem->setLine(sumX , m_middle, sumX + length, m_middle);
sumX += m_rightLineItem->boundingRect().width();
//-------------------------------------------------------------------------------------------
m_rightCornerSvgItem->setPos(sumX - 2, m_middle - m_rightCornerSvgItem->boundingRect().height() / 4);
sumX += m_rightCornerSvgItem->boundingRect().width() * m_scale;
//-------------------------------------------------------------------------------------------
m_right->setRect(sumX, m_middle, 4, 4);
//-------------------------------------------------------------------------------------------
}
UPD 一点点黑客帮助了我:只需添加removeFromGroup&amp;调整大小期间某些项的addToGroup - 并且将正确绘制boundingRect。但它看起来有点奇怪。