[Qt-interest] QGraphicsView Transformations

Samuel Rødal sroedal at trolltech.com
Thu Jun 18 09:44:09 CEST 2009


Ollie wrote:
> Hi,
> 
> I'm using QGraphicsView to render QGraphicsItems of a
> QGraphicsScene.The whole QGraphicsScene can be manipulated from the
> QGraphicsView using scale(.), rotate(.), shear (.) and translate(.) .
> Though when calling translate(.) the items in QGraphicsScene do not
> move, while rotation, scale and shear work properly.
> Find below a minimal example illustrating the problem. Am I missing
> something..? I'm using Qt 4.5 on winxp.
> 
> Any suggestions are greatly received.
> 
> Thanks
> Olivier
> 
> /* sample code */
> #include <QApplication>
> #include <QGraphicsView>
> #include <QKeyEvent>
> 
> class Viewer:
> 		public QGraphicsView
> {
> public:
> 	Viewer(){};
> 	~Viewer(){};
> 	
> protected:
> 	void keyPressEvent(QKeyEvent *ke){
> 		switch(ke->key()){
> 			case Qt::Key_T:
> 				translate(10, 0);
> 				break;
> 			case Qt::Key_R:
> 				rotate(45);
> 				break;
> 			case Qt::Key_S:
> 				scale(1.1, 1.1);
> 			case Qt::Key_X:
> 				shear(0.1, 0.1);
> 			default:
> 				QWidget::keyPressEvent(ke);
> 		}
> 	}
> };
> 
> int main(int argc, char *argv[])
> {
> 	QApplication app(argc, argv);
> 
> 	Viewer viewer;
> 	viewer.setScene(new QGraphicsScene());
> 	viewer.scene()->addRect(100, 100, 50, 50);
> 		
> 	viewer.resize(640, 480);
> 	viewer.show();
> 
> 	return app.exec();
> }
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

Try changing main to do this:

     Viewer viewer;
     viewer.setScene(new QGraphicsScene());
     QGraphicsItem *item = viewer.scene()->addRect(100, 100, 50, 50);
     viewer.scene()->setSceneRect(-1000, -1000, 2000, 2000);
     viewer.centerOn(item);
     viewer.setTransformationAnchor(QGraphicsView::NoAnchor);

Problem is that unless the scene rect is larger than the view graphics 
view will make sure that the scene rect is centered in the view. Also, 
the default transformation anchor AnchorViewCenter ensures that the 
scene point at the center of the view remains unchanged, so you need to 
change the transformation anchor to NoAnchor as well.

Regards,

Samuel



More information about the Qt-interest-old mailing list