[Qt-interest] Keep Same QGraphicsItem's Pen Width Across Scaling

John McClurkin jwm at nei.nih.gov
Fri Oct 2 19:08:55 CEST 2009


anonymous anonymous wrote:
> Hi again.
> I dont know if I have a problem with my mailing list account, but for 
> the 3rd time: I think what you are looking for is exactly 
> QPen::setCosmetic(true). No more, no less. No matrix or other hack 
> involved. Cosmetic pen do not have their width impacted by any item 
> transformation.

No, setCosmetic is exactly not what he is looking for. Seting cosmetic 
to true means that the pen draws with a constant width regardless of 
transformation. the O.P. wants to change the pen width depending on the 
scale of the drawing.

> 
> 
> 
> 2009/10/2 Josiah Bryan <jbryan at productiveconcepts.com 
> <mailto:jbryan at productiveconcepts.com>>
> 
>     Ben Swerts wrote:
>      > Hi Josiah,
>      >
>      > In the paint function of your custom QGraphicsItem you can
>     retrieve the
>      > transformation matrix of the painter (QPainter::transform). You
>     can then
>      > multiply the pen width with the inverse of the scaling value to
>     obtain a
>      > fixed width.
>      >
>     Hey Ben -
> 
>     Thanks! Worked perfectly. After reading
>     http://doc.trolltech.com/4.3/qtransform.html#basic-matrix-operations, I
>      found I could use m11 and m22 for scaling. For the record, here's the
>     code that I finally came up with:
> 
>     QPen p = ...;
>     QTransform tx = painter->transform();
>     qreal scale = qMax(tx.m11(),tx.m22());
>     p.setWidthF(1/scale * p.widthF());
>     painter->setPen(p);
> 
>     Not sure if qMax or qMin would be more appropriate to extract a 1-D
>     scale value for our use, so I rolled the dice and decided to use qMax.
>     Looks good enough for my use.
> 
>     Thanks again for your help, Ben!
> 
>     Cheers!
>     -josiah
> 
> 
>      >
>      >> I've got a custom QGraphicsItem (simple box) that draws an outline
>      >> around itself, say 3px wide. Great for "normal" live view.
>     However, my
>      >> app also renders an "icon" view of the scene that the box is in
>     - this
>      >> "icon" is about 96x96. Then there's also the "preview" view of
>     the scene
>      >> before it goes live, this is rendered about 320x240. The live
>     scene is
>      >> scaled approx 1:1, so its about 1024x768.
>      >>
>      >> What I'm wondering is if there's any way for my custom graphics
>     item to
>      >> find out the scale it's being rendered at so that it can adjust
>     it's pen
>      >> width accordingly inorder to render at approx 3px width
>     regardless of
>      >> scale? (The outline isn't always 3px - it can vary depending on
>     various
>      >> other factors [type of box, datapoint the box represents, etc]).
>      >>
>      >> Any ideas on how to find the scale of the scene from inside the
>     paint
>      >> method of my graphics item?
>      >>
>      >> Thanks!
>      >> -josiah
>      >
>      > _______________________________________________
>      > Qt-interest mailing list
>      > Qt-interest at trolltech.com <mailto:Qt-interest at trolltech.com>
>      > http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
>     --
> 
>     -=-=-=-=-=-=-=-=-=-=-=-=-
>     Josiah Bryan
>     Productive Concepts, Inc.
>     jbryan at pciint.com <mailto:jbryan at pciint.com>
>     (765) 964-6009, ext. 224
> 
>     _______________________________________________
>     Qt-interest mailing list
>     Qt-interest at trolltech.com <mailto:Qt-interest at trolltech.com>
>     http://lists.trolltech.com/mailman/listinfo/qt-interest
> 
> 



More information about the Qt-interest-old mailing list