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