Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsView moves when add item
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView moves when add item

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thiberi0
    wrote on last edited by
    #1

    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);
    

    }
    @
    http://postimg.org/gallery/707kl4rq/becc4e7d/

    thiberi0

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thiberi0
      wrote on last edited by
      #2

      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 ?

      thiberi0

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved