-2

I have an old c++ program that I need to bring back to life. My task now is to draw a single Polygon. I am stuck at the very beginning since I have no experience in graphics. The points I want to use to draw a Polygen are stored in

QVector < QPointF> points which looks like that:

enter image description here

Now I want to draw my Polygon. I use this code:

            ggScene = new QGraphicsScene();   
            QPolygonF shape(points);
            QGraphicsItem* gg = ggScene->addPolygon(shape);

When compiling I get the following error:

cannot convert QGraphicsPolygonItem* to QGraphiscItem in initialization

Can anybiody help me on this please, thank you.

user3443063
  • 1,455
  • 4
  • 23
  • 37
  • 3
    are you sure this is the correct error message and code? I would expect that error for eg `QGraphicsItem gg = ggScene->addPolygon(shape);`. Can you provide a [mcve]? – 463035818_is_not_an_ai Dec 07 '18 at 11:14
  • I also get Cannot convert QGraphicsPolygonItem* to QGraphicsItem in initialization – user3443063 Dec 07 '18 at 11:36
  • "also" ? Only you get the error and only you know what is the code, to get help you need to provide a [mcve]. `QGraphicsPolygonItem` inherits from `QAbstractGraphicsShapeItem` which inherits from `QGraphicsItem`, hence your code should be fine, but the error is about converting a `QGraphicsPolygonItem*` (pointer) to a `QGraphicsItem` (object) and there is no such conversion in the code you show – 463035818_is_not_an_ai Dec 07 '18 at 12:14
  • You should include , it seems that QGraphicsItem is forward declared somewhere but not known by the compiler at the line of assignment. – tunglt Dec 07 '18 at 13:44

1 Answers1

0

I'm going to go out on a limb and guess that the actual error message is...

cannot convert ‘QGraphicsPolygonItem*’ to ‘QGraphicsItem*’ in initialization

(Note QGraphicsItem* rather than just QGraphicsItem)

If that's the case then the problem is probably that the compiler is only seeing a forward declaration of QGraphicsPolygonItem. Hence it is not aware that QGraphicsPolygonItem inherits from QGraphicsItem and so cannot perform the implicit conversion from QGraphicsPolygonItem* to QGraphicsItem*.

The fix is probably the simple addition of...

#include <QGraphicsPolygonItem>
G.M.
  • 12,232
  • 2
  • 15
  • 18