PDA

View Full Version : Using custom coordinates with QGraphicsScene



rec
6th June 2010, 17:48
I am experimenting with a WYSIWYG editor that allows a user to draw shapes on a page and the Qt graphics scene support seems perfect for this. However, instead of working in pixels I want all my QGraphicsItem objects to work in tenths of a millimetre but I don't know how to achieve this.

For example:


// Create a scene that is the size if an A4 page (2100 = 21cm, 2970 = 29.7cm)
QGraphicsScene* scene = new QGraphicsScene(0, 0, 2100, 2970);
// Add a rectangle located 1cm across, 1cm down, 5cm wide and 2cm high
QGraphicsItem* item = scene->addRect(100, 100, 500, 200);
...
QGraphicsView* view = new QGraphicsView(scene);
setCentralWidget(view);

Now, when I display the scene above I want the shapes to appear at correct size for the screen DPI. Is this simply a case of using QGraphicsView::scale or do I have to do something more complicated?

Note that if I was using a custom QWidget instead then I would use QPainter::setWindow and QPainter::setViewport to create a custom mapping mode but I can't see how to do this using the graphics scene support.

Any advice on WYSIWYG support with scenes is welcome.

high_flyer
7th June 2010, 09:44
However, instead of working in pixels I want all my QGraphicsItem objects to work in tenths of a millimeter but I don't know how to achieve this.
The QGraphicsView coordinate system is not pixel based, its logical.
For convenience, a non transformed one unit corresponds to one pixel.
But you can transform it to fit any unit you want.

If you want to have the displayed units correspond with tenth of cm,you have to take the physical size of the screen pixel in account, which may differ between screens.
If the displayed image does not need to have a real physical size, and only the correct unit ratio, then you have no problem.