Results 1 to 3 of 3

Thread: Problem with QPainter drawText() not rendering text at large coordinates

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Talking Re: Problem with QPainter drawText() not rendering text at large coordinates

    I found a work around for this, as suggested in bug 192573:
    http://qt.nokia.com/developer/task-t...ntry&id=192573

    I used the worldTransform to translate the painter, so that I can use position offsets in drawText() that start at 0.

    Qt Code:
    1. void TimeRulerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. Q_UNUSED(widget);
    4.  
    5. if(option->levelOfDetail < 0.05) return;
    6.  
    7. QColor fillColor = (option->state & QStyle::State_Selected) ? Bg.dark(150) : Bg;
    8.  
    9. if (option->state & QStyle::State_MouseOver)
    10. fillColor = fillColor.light(125);
    11.  
    12. QColor color;
    13. qreal width;
    14. if (option->state & QStyle::State_MouseOver) {
    15. color = QColor(Qt::yellow);
    16. width = 1.0;
    17. } else {
    18. color = Fg;
    19. width = 0.75;
    20. }
    21.  
    22. painter->setFont(QApplication::font());
    23. painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
    24. painter->setPen(QPen(color, width));
    25.  
    26. const QRectF exposed = option->exposedRect;
    27. const qreal ex_left = exposed.left();
    28. const qreal ex_right = exposed.right();
    29.  
    30. const qint64 start_tick = (qint64)ex_left/TICK_WIDTH;
    31. const qint64 end_tick = (qint64)ex_right/TICK_WIDTH;
    32.  
    33. const qint64 n_tick = start_tick*TICK_WIDTH;
    34. const qint64 n_tick_1 = n_tick - 1;
    35.  
    36. QTransform wt = painter->worldTransform();
    37. wt.translate(n_tick_1, 0);
    38. painter->setWorldTransform(wt);
    39.  
    40. for(qint64 i = 0; i <= end_tick-start_tick; i++) {
    41. const qint64 time = i + start_tick + StartTime;
    42. const qint64 time_mod_10 = time%10;
    43.  
    44. // Draw the tick mod 10 lines:
    45. if(time%10 == 0) painter->drawLine(i*TICK_WIDTH, TICK_HEIGHT, i*TICK_WIDTH, -2*TICK_HEIGHT);
    46.  
    47. if(option->levelOfDetail < 0.5) return;
    48.  
    49. // Draw tick line every 10 ticks:
    50. painter->setPen(QPen(color, width));
    51. const QString time_str = QString::number(time);
    52. const QString time_mod_10_str = QString::number(time_mod_10);
    53.  
    54. const qint64 x = i*TICK_WIDTH;
    55. const qint64 x_1 = (i*TICK_WIDTH) + 1;
    56.  
    57. const QPoint pt = QPoint(x , -1);
    58. const QPoint pt_1 = QPoint(x_1, 11);
    59.  
    60. // Draw full time every 10 ticks:
    61. if(time%10 == 0) painter->drawText(pt, time_str);
    62.  
    63. // Draw time mod 10 (tick number):
    64. painter->drawText(pt_1, time_mod_10_str);
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to cgomez116 for this useful post:

    yj_yulin (19th December 2012)

Similar Threads

  1. text rendering problem
    By vno4697 in forum Qt Programming
    Replies: 0
    Last Post: 12th August 2008, 14:44

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.