Results 1 to 5 of 5

Thread: Painter: transform coordinates but not pen width settings

  1. #1
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Painter: transform coordinates but not pen width settings

    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:

    Qt Code:
    1. QTransform trans = painter->transform();
    2. painter->resetTransform();
    3.  
    4. QPen pen = painter->pen();
    5. pen.setWidth(new_width);
    6. painter->setPen(pen);
    7.  
    8. painter->setTransform(trans);
    To copy to clipboard, switch view to plain text mode 

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

    Any ideas?
    This rude guy who doesn't want you to answer his questions.

    Note: An "expert" here is just someone that's posted a lot.

    "The fact of where you do the encapsulation is meaningless." - Qt Certified Developer and forum moderator

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Painter: transform coordinates but not pen width settings

    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.
    Last edited by SixDegrees; 12th April 2011 at 22:42.

  3. #3
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painter: transform coordinates but not pen width settings

    Quote Originally Posted by SixDegrees View Post
    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.
    Last edited by nroberts; 12th April 2011 at 23:20.
    This rude guy who doesn't want you to answer his questions.

    Note: An "expert" here is just someone that's posted a lot.

    "The fact of where you do the encapsulation is meaningless." - Qt Certified Developer and forum moderator

  4. #4
    Join Date
    Dec 2006
    Location
    Kędzierzyn-Koźle
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painter: transform coordinates but not pen width settings

    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-intere...ad00163-0.html

  5. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Painter: transform coordinates but not pen width settings

    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

Similar Threads

  1. Replies: 4
    Last Post: 18th March 2010, 10:11
  2. QToolBox width fixed to maximum content width
    By ghorwin in forum Qt Programming
    Replies: 0
    Last Post: 10th July 2009, 09:58
  3. Replies: 6
    Last Post: 14th April 2009, 17:40
  4. How to set QTableView width to width of horizontal header?
    By martinb0820 in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 20:51
  5. Changing painter settings for QPixmap
    By spagatoni in forum Newbie
    Replies: 7
    Last Post: 22nd October 2008, 16:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.