Results 1 to 3 of 3

Thread: QPainter::rotate and Qwt problems

  1. #1
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QPainter::rotate and Qwt problems

    Hello,

    I'm porting a QPainter-based code (written in absolute coordinates) to Qwt plot item. Normally the porting consists of transforming the coordinates using QwtScaleMap::transform() and using QwtPainter's static draw*() functions. But the original code uses QPainter::rotate() which really screws it all up and gives some really strange (and inexplicable) results.

    For example,
    Qt Code:
    1. for(int i=0; i<50; i++) {
    2. painter->save();
    3. painter->rotate(-90 - i*7.2);
    4. QwtPainter::drawText(painter, QwtScaleMap::transform(xMap, yMap, QRectF(-14,-595,100,100)),
    5. Qt::AlignLeft | Qt::AlignBottom, QString::number);
    6. painter->restore();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Basically, what it does (in the original) is that it draw the numbers radially around a circle (think numbers around a dial).

    I can use a separate QTransform object, rotate it and map that rectangle to it, giving me the text approximately where I want it. But, it's still off enough to be bad, and the text itself is not rotated (and I really need that).

    Is there a way to make the painter rotation work with Qwt? Or maybe draw the rotated text some other way?

    Thanks in advance!
    Alexander

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,311
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter::rotate and Qwt problems

    You have to take care about the origin of your rotation. Your code rotates around the top left position of the plot cancas.

    Rotating the text around the center of a rectangle would work like this ( untested, out of my memory ):

    Qt Code:
    1. QRectF textRect QwtScaleMap::transform(xMap, yMap, QRectF(-14,-595,100,100) );
    2. painter->save();
    3. painter->translate( textRect.center() );
    4. painter->rotate( -90 - i * 7.2);
    5.  
    6. textRect.moveCenter( QPointF( 0.0, 0.0 ) );
    7. QwtPainter::drawText(painter, textRect, Qt::AlignLeft | Qt::AlignBottom, QString::number);
    8.  
    9. painter->restore();
    To copy to clipboard, switch view to plain text mode 
    But in your case I guess you want to align the rotated text to a specific position not to the center of some pointless rectangle.

    HTH,
    Uwe

  3. The following user says thank you to Uwe for this useful post:

    alex_sh (20th October 2011)

  4. #3
    Join Date
    Sep 2010
    Posts
    46
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter::rotate and Qwt problems

    Thanks!

    Using your hint I was able to fix the problem like this:
    Qt Code:
    1. QPointF zero_point = QwtScaleMap::transform(xMap, yMap, QPointF(0, 0));
    2. for(int i=0; i< 50; i++) {
    3. painter->save();
    4. painter->translate(zero_point);
    5. painter->rotate(-90 - i * 7.2);
    6. painter->translate(QPointF(-zero_point.x(), -zero_point.y()));
    7. QwtPainter::drawText(painter, QwtScaleMap::transform(xMap, yMap, QRectF(-14,595-100,100,100)),
    8. Qt::AlignLeft | Qt::AlignBottom, QString::number(i));
    9. painter->restore();
    10. }
    To copy to clipboard, switch view to plain text mode 

    The original code had (0, 0) coordinates in the center of the painter (using window / viewport translation), so that was the rotation center. By translating to it, rotating and then translating back the problem fixed itself.
    Another small problem was that the Y axis was inverted (QwtScaleMap::transform() inverts it), hence 595-100 instead of -595.

    Thanks again!

Similar Threads

  1. Problems with QPainter
    By franco.amato in forum Qt Programming
    Replies: 13
    Last Post: 15th March 2010, 07:29
  2. Problems with QPainter
    By franco.amato in forum Qt Programming
    Replies: 11
    Last Post: 23rd November 2009, 20:54
  3. Qt3-QPainter rotate exception
    By Raccoon29 in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2007, 17:43
  4. Replies: 3
    Last Post: 30th April 2006, 19:22
  5. QPainter rotate function
    By ir210 in forum Newbie
    Replies: 3
    Last Post: 17th January 2006, 04:31

Tags for this Thread

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.