Results 1 to 8 of 8

Thread: How to create a text along with curve using QPainterPath

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to create a text along with curve using QPainterPath

    I don't have time to read your code right now, but it look like you are rotating each character by the right amount but in the wrong direction.

  2. #2
    Join Date
    Aug 2009
    Posts
    6
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: How to create a text along with curve using QPainterPath

    Thanks Chris,

    Yes I dont have any clue how I can achieve a rotation based on angle QTransform.

  3. #3
    Join Date
    Aug 2009
    Posts
    6
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: How to create a text along with curve using QPainterPath

    http://labs.qt.nokia.com/2006/11/07/text-on-a-path/ ... this looks also my solution but a google search for Zack Rusin returned https://www.facebook.com/pages/RIP-Z...830579?sk=info, if its true and if its the same Zack Rusin then its so sad

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to create a text along with curve using QPainterPath

    Actually, the original C++ code has the same problem. You get a better result if you make the angle negative at line 44 of your Python version.

    The solution can e simplified a little by getting Qt to do more of the mathematics:
    Qt Code:
    1. #include <QtGui>
    2. #include <cmath>
    3.  
    4. class Widget : public QWidget
    5. {
    6. public:
    7. Widget ()
    8. : QWidget() { }
    9. private:
    10. void paintEvent ( QPaintEvent *)
    11. {
    12. QString hw("hello world");
    13. int drawWidth = width() / 100;
    14. QPainter painter(this);
    15. QPen pen = painter.pen();
    16. pen.setWidth(drawWidth);
    17. pen.setColor(Qt::darkGreen);
    18. painter.setPen(pen);
    19.  
    20. QPainterPath path(QPointF(0.0, 0.0));
    21.  
    22. QPointF c1(width()*0.2,height()*0.8);
    23. QPointF c2(width()*0.8,height()*0.2);
    24.  
    25. path.cubicTo(c1,c2,QPointF(width(),height()));
    26.  
    27. //draw the bezier curve
    28. painter.drawPath(path);
    29.  
    30. //Make the painter ready to draw chars
    31. QFont font = painter.font();
    32. font.setPixelSize(drawWidth*2);
    33. painter.setFont(font);
    34. pen.setColor(Qt::red);
    35. painter.setPen(pen);
    36.  
    37. qreal percentIncrease = (qreal) 1/(hw.size()+1);
    38. qreal percent = 0;
    39.  
    40. for ( int i = 0; i < hw.size(); i++ ) {
    41. percent += percentIncrease;
    42.  
    43. QPointF point = path.pointAtPercent(percent);
    44. qreal angle = path.angleAtPercent(percent); // Clockwise is negative
    45.  
    46. painter.save();
    47. // Move the virtual origin to the point on the curve
    48. painter.translate(point);
    49. // Rotate to match the angle of the curve
    50. // Clockwise is positive so we negate the angle from above
    51. painter.rotate(-angle);
    52. // Draw a line width above the origin to move the text above the line
    53. // and let Qt do the transformations
    54. painter.drawText(QPoint(0, -pen.width()),QString(hw[i]));
    55. painter.restore();
    56. }
    57. }
    58.  
    59. };
    60.  
    61. int main(int argc, char **argv)
    62. {
    63. QApplication app(argc, argv);
    64. Widget widget;
    65. widget.show();
    66. return app.exec();
    67. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2009
    Posts
    6
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: How to create a text along with curve using QPainterPath

    thanks a lot Chris, it works perfectly ...

Similar Threads

  1. Replies: 9
    Last Post: 12th May 2014, 01:25
  2. How to create a 2d curve editor?
    By vinaym in forum Qwt
    Replies: 1
    Last Post: 4th June 2013, 06:29
  3. how to create a curve in qwtplot?
    By ethanfeng in forum Qwt
    Replies: 1
    Last Post: 27th March 2011, 09:05
  4. Replies: 5
    Last Post: 28th May 2010, 10:45
  5. How draw a rotated text in a QPainterPath?
    By iw2nhl in forum Qt Programming
    Replies: 6
    Last Post: 17th August 2007, 18:55

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
  •  
Qt is a trademark of The Qt Company.