Results 1 to 3 of 3

Thread: QTextLayout draws text outside the widget

  1. #1
    Join Date
    Aug 2009
    Posts
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextLayout draws text outside the widget

    Hi all,

    Working on simple CAD application using QGraphicsView/QGraphicsScene, I'm trying to implement a 'text entity' derived from QGraphicsItem. (something like the QGraphicsSimpleTextItem).
    Inside this item I'm using the QTextLayout::draw() to draw the text. And this is the problem. The text is drawn also outside the view widget (QGraphicsView) as shows the attached image.

    Has anybody an idea how to solve this? I can also post a code ...
    Thans a lot

    Tomas
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextLayout draws text outside the widget

    WTF? Wow, what have you done to achieve that?
    Quote Originally Posted by alpinista View Post
    I can also post a code ...
    That would be the easiest way, because I can imagine some errors...

  3. #3
    Join Date
    Aug 2009
    Posts
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextLayout draws text outside the widget

    yeah, it's pretty strange

    here's the code:

    Qt Code:
    1. /**
    2.  *
    3.  *
    4.  */
    5. void CTextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    6. {
    7. if (isSelected())
    8. painter->setPen(_sel_pen);
    9. else
    10. painter->setPen(_std_pen);
    11.  
    12.  
    13. _layout->draw(painter, QPointF(0, -_line_height));
    14.  
    15.  
    16. // draw cursor
    17. if (_draw_cursor)
    18. {
    19. painter->setPen(_cur_pen);
    20. painter->drawLine(_lastline_width, boundingRect().bottom(), _lastline_width + Lexo2D::Const::DEFAULT_TEXT_SIZE, boundingRect().bottom());
    21. }
    22.  
    23.  
    24. // for debugging purposes ...
    25. if (Lexo2D::Debug::show_bounding_rectangles)
    26. {
    27. // draw bounding rectangle
    28. painter->setPen(QColor(255, 0, 0));
    29. painter->drawRect(boundingRect());
    30.  
    31. // draw shape
    32. painter->setPen(QColor(255, 255, 0));
    33. painter->drawPath(_shape);
    34. }
    35. }
    36.  
    37.  
    38.  
    39. /**
    40.  *
    41.  *
    42.  */
    43. void CTextItem::setText(QString & str)
    44. {
    45. _layout->setText(str);
    46.  
    47. setupLayout();
    48. }
    49.  
    50.  
    51.  
    52.  
    53. /**
    54.  *
    55.  *
    56.  */
    57. void CTextItem::setupLayout()
    58. {
    59. _layout->beginLayout();
    60.  
    61. while (_layout->createLine().isValid())
    62. ;
    63.  
    64. _layout->endLayout();
    65.  
    66. _shape = QPainterPath();
    67. qreal height = 0;
    68. qreal width = 0;
    69.  
    70. for (int i = 0; i < _layout->lineCount(); ++i)
    71. {
    72. QTextLine line = _layout->lineAt(i);
    73. width = qMax(width, line.naturalTextWidth());
    74. _lastline_width = line.naturalTextWidth();
    75.  
    76. line.setPosition(QPointF(0, height));
    77. height += line.height();
    78.  
    79. _shape.addRect(line.naturalTextRect().translated(0, -line.height()));
    80. }
    81.  
    82.  
    83. if (_layout->lineCount() > 0)
    84. _line_height = _layout->lineAt(0).height();
    85.  
    86. prepareGeometryChange();
    87.  
    88. _bounding_rect = _layout->boundingRect();
    89. _bounding_rect.setWidth(width + Lexo2D::Const::DEFAULT_TEXT_SIZE);
    90. _bounding_rect.translate(0, -_line_height);
    91.  
    92. update();
    93. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 08:06
  3. ‎a lightweight text showing widget
    By jacum in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2007, 15:44
  4. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 16:30
  5. widget for text AND buttons
    By nongentesimus in forum Newbie
    Replies: 2
    Last Post: 16th June 2006, 14:43

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.