Using custom coordinates with QGraphicsScene

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2010-06-06T12:47:50Z Indexed on 2010/06/06 12:52 UTC
Read the original article Hit count: 351

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt