tags:

views:

140

answers:

2

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.

+2  A: 

QGraphicsView::scale should do the job. But I prefer setting the transform. It gives me much more control over how the scene is displayed. But that's because I need things like rotation, flipping, etc. It also allow me to track what I did to the scene.

Stephen Chu
A: 

Stephen Chu set me on the right path - using scale seems to work.

const qreal xScale = physicalDpiX() / 254.0;
const qreal yScale = physicalDpiY() / 254.0;
view->scale(xScale, yScale);

This results in measurements that look accurate on screen.

Rob