You are not logged in.

1

Sunday, March 21st 2010, 8:00pm

QRectF coordinats ?

Hi everyone,

I am a little confused by the QGraphics framework coordiantes system. If I add a QGraphicsRectItem to a QGraphicsScene by the function QGraphicsRectItem * QGraphicsScene::addRect ( const QRectF & rect, const QPen & pen = QPen(), const QBrush & brush = QBrush() ), let's say the QRectF is given as QRectF(x, y, Width, Height), how do those values correspond to the scene coordinates? I can see that the top-left corner of the item I added in the scene is (x,y), so I assume the first two arguments in QRectF(x,y, Width, Height) mean the scene coordinates (or parent coordiantes). However, if I use QGraphicsItem::scenePos() to get the scene coordinates of the item I just added, it shows that it is (0,0). So I am totally lost. And my next question derived from the last one is, if I just create a QRectF item but don't add it to any scene or item, then what's the meaning of the first two arguments in the constructor? Any can help me? Thanks!

Cheers

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Sunday, March 21st 2010, 9:51pm

tetelee,

Lets go basic by creating a scene and placing a grahics item in the top right corner of the scene with coords (0,0) as the top right coords.

First create the scene providing it the coords you want to layout.
QGrahicsScene *scene = new QGraphicsScene( this );
scene->setSceneRect( 0,0,500,500 ); // you now have scene with coords topleft( 0,0), bottom right ( 500,500 );
create your item. You chose Rect;
scene->addRect( 0, 0, 100, 50 ); // Adds rectangle at scene coords( 0,0); topleft corner of scene
/* NOTE: if you graphicsview is stretched bigger than your scene it will appear it's not in the corner but off some when its not*/
You could do this as well: (#include <QGraphicsRectItem> )
QGraphicsRectItem *rect = scene->addRect( 0,0,100,50 );
rect->setPos( 250,250 ); // this would put the rect in the middle of the scene (so to speak based on coords of scene )

Of course you will need to add the scene to graphicsView for this to work above;
graphicsView->setScene( scene );

This is just basic - but it might be enough to get you going to understand the reading portions. It does explain it quite well.

Hope this is helpful,

Junior