[Qt-jambi-interest] maping & Cooirdinates
Gunnar Sletta
gunnar at trolltech.com
Mon Aug 11 08:23:41 CEST 2008
Dawid Sip wrote:
> Hi,
>
> Im having a tough time with Rectangle positioning in a scene.
> Basicaly I'm using a custom Cursor made from an rectangular image. I
> take the im and get its .rect(), I than make
> graphicsView.mapToScene('the rectangle'), and get a polygon from which i
> create graphicsItem which i add to the scene. I want this item to be
> added at the cursor position which i have in QPoint p, do i do
> polygonItem.moveBy(p.x(), p.y()). Unfortunately this does not place the
> item where the cursor is. This has something to do with the
> graphicsView.mapToScene transformation. When I ommit this transformation
> and create polyItem directrly from the 'rect' and move it by the amount
> in p, all works fine. I cant leave it like this though because i need
> the mapToScene to make the polyItem dimensions appear exactly like the
> cursors. What do i do wrong here?
Hi Dawid,
I think you may want to have a look at:
http://doc.trolltech.com/qtjambi-4.4/html/com/trolltech/qt/graphicsview.html#the-graphics-view-coordinate-system
Maybe it will solve some of the confusion.
There are several coordinate systems here. The QGraphicsView operates in
pixels and is called view coordinates. The mouse is based on this
coordinate system.
The items live in scene coordinates. If there is no transformation what
so ever and no scrolling etc the scene and view coordinate systems are
the same. If there are transformations or any kind these two are
different and things will go wrong if mixed.
There is also the items coordinate system which is based on the scene's
in addition to the acculuated transformations of all the parents of the
item, but thats all covered in the link above...
When you want to position an item under the mouse, you need to map the
mouse position (view space) to the scene.
protected void updateItemToMousePos(QPoint mousePos,
QGraphicsItemInterface item) {
QPointF sceneMousePos = view.mapToScene(mousePos);
item.setPos(sceneMousePos);
}
The above will place the items coordinate system so that the items (0,0)
is located under the mouse. If the is a rect item that has a
boundingRect() that is (0, 0, 10, 10), you'll now have a rect item
starting at the mouse and spanning 10 pixels down and to the right.
If the scene is scaled by 2,2, you would have an rect there that was 20
by 20 pixels large and so forth..
Hope this helps a bit ;-)
best regards,
Gunnar
More information about the Qt-jambi-interest
mailing list