PDA

View Full Version : QGraphicsView - resize items on graphicsview resize -> calculate new positions



shock
3rd December 2011, 12:52
Hey Guys,

I am using a GraphicsView with a GraphicsScene which i added a PixmapItem....
The QGraphicsPixmapItem is treated as my background item on Z-value "0" on Z-value "1" i have several other pixmapItems.

My Problem:
When i resize the whole program the QGraphicsView also resizes and so the Background item should be resized (what works fine) - it is just a *.scaled() Pixmap where scaled() gets the current size of the GraphicsView.
Now i have to reposition the items on Z-value "1" to match the "same" position as before on the underlaying pixmap

My current Solution:
My current solution is not correct becauze the repositioning does "not work" - it's not exact .. (for the Background PixmapItem i used Qt::KeepAspectRatio)
I saved the original size of the background Pixmap and the position of each Item for that "base size"
On resizing i calculate the new x and y coordinates by referring to the old width/height of the Background Pixmap

This means:

NewWidth/oldWidth * oldX-Coordinate = NewX-Coordinate
NewHeight/oldHeight * oldY-Coordinate = NewY-Coordinate

Anyway .. it's "kinda" working but not exact (i thought it would work better that way ^^)

Anybody an idea how it is possible to calculate that more precisely?? - Would be really lovely ;)
Thanks =)

shock
8th December 2011, 09:17
Has nobody any idea? There must be a smart way to solve this i think!?

Spitfire
8th December 2011, 09:50
I'm just wondering, why you have to change position of the items in the scene?

Wouldn't it be simpler to add all the items to the scene in the places you want them, and then on every resize call QGraphicsView::fitInView() ?

This will make sure that everything is scalled as required and positioned correctly (that's assuming that you're not bothered that everything will get smaller/bigger as you change window size).

Unless I've misunderstood what you want to achieve.

shock
11th December 2011, 10:09
Hmm ... seems it was really that simple... ;) Thanks man ;)