c++qtqgraphicsitemqpolygon

Qt QGraphicsItem drawing a polygon within the boundingRect() of separate class?


I've created a GraphicsItem in a new class and have painted a polygon, however, instead of it being drawn inside the boundingRect() for the class it is drawing on the main GraphicsView at coordinates I hoped it would be drawn inside the 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);

}

The last two lines when not commented out would fill the boundingRect() with a rectangle, and I am able to pass to it the ItemBoundary varibale unlike with the polygon above.

How could I pass the ItemBoundary (=BoundingRect()) to the polygon too?

Edit: Essentially I would like to draw a polygon, which can be moved and as a separate class, to send to the QGraphicsView in my main user interface.


Solution

  • As @FrankOsterfeld said:

    painter->translate(780,425);

    which moved the item into the region in which the boundingRect() lies.