QGraphicsView moves when add item
-
I am having a problem to figured out what's wrong with my implementation of adding items to the scene/view. The items should be added at the mouse position, however looks like the QGraphicsView moves and the items are appear in different location's.
I reimplemented the mousePressEvent of QGraphicsScene to add my items in the mouse clicked position:
@
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent mouseEvent)
{
GraphicsBacia bacia;// my derived item from QGraphcsItem
....
case (ModoEdicao::InserirBacia):
//
primeiroItemClicado = false;
bacia = new GraphicsBacia(parametros);
addItem(bacia);
bacia->setPos(mouseEvent->scenePos());
break;
...QGraphicsScene::mousePressEvent(mouseEvent);
@My view is a QGraphicsView, so i think that the problem must be in the scene implementation:
@
void MainWindow::criarWidgets()//Create the QGraphicsView
{
parametros = new ParametrosControleWidget;GraphicsScene* scene = new GraphicsScene(parametros); ui->graphicsView->setScene(scene);
-
I just added setSceneRect to QGraphicsScene constructor and the problem was solved. I think that's because I didn't set the scene rectangle the QGraphicsView automatically try to fit all item's in the current view and the items don't appear at the clicked location. From Qt:
"If the scene rect is unset, QGraphicsScene will use the bounding area of all items, as returned by itemsBoundingRect()..."
There is any problem if my scene rectangle is smaller or bigger than the QGraphicsView ?