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. Changing the position of QGraphicsView content
Forum Updated to NodeBB v4.3 + New Features

Changing the position of QGraphicsView content

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 185 Views
  • 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.
  • F Offline
    F Offline
    Forfunckle
    wrote on last edited by
    #1

    I am reposting this question because the original didn't get answers.

    I have the following situation: there are some QGraphicsItems in my QGraphicsScene. When I add them to the scene, they look nice and centered. Afterwards, I have a zoom system which scales up my elements individually, instead of QGraphicsView::scale() (which I do not want to use because it doesn't work for what I need). Thing is, when I zoom in, it works, but when I zoom out, it's not centering in the center of the items, but at the top left corner of the items. Here is what I mean:

    What i want

    I would like it to stay in the center. I am also already using centerOn(), but it only works when I use QGraphicsView::scale() instead of my own system. How can I make it work? Here is the code that gets executed after my zoom:

    // ...
    // scaling the elements here
    // ...
    QPointF delta = target_viewport_pos - mouse_event->pos();
    if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) {
        target_viewport_pos = mouse_event->pos();
        target_scene_pos = _view->mapToScene(mouse_event->pos());
    }
    _view->centerOn(target_scene_pos);
    QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
                                                               _view->viewport()->height() / 2.0);
    QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
    _view->centerOn(_view->mapToScene(viewport_center.toPoint()));
    
    jsulmJ 1 Reply Last reply
    0
    • F Forfunckle

      I am reposting this question because the original didn't get answers.

      I have the following situation: there are some QGraphicsItems in my QGraphicsScene. When I add them to the scene, they look nice and centered. Afterwards, I have a zoom system which scales up my elements individually, instead of QGraphicsView::scale() (which I do not want to use because it doesn't work for what I need). Thing is, when I zoom in, it works, but when I zoom out, it's not centering in the center of the items, but at the top left corner of the items. Here is what I mean:

      What i want

      I would like it to stay in the center. I am also already using centerOn(), but it only works when I use QGraphicsView::scale() instead of my own system. How can I make it work? Here is the code that gets executed after my zoom:

      // ...
      // scaling the elements here
      // ...
      QPointF delta = target_viewport_pos - mouse_event->pos();
      if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) {
          target_viewport_pos = mouse_event->pos();
          target_scene_pos = _view->mapToScene(mouse_event->pos());
      }
      _view->centerOn(target_scene_pos);
      QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
                                                                 _view->viewport()->height() / 2.0);
      QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
      _view->centerOn(_view->mapToScene(viewport_center.toPoint()));
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Forfunckle Isn't this same question as https://forum.qt.io/topic/115568/how-to-change-the-position-of-qgraphicsview-content ?
      Please do not double post!
      Closing...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      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