Qt QGraphicsItem在单独的类的boundingRect()中绘制多边形?

时间:2015-12-13 18:43:42

标签: c++ qt qgraphicsitem qpolygon

我在一个新类中创建了一个GraphicsItem并绘制了一个多边形,但是,它不是在boundingRect()内绘制的,而是绘制在坐标的主GraphicsView上我希望它会被绘制在里面boundingRect()。

Detector::Detector()
{
    Pressed = false; //Initally the pressed boolean is false, it is not pressed
    setFlag(ItemIsMovable);
}    

QRectF Detector::boundingRect() const
{
    return QRectF(780,425,70,40);
}

void Detector::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF ItemBoundary = boundingRect();
    QBrush *fillBrush = new QBrush(QColor(83,71,65));


    QPolygon DetectorPolygon;
    DetectorPolygon << QPoint(0,0);
    DetectorPolygon << QPoint(20,10);
    DetectorPolygon << QPoint(70,10);
    DetectorPolygon << QPoint(70,20);
    DetectorPolygon << QPoint(20,20);
    DetectorPolygon << QPoint(0,40);

    QPen borderPen;
    borderPen.setWidth(2);
    borderPen.setColor(QColor(152,133,117));

    painter->setBrush(*fillBrush);
    painter->setPen(borderPen);
    painter->drawPolygon(DetectorPolygon);


//    painter->fillRect(ItemBoundary,*fillBrush);
//    painter->drawRect(ItemBoundary);

}

未注释掉的最后两行将使用矩形填充boundingRect(),并且我能够将ItemBoundary varibale传递给它,与上面的多边形不同。

我怎样才能将ItemBoundary(= BoundingRect())传递给多边形?

编辑:基本上我想绘制一个多边形,可以将其移动并作为一个单独的类发送到我的主用户界面中的QGraphicsView。

1 个答案:

答案 0 :(得分:0)

正如@FrankOsterfeld所说:

  

painter->translate(780,425);

将项目移动到boundingRect()所在的区域。