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?