-1

I'm drawing a QGraphicsPolygonItem like this:

enter image description here

I need to have the dimensions information on the scene, like this:

enter image description here

I'm using QGraphicsPolygonItem, QGraphicsScene and QGraphicsView.

Mahmoud Hassan
  • 598
  • 1
  • 3
  • 15
  • A `GraphicsItem` can have a parent such as a `Qwidget`. So, you can add your `QGraphicsPolygonItem` as a child of another `GraphicsItem` that will draw the information. – Dimitry Ernot Dec 03 '13 at 09:47
  • @Romha `GraphicsItem` can have a parent, but it can't have a `QWidget` as its parent. Otherwise you are correct. – thuga Dec 03 '13 at 10:16
  • @thuga, technically a QGraphicsItem could have a parent of a QWidget, if the QWidget is wrapped in a QGraphicsProxyWidget. Of-course you could argue that the proxy widget is actually its parent, but the desired effect would be the same. – TheDarkKnight Dec 03 '13 at 10:21
  • I would suggest that you derive your own class from QGraphicsTextItem to add the dimension text. – Abhishek Bansal Dec 03 '13 at 10:28
  • @thuga, Sorry, I have a poor english... I mean we can set a parent to a `QGraphicsItem` as we would with a `QWidget`... – Dimitry Ernot Dec 03 '13 at 10:29
  • @Merlin069 True, but I'm not talking about the effect, I'm talking about the constructor/`QGraphicsItem::setParentItem` not accepting a `QWidget` as the parent. – thuga Dec 03 '13 at 10:55

1 Answers1

1

There are several ways to achieve what is being asked here.

Personally, I'd opt for not using a QGraphicsPolygonItem, but creating a class derived from QGraphicsItem. This class would store a QPolygonF to store the required points. Then you'd overload the following functions:-

  • boundingRect
  • shape
  • paint

In the paint function the class draws the polygon and all the dimensions with it.

Alternatively, you could create separate QGraphicsItem classes for just the dimensions, set their parent as the QGraphicsPolygonItem and then set their positions, but this can get a bit messy.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • ok, I've already re-implemented QGraphicsPolygonItem and overloaded paint method, the issue I'm asking for is how to draw the dimensions, there are two challenging issues, 1- how to choose the font size (so it is the small even if the user zoom in the scene). 2- How to draw the arrows. – Mahmoud Hassan Dec 08 '13 at 08:50
  • 1
    If you want to draw the text, lines and arrows and ensure they don't scale when the user zooms, you need to put them in a separate object derived from QGraphicsItem, set the flag QGraphicsItem::ItemIgnoresTransformations and then add them as a child objects to your polygon object. – TheDarkKnight Dec 09 '13 at 10:15
  • 1
    Any idea about how to draw arrows? – Mahmoud Hassan Dec 17 '13 at 14:38
  • Either draw each section in the paint function with painter->drawLine, or load an image into QPixmap. Personally, I'd use the former. – TheDarkKnight Dec 17 '13 at 15:50