是qgraphicsitems实际高度和宽度与转换后的sceneboundingrect不同

时间:2015-02-07 10:00:00

标签: c++ qt

我正在编写qgraphicsitem的代码,以便在Qgraghicsscene中进行移动和变换,但是当我对其进行变换时,它的实际高度和宽度与它的sceneboundingrect不同。 为什么会这样? actuallu我必须在变换项目周围绘制轮廓,但该轮廓应该是项目的sceneboundingrect而不是它自己的。 我怎么能管理它? 请重播你的答案。 提前谢谢。

这是我的代码示例 下面的代码是qgraphicsitem的绘画函数

if(bRegionSelected)
{

    _outterborderPen.setStyle(Qt::DashLine);
    painter->setPen(_outterborderPen);
    QPointF topLeft(_drawingOrigenX, _drawingOrigenY);
    QPointF bottomRight( _drawingWidth, _drawingHeight);

    painter->setBrush(Qt::gray);
    QRectF rect2(topLeft, bottomRight);
    painter->setOpacity(0.6);
    painter->drawRect(rect2);
    painter->setOpacity(1);
    painter->setBrush(QBrush(Qt::black, Qt::SolidPattern));
    painter->setPen(QPen(Qt::black));

    painter->drawRect(_drawingOrigenX, _drawingOrigenY, nHandlerWidth, nHandlerWidth);
    painter->drawRect(_drawingWidth-nHandlerWidth-2, _drawingOrigenY, nHandlerWidth, nHandlerWidth);
    painter->drawRect(_drawingOrigenX, _drawingHeight-nHandlerWidth-2, nHandlerWidth, nHandlerWidth);
    painter->drawRect(_drawingWidth-nHandlerWidth-2, _drawingHeight-nHandlerWidth-2, nHandlerWidth, nHandlerWidth);

    qDebug() << "here in paint :: " << sceneBoundingRect() << "and" << _drawingWidth << _drawingHeight;

    if(transform().isRotating())
    {
        qDebug() << "here in paint for ratating :::: " << _drawingHeight-sceneBoundingRect().height();
        _outterborderPen.setStyle(Qt::DashLine);
        painter->setPen(_outterborderPen);
        QPointF topLeft1(int(0), int(_drawingHeight-sceneBoundingRect().height()));
        QPointF bottomRight1(_drawingWidth, sceneBoundingRect().height());
        QRectF rect21(topLeft1, bottomRight1);
        painter->setOpacity(0.2);
        painter->drawRect(rect21);
    }
}
else
{
    painter->setPen(Qt::black);
    QPointF topLeft(_drawingOrigenX, _drawingOrigenX);
    QPointF bottomRight(_drawingWidth, _drawingHeight);
    QRectF rect2(topLeft, bottomRight);
    painter->setOpacity(1);
    painter->drawRect(rect2);
}

}

1 个答案:

答案 0 :(得分:0)

我猜你是在QGraphicsItem的子类中实现这个函数。如果您想获得项目周围的矩形,我认为您需要使用boundingRect()而不是sceneBoundingRect()

sceneBoundingRect()总是会更改,例如当项目旋转时,但boundingRect()保持不变。