How to draw in the scene of the QGraphicsView?
I have an application in which the user can draw some points with the mouse and I used QGraphicsView to do that.
I initialized a scene within the QGraphicsView:
scene = new QGraphicsScene(this); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setSceneRect(0, 0, 850, 480); setScene(scene); setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::Antialiasing); setTransformationAnchor(AnchorUnderMouse); scale(qreal(1.0), qreal(1.0)); setMinimumSize(400, 400);
This scene doesn't cover the entire QGraphicsView and I want the user to be able to draw the points only on the scene. Also the coordinates of the points should be that from the scene not the QGraphicsView area.
This is one screenshot! of how it looks.
I tried doing like this:
QPoint p = event->pos(); QRectF sceneRect = this->sceneRect(); if ((p.x() > sceneRect.left())&&(p.x() < sceneRect.right())&&(p.y() > sceneRect.top())&& (p.y() < sceneRect.bottom())){ QMessageBox msg; msg.setText("point is: " + QString::number(p.x()) + ", " + QString::number(p.y())); msg.exec(); }
where I 开发者_如何学运维am testing the coordinates. But it doesn't return the correct results. How can I constrain the user to draw only on the scene?
Got it. The coordinates of the points created are from the QGraphicsView and not the scene so when putting the constraints on the points to be within the scene it will not work.
Have to map the points to the scene:
QPointF p = mapToScene(event->pos());
精彩评论