PDA

View Full Version : Painter: transform coordinates but not pen width settings



nroberts
12th April 2011, 22:16
I want to use QPainter's transform setup to convert coordinates in my system into the device's system (including zoom) but I don't want to apply those same changes to pens. Currently I have tried:



QTransform trans = painter->transform();
painter->resetTransform();

QPen pen = painter->pen();
pen.setWidth(new_width);
painter->setPen(pen);

painter->setTransform(trans);


This isn't working. After this call the pen is this giant rectangle rather than a two pixel line.

Any ideas?

SixDegrees
12th April 2011, 22:33
First, a pen of width 0 is always drawn at the same (1-pixel) width.

Second, you could apply the transform to the painter, then apply its inverse to the pen. Not exactly sure how to proceed with that idea, but the basic idea is to scale the pen width back to what it started out as.

---------------

Or, simply store the "fixed" pen size, apply the transform to the painter, then set the pen width to the stored value.

nroberts
12th April 2011, 23:04
First, a pen of width 0 is always drawn at the same (1-pixel) width.

Second, you could apply the transform to the painter, then apply its inverse to the pen. Not exactly sure how to proceed with that idea, but the basic idea is to scale the pen width back to what it started out as.

I've tried something very similar to this. Originally I was applying my own transforms to get what I'm now mostly getting with the painter transform. I used that original code to invert the width setting and pass it to setWidthF.

This worked except that when drawing a line that starts and ends in exactly the same place I get a box that's the size of my pen in my world coordinates. I can't make any sense of this behavior. Nowhere that is not my wrapper code is setWidth being called and yet under this odd condition it looks like the device width is in the wrong system. I could of course filter out draw commands that trigger this, but I would like to understand and possibly fix whatever I'm doing wrong.

---------------------------

Correction: It appears that I was a touch mistaken. When the line length is 0 (start==end) and I leave out my pen width correction, I still get a square "dot" that is huge compared to what I expect. When I set my pen width to '2' I get a box that is 28x28 in world coordinates. When I set my pen width to to_world(2), which converts 2 pixels into a distance in the world, I get a box that is 2x2 in world coordinates. I'm betting this is still 14x the pen's true width.

tzioboro
13th June 2012, 15:44
Hi I had the same problem ...
Anybody that get here - you might use QPen::setCosmetic - that will obey all transformations and draw line with given width of pixels.
If you are going to use it with different devices (ex. printer and screen) you might recalculate thickness regardles to device ppi or other metrics.

This thread was very helpfull about this:
http://lists.trolltech.com/qt-interest/2006-12/thread00163-0.html

montylee
16th January 2013, 08:53
QPen::setCosmetic(true) works fine on all platforms except Mac. There are some issues on Mac and even after making the pen cosmetic, it's getting scaled. Check out this bug log too:

https://bugreports.qt-project.org/browse/QTBUG-16303