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

How to change the position of QGraphicsView content?

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

    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()));
    
    A 1 Reply Last reply
    0
    • F Forfunckle

      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()));
      
      A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      @Forfunckle
      What is "your own system"? Without understanding that, we will not be able to give you answers.

      My guess is that your boundingRect is wrong after using your scaling system.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Forfunckle
        wrote on last edited by Forfunckle
        #3

        @Asperamanca
        You're right, I forgot to say. Sorry!

        I'm just scaling the elements like that:

        for (int y = 0; y < canvasSize[0]; i++)
        {
            for (int x = 0; x < canvasSize[1]; j++)
            {
                pixels[y][x]->setRect(y*zoomLevel, x*zoomLevel, zoomLevel, zoomLevel);
            }
        }
        

        pixels is a matrix of rect items. I am changing their rects directly.

        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