PDA

View Full Version : QGraphicsScene changes visible area when scenerect changes



ThomasS0
26th October 2015, 15:51
Hi,
when i click into the graphicsView with the following example a QGraphicsEllipseItem will be added.
My problem is that the already existing QGraphicsEllipseItems seem to "mov"e each time when a new item is added.
I want the visible are of the scene to be fixed.

When i set the scene rect to a fixed rect, the items stay in position, but then the scene rect is not correct.
I want the sceneRect to include all items and thad the visible area of the scene stays fixed when i add new items.

Can anyone help?



#include <QGraphicsScene>
#include <QGraphicsView>
#include <QApplication>
#include <QGraphicsEllipseItem>
#include <QMouseEvent>
#include <QPointF>

class MyView : public QGraphicsView {
public:
void mousePressEvent(QMouseEvent *event) {
QPointF pos = mapToScene(event->pos());
scene()->addItem(new QGraphicsEllipseItem(pos.x()-5,pos.y()-5,10,10));
}
};

int main(int argc, char **argv) {

QApplication app(argc, argv);

QGraphicsScene scene;
MyView v;
v.setScene(&scene);
v.show();

return app.exec();

}

anda_skoa
26th October 2015, 17:08
You could try to set the view's resize anchor to NoAnchor.

Cheers,
_

ThomasS0
26th October 2015, 17:23
setting the resize anchor doesn't help. As far as is understand, the resize anchor is only relevent when the QGraphicsView is resized?

When i add items, not the size of the QGraphicsView is changed, but the size of the sceneRect.

anda_skoa
26th October 2015, 20:13
Ah, too bad.
Would have been a lucky guess :)

Cheers,
_

ThomasS0
27th October 2015, 13:42
ok... i guess its not a big deal if you know how to do it. Any other ideas?