Qt QGraphicsItem跳转到场景矩形的外部

Qt QGraphicsItem跳转到场景矩形的外部,qt,Qt,我正在写一个基本的项目。但有时在移动或按下时,它会跳到场景矩形的外部。我正在使用OpenSuse 12.2+Qt4.8.1。代码非常简单。但我找不到问题所在 这是我的密码: TrackLabelItem::PpiTrackLabelItem(PpiTrackItem *pParent, QGraphicsScene *pScene, QGraphicsLineItem *pCombLine) : QGraphicsItem(0, pScene), mpParent(pParent), m

我正在写一个基本的项目。但有时在移动或按下时,它会跳到场景矩形的外部。我正在使用OpenSuse 12.2+Qt4.8.1。代码非常简单。但我找不到问题所在

这是我的密码:

TrackLabelItem::PpiTrackLabelItem(PpiTrackItem *pParent, QGraphicsScene *pScene, QGraphicsLineItem *pCombLine)
    : QGraphicsItem(0, pScene), mpParent(pParent), mpCombineLine(pCombLine), mBoundingRect(QRectF(0, 0, 45, 15))
{

    setZValue(TRACK_Z_VALUE);   
    setFlags( ItemIsMovable | ItemSendsGeometryChanges | ItemIgnoresTransformations);
    setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}

TrackLabelItem::~TrackLabelItem()
{

}

QRectF TrackLabelItem::boundingRect() const
{
    return mBoundingRect;
}

void TrackLabelItem::paint(QPainter *pPainter, const QStyleOptionGraphicsItem *pOption, QWidget *pWidget)
{

    pPainter->setRenderHint(QPainter::Antialiasing);
    pPainter->setPen(QPen(Qt::white, /*0.3*/1, Qt::DotLine, Qt::RoundCap, Qt::RoundJoin));
    pPainter->drawRect(mBoundingRect);
    pPainter->setFont(MAP_FONT);
    pPainter->setPen(QPen(Qt::white));
    pPainter->drawText(2, 1, mBoundingRect.width()-2, mBoundingRect.height()-1, Qt::AlignHCenter | Qt::AlignVCenter, mText);

}//paint

void TrackLabelItem::mouseMoveEvent(QGraphicsSceneMouseEvent * pEvent)
{
    update();
    QGraphicsItem::mouseMoveEvent(pEvent);
}

QVariant TrackLabelItem::itemChange(GraphicsItemChange pChange, const QVariant &pValue)
{
    if (pChange == ItemPositionChange && scene())
    {
        // value is the new position.
        QPointF newPos = pValue.toPointF();
        QRectF rect = scene()->sceneRect();
        if (!rect.contains(newPos))
        {
            double rectBottom = rect.bottom() + MAP_SCENE_DIFF;
            // Keep the item inside the scene rect.
            double posX = qMin(rect.right(), qMax(newPos.x(), rect.left()));
            double posY = qMin(rectBottom, qMax(newPos.y(), rect.top()));
            newPos = QPointF(posX, posY);

            return newPos;
        }
    }
    else if(pChange == ItemPositionHasChanged)
    {
        QPointF endPoint(pValue.toPointF().x(), pValue.toPointF().y());
        QPointF startPoint = mpParent->pos();
        mpCombineLine->setLine(QLineF(startPoint, endPoint));

        return QPointF(pValue.toPointF().x(), pValue.toPointF().y());
    }

    return QGraphicsItem::itemChange(pChange, pValue);
}//itemChange

如果设置可移动标志,QGraphicsRectItem会发生什么情况?