PDA

View Full Version : Fixed QGraphicsScene in view



jano_alex_es
9th May 2012, 21:13
Hi,

by default, when we add an item in a QGraphicsScene the view tries to center it. If we add one more it tries to center both... I'd like to have it "static", a viewport unable to move.

In order to do that I set the size of the scene to the view:



//Scene
pScene = new QGraphicsScene(ui->view);
pScene->setSceneRect(0, 0, ui->view->width(), ui->view->height());
//View
ui->view->setInteractive(false);
ui->view->setScene(pScene);


QGraphicsTextItem* txtG = pScene->addText("G");
txtG->setPos(0, 0);
QGraphicsTextItem* txtD = pScene->addText("D");
txtD->setPos(110, 110);



It solves the problem, but the coordinates seem to be wrong, 0, 0 should be corner top-left and it's close to the view's center.

Does anybody know how I can solve this problem or any other way to "fix the viewport"?

thanks!

wysota
10th May 2012, 01:29
If I were you, I'd check if ui->view->width() and ui->view->height() return values you expect since if you call this code before the window is first shown, they are likely to have bogus values (as of course explained in the docs).