0

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.

Alarming
  • 137
  • 1
  • 3
  • 14
  • What exactly is the intended relationship between polygon and rectangle? Do you want to center the polygon inside the rectangle? Scale it so its contained? I think you got the logic the wrong way: boundingRect() *reports* to QGraphicsView where paint() will paint the item, not the other way around: boundingRect doesn’t affect the painting itself. Also don’t create QBrush on the heap, it will leak - create it on the stack instead. – Frank Osterfeld Dec 13 '15 at 20:10
  • @FrankOsterfeld The polygon is supposed to fill the rectangle. I want to create a polygon as a separate class to the QGraphicsView, if a boundingRect is not needed then ok, but I don't know. When I paint the polygon it comes out on the GraphicsView and I am unable to move it - even though it set the ItemIsMovable (i've added this to the main question above). Also, how would you create a QBrush on stack correctly - every time I tried I was unable to set the colour using QColor, only this method worked. Thanks :D – Alarming Dec 13 '15 at 20:26
  • NVM about the QBrush I worked it out, but the rest I still can't do :( – Alarming Dec 13 '15 at 20:37
  • Does `painter->translate(780,425);` solve your problem? This is rather unintuitive though IMO - I’d rather let the bounding rect start at (0,0) and move the resulting item to (780,425) instead. – Frank Osterfeld Dec 13 '15 at 20:43
  • @FrankOsterfeld Yes it does! Thanks. When I changed the boundingRect's position to `QRectF(0,0,70,30);` it didn't work (wasn't movable) but keeping it at `QRectF(780,425,70,30);` it did. Thanks :D You should post that as an answer. – Alarming Dec 13 '15 at 21:15

1 Answers1

0

As @FrankOsterfeld said:

painter->translate(780,425);

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

Alarming
  • 137
  • 1
  • 3
  • 14