How to change center of scene/view?
-
Hi all,
I have created a QGraphicsView and added a scene for that, Initially I added four items in the scene. I printout the center of scene in this way.
QRectF sceneRect = _view->sceneRect(); QPointF center = sceneRect.center(); qDebug()<< "After resetting Center: " << center;
Output
After resetting Center: QPointF(263.5,129)The center value that is coming looks to good for me.
Now I am clicking on third item and on click I am adding a new item, So now total item in scene become 5. Now the center of scene changed due to addition of new Item.
And new center is coming at QPointF(293.5,131.5)Now my requirement is that the location of all the 4 item should be at same position as before click.
I am trying to change the center of scene to previous location QPointF(263.5,129) using
_view->centerOn(QPointF(263.5,129)); _view->update();
But the above code not changing the center of scene.
Could you please point out what I am doing wrong?Or if you guys have any other solution for changing the center of scene please tell me. that would be very helpful.
-
@Anil-Kumar-Saini said in How to change center of scene/view?:
Now my requirement is that the location of all the 4 item should be at same position as before click.
Adding items to a scene does not move existing items in the scene. The original four items have the same position after adding a fifth.
The portion of the scene displayed in the view's viewport is also unchanged by adding items, i.e. the original items remain exactly where they were on screen but you may get scroll bars.This is demonstrated
#include <QApplication> #include <QGraphicsScene> #include <QGraphicsRectItem> #include <QGraphicsView> #include <QTimer> void addItem(QGraphicsScene *scene, int i) { QGraphicsRectItem *item = new QGraphicsRectItem(i*50, i*50, 50, 50, nullptr); item->setBrush(Qt::cyan); scene->addItem(item); } int main(int argc, char **argv) { QApplication app(argc, argv); QGraphicsScene scene; for (int i = 0; i < 4; ++i) { addItem(&scene, i); } # Add a fifth item after 3 seconds QTimer::singleShot(3000, [&scene]() { addItem(&scene, 5); } ); QGraphicsView view(&scene); view.show(); return app.exec(); }
Four items:
After adding a fifth:
Perhaps you can explain what the requirement actually is? -
@ChrisW67 You can see in this image I have four items and I calculated center of scene as A and clicked point as B
After clicking on point B A new item added and that cause shifting of point B towards left .
Here what I want is that I want to match Poinit A with point C or Point B with point D by shifting the scene.. Here I am trying to set point A as center of scene so that it will move item toward right and then point B will shift on point D.
@Qt-embedded-developer @qt_learning @qtgraphical @develop-world