Hello!

I want the center of the scene to remain in the center when scaling; we do the following:

//масштабируем так чтобы центр сцены оставался в центре //определяем точку центра viewporta QPoint Center(0.5 * this->width(), 0.5 * this->height()); //переводим в масштаб сцены QPointF SCenter = mapToScene(Center); //определяем точку по объекту PixMapItem SCenter = GrapthPixMapItem.mapFromScene(SCenter); //масштабируем объект GrapthPixMapItem.setScale(1.20); //отображаем всю сцену this->setSceneRect(MainGraphicsScene.itemsBoundingRect()); //получаем координаты центра масштабирования в масштабе сцены SCenter = GrapthPixMapItem.mapToScene(SCenter); 

now it seemed to be left to do

 this->ceneterOn(SCenter); 

and all will be well. But no, the center with this action floats. In the help it is written that the scrollbars are int, and the center of the float seems to be inaccurate. However, if we continue instead of this-> ceneterOn (SCenter), continue the code like this:

 //получаем координаты цетра масштабирования в масштабе viewport Center = mapFromScene(SCenter); //считаем дельту сдвига Center.setX(Center.x() - 0.5 * this->width()); Center.setY(Center.y() - 0.5 * this->height()); //сдвигаем скролбары так чтобы точка масштабирования стала в центре this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() + Center.x()); this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() + Center.y()); 

The center stands quite well and floats just a little bit between the zoom steps, around one point. I'm doing something wrong with centerOn. Or is it such a fate and you just have to do everything by hand?


I still don’t understand something ... If you set resizeAnchor to the viewport center and rotate the scale, everything happens again around a certain center that coincides with the center that gave centerOn (), but does not coincide with the real viewport center. What am I doing wrong?


Well, everything turned out to be simple, the zoom points are set naturally for the scene, not for the objects. And working on them you need to work with the scale of the scene. The important point is to set not only resizeAnchor, but also TransformationAnchor. Then specifying the center through centerOn (...) and changing the scale of the scene, the selected point goes to the center of the scene if it can, and remains visible if the scene cannot be moved. Everything works correctly

  • Not very clear, you scale the element itself for what? Scale with the mouse (wheel)? Can it be better to directly view the scene? - alexis031182
  • I scale on everyone and the wheel, and the slider, and the buttons. Could be better and twist the scene, I have not thought about this kind of magic. I went straight, I needed the object to become bigger, I increased it .... But it seems I understood what was the matter, all these anchors and so on are about the scene, not about objects, the latter live in their own space ... - Andrey Golikov

1 answer 1

Try something like this:

 #include <math.h> #include <QtGui/QWheelEvent> #include <QtWidgets/QGraphicsView> class GraphicsView : public QGraphicsView { Q_OBJECT public: explicit AGraphicsView(QWidget *parent = Q_NULLPTR) { setTransformationAnchor(QGraphicsView::AnchorViewCenter); setScene(new QGraphicsScene(this)); } virtual ~AGraphicsView() {} protected: virtual void wheelEvent(QWheelEvent *event) { const qreal factor = std::pow(1.2,event->angleDelta().y()/240.0); scale(factor, factor); } }; 
  • Yes thank you. I have already rewritten to scale through the scene. It became clear how centerOn () works and how the scale works relative to the center. At first, I wrote about the same with my hands, but it turns out that everything can happen by itself :) ... It remains unclear with the resizer on the cursor. On a small scale, it works well, but then it starts to twitch wildly, so that the point flies away. What are the conditions of work on the cursor? - Andrey Golikov
  • "With a cursor resize" - is this, in a sense, a selection or a mouse wheel? - alexis031182
  • when the wheel turns the scale, and the anchor is selected UnderMouse. There, when the scene is enlarged, the point you point to is very jerking. That is, on a small scale, everything works as you like, increases around the point that the cursor points to, and then after 1000% it starts to sausage, sometimes even the point flies away from the screen. I think this is already some kind of rounding problem, the scale is too big ... - Andrey Golikov
  • Probably the scaling factor should vary depending on, in fact, scale. That is, if we increase, then we do it with a smaller factor . Probably so. Unfortunately, did not try. - alexis031182