Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Ignoring transformations

If you try to zoom in on our custom rectangles scene (for example, by calling view.scale(4, 4)) , you will note that everything is scaled proportionally, as you would expect. However, there are situations where you don't want some elements to be affected by scale or other transformations. Qt provides multiple ways to deal with it.

If you want lines to always have the same width, regardless of the zoom, you need to make the pen cosmetic:

QPen pen = parent->pen();
pen.setCosmetic(true);
parent->setPen(pen);

Now, the rectangles will always have lines with one-pixel width, regardless of the view's scale (anti-aliasing can still blur them, though). It's also possible to have cosmetic pens with any width, but using them in Graphics View is not recommended.

Another common situation where you don't want transformation to apply is displaying text. Rotating and shearing text usually makes it unreadable, so you'd usually want to make it horizontal and untransformed. Let's try to add some text to our project and look at how we can solve this problem.