Results 1 to 5 of 5

Thread: Subscript text in painter->drawTExt(..)

Threaded View

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

    Default Re: Subscript text in painter->drawTExt(..)

    Qt Code:
    1. void drawRichText(QPainter *painter, const QRect &rect, int flags, QTextDocument &text)
    2. {
    3. text.setPageSize(QSize(rect.width(), QWIDGETSIZE_MAX));
    4.  
    5. QAbstractTextDocumentLayout* layout = text.documentLayout();
    6.  
    7. const int height = qRound(layout->documentSize().height());
    8. int y = rect.y();
    9. if (flags & Qt::AlignBottom)
    10. y += (rect.height() - height);
    11. else if (flags & Qt::AlignVCenter)
    12. y += (rect.height() - height)/2;
    13.  
    14. QAbstractTextDocumentLayout::PaintContext context;
    15. context.palette.setColor(QPalette::Text, painter->pen().color());
    16.  
    17. painter->save();
    18.  
    19. painter->translate(rect.x(), y);
    20. layout->draw(painter, context);
    21.  
    22. painter->restore();
    23. }
    To copy to clipboard, switch view to plain text mode 

    If you need additional operations like heightForWidth() or textSize() you can have a look at the implementation of QwtRichTextEngine in the Qwt package.

    And yes, you can ask yourself, why you have to write this strange code for such a simple task.

    HTH,
    Uwe

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

    maverick_pol (8th January 2008)

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.