how to draw QGraphicsPolygonItem
-
Hi,
I am trying to draw a polygon in my graphicsScene.
The polygon exists of QPoints that are stored inside qv_points (qv = QVector)void graphicsView::addPoly() { qDebug() << "Add polygon"; QPolygonF polygon; polygon << qv_points; QBrush brush; brush.setColor(GREEN); QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem(); newPoly->setPolygon(polygon); newPoly->setBrush(QBrush(GREEN, Qt::SolidPattern)); scene->addItem(newPoly); }
It comes into the function since I can see "Add polygon" in the debugger.
Does anyone know why my polygon is not being drawn? -
hi
The debugger for visual studio is a separate download called
Debugging tools from MS.When you press Ctrl+N , you are not creating a new instance of the
graphicsView or something like that ? -
@hobbyProgrammer
I know nothing about this area, but is it right that yourYep, @mrjj says it will do that!QPolygonF polygon
goes out of scope? IssetPolygon(polygon)
going to copy it?Or, does your polygon (
qv_points
) have the right number of sensible points? -
Hi
Check the actual points as the code works fine.QPolygonF polygon; polygon << QPointF( 10, 10 ) << QPointF( 0, 90 ) << QPointF( 40, 70 ) << QPointF( 80,110 ) << QPointF( 70, 20 ); QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem(); newPoly->setPolygon(polygon); newPoly->setBrush(QBrush(Qt::green, Qt::SolidPattern)); scene->addItem(newPoly);
@JonB Good point but it will make a copy of the QPolygonF so its not out of scope issue in this case.
But spot on with the qv_points.
I think thye might be in scene coordinates or view and not local. -
@mrjj Hi thanks.
I found that it does fill the qv_points, but when it comes in the addPolygon function the qv_points is empty.I have absolutely no clue why. This is my code:
addPolygon is called by using the shortcut Ctrl+N (new polygon).void graphicsView::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton) { bool point_exists = false; for(int i = 0; i < qv_points.size(); i++) { if(qv_points.at(i).x() <= mapToScene(event->pos()).x()+2.5 && qv_points.at(i).x() >= mapToScene(event->pos()).x()-2.5 && qv_points.at(i).y() <= mapToScene(event->pos()).y()+2.5 && qv_points.at(i).y() >= mapToScene(event->pos()).y()-2.5) { point_exists = true; } } if(point_exists) { qDebug() << "point exists"; } else if(!point_exists || qv_points.isEmpty()) { addPoint(mapToScene(event->pos())); } } } void graphicsView::addPoly() { qDebug() << "Add polygon"; qDebug() << qv_points; QPolygonF polygon; polygon << qv_points; qDebug() << polygon; QBrush brush; brush.setColor(GREEN); QGraphicsPolygonItem *newPoly = new QGraphicsPolygonItem(); newPoly->setPolygon(polygon); newPoly->setBrush(QBrush(GREEN, Qt::SolidPattern)); scene->addItem(newPoly); } void graphicsView::addPoint(QPointF pos) { qv_points << pos; qDebug() << qv_points; QGraphicsEllipseItem *newEllipse = new QGraphicsEllipseItem(); double x,y; QPointF point = pos; x = point.x()-2.5; y = point.y()-2.5; newEllipse->setRect(QRectF(x,y,5,5)); newEllipse->setBrush(QBrush(Qt::red, Qt::SolidPattern)); newEllipse->setFlags(QGraphicsEllipseItem::ItemIsMovable | QGraphicsEllipseItem::ItemIsSelectable); //add item to scene scene->addItem(newEllipse); qDebug() << pos; }
this is what the debugger says:
QVector(QPointF(142,73)) QPointF(142,73) QVector(QPointF(142,73), QPointF(122,305)) QPointF(122,305) QVector(QPointF(142,73), QPointF(122,305), QPointF(289,323)) QPointF(289,323) QVector(QPointF(142,73), QPointF(122,305), QPointF(289,323), QPointF(284,69)) QPointF(284,69) Add polygon QVector() QPolygonF()
-
Hi
Its very good you use qDebug() but would be even nicer if you added some text so
its easier to follow what is what.qDebug() <<"graphicsView::addPoint - qv_points: " << qv_points;
Anyway, it looks very odd as list suddenly is empty.
It looks all is inside same class so unless you call clear on it
i see no way it suddenly goes empty.However, looking at the posted code, there is also nowhere - where you add to the qv_points list.
-
@mrjj said in how to draw QGraphicsPolygonItem:
However, looking at the posted code, there is also nowhere - where you add to the qv_points list.
the first line of addPoint() is where the point is added to the qv_points.
void graphicsView::addPoint(QPointF pos) { qv_points << pos;
-
@hobbyProgrammer
Hi
Ah i miss that as i was looking for append or similar :)Yes, just start the app in debug mode (f5)
Then place a break point.
Then press F10 to step or F11 -
This post is deleted!
-
@mrjj
I managed to see that it shows up empty in the addPolygon function, but when I add a new point it still shows the old points in the debugger.This is what I get in the debugger:
qv_points in addPoint: QVector(QPointF(27,40)) qv_points in addPoint: QVector(QPointF(27,40), QPointF(50,229)) qv_points in addPoint: QVector(QPointF(27,40), QPointF(50,229), QPointF(151,236)) qv_points in addPoint: QVector(QPointF(27,40), QPointF(50,229), QPointF(151,236), QPointF(152,147)) addPoly() qv_points: QVector() addPoly() qv_points: QVector() addPoly() qv_points: QVector() qv_points in addPoint: QVector(QPointF(27,40), QPointF(50,229), QPointF(151,236), QPointF(152,147), QPointF(245,129))
-
hi
The debugger for visual studio is a separate download called
Debugging tools from MS.When you press Ctrl+N , you are not creating a new instance of the
graphicsView or something like that ? -
@mrjj yes I did! Thank you so much.
I had this in the create actions
GraphicsView gv = new GraphicsView; addPolyAct = new QAction(tr("&Add Poly..."), this); addPolyAct->setShortcut(QKeySequence::New); connect(addPolyAct, SIGNAL(triggered()), gv , SLOT(addPoly()));
since I promoted the widget inside the designer I changed it to:
addPolyAct = new QAction(tr("&Add Poly..."), this); addPolyAct->setShortcut(QKeySequence::New); connect(addPolyAct, SIGNAL(triggered()), ui->graphicsView, SLOT(addPoly()));
Now everything works perfectly!
Thank you so much. -
Ok. Good. :)
It was my only guess
how on earth the points list could become
empty.
Sometimes Im a lucky guesser :)