开发者

How to anchor QGraphicsView to a special point on a scene?

How to anchor QGraphicsView to a special point on a scene?

I want a center of a view is anchored to the scene point (0,0).

But as said in docummentation:

If the whole scene is visible in the view, (i.e., there are no visible scroll bars,) the view's alignment will decide where the scene will be rendered in the 开发者_StackOverflow中文版view.

And if I set agnment to Qt::AlignCenter view becomes anchored to the scene center.

Is it possibe to do?

I need something like QGraphicsView::centerOn that always put the point in to the view center.


You can anchor a QGraphicsView at a specific position by defining ("forcing") its sceneRect property, other than the default one (i.e. QGraphicsScene bounding rectangle).

http://qt-project.org/doc/qt-4.8/qgraphicsview.html#sceneRect-prop

Here is a code example. The view is centered at point(0,0), no mather the bounding rect scene or centerOn function.

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>
#include <QDebug>

//...

QGraphicsScene scene;
QGraphicsView view(&scene);

QRect viewRect(-100, -100, 200, 200);
view.setSceneRect(viewRect);
qDebug() << viewRect.center(); //QPointF(0,0)

scene.addEllipse(-5,-5,10,10);
qDebug() << scene.sceneRect(); //QRectF(-5,-5 10x10)
scene.addRect(QRectF(0, 0, 200, 200));
qDebug() << scene.sceneRect(); //QRectF(-5,-5 205x205)

view.show();

view.centerOn(QPointF(50, 50)); //nothing happens!

This should do the trick.


You can set the alignment property to achieve this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜