Results 1 to 8 of 8

Thread: Painting Rich Text

  1. #1
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Painting Rich Text

    Hi,

    I want to paint HTML-formatted text with a QPainter. In Qt3, this was easily done with QSimpleRichText - but I am not sure what the equivalent method is in Qt4.

    I have tried using the QTextLayout class, but without success. This code, for example, does not appear to draw anything:

    Qt Code:
    1. QTextLayout l( "test" );
    2. l.draw( qPainter, QPointF( 0, 0 ) );
    To copy to clipboard, switch view to plain text mode 

    I have tried passing a font to QTextLayout in its constructor, changing the pen used in QPainter, etc, but none of these changes has resulted in drawn text.

    Any ideas on where I'm going wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting Rich Text

    IMO you must help it a bit with the layout process:
    Qt Code:
    1. QTextLayout l( "test" );
    2. l.beginLayout();
    3. QTextLine line = l.createLine();
    4. line.setLineWidth( 100 );
    5. line.setPosition( QPointF( 0, 14 ) );
    6. l.endLayout();
    7. l.draw( qPainter, QPointF( 100, 100 ) );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Painting Rich Text

    Excellent - that works perfectly!

    Thanks Jacek

  4. #4
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Painting Rich Text

    Another issue - the painted text isn't formatted according to the HTML tags; instead, the text is printed as plain text (including the tags).

    I couldn't see any options in QTextLayout documentation for specifying the formatting of the text - only a reference to using it as part of a QTextDocument..

    So how do I make it format it as rich text (using HTML tags to achieve the formatting isn't important)?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting Rich Text

    As the docs say:
    The QTextLayout class is used to lay out and paint a single paragraph of text.
    ...
    The class has a rather low level API and unless you intend to implement your own text rendering for some specialized widget, you probably won't need to use it directly.
    Try QTextDocument::documentLayout() and QAbstractTextDocumentLayout::draw().

  6. #6
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Painting Rich Text

    Thanks for the tip, Jacek.

    However, I can't get anything to appear via QAbstractTextDocumentLayout::draw(). For example, here is some test code that doesn't work:

    Qt Code:
    1. QTextDocument td( "test" );
    2. QAbstractTextDocumentLayout::PaintContext ctx;
    3. ctx.clip = QRectF( 0, 0, 400, 100 );
    4. qPainter->drawRect( ctx.clip ); // Test that I'm drawing in the right area
    5. td.documentLayout()->draw( qPainter, ctx );
    To copy to clipboard, switch view to plain text mode 

    The clip rectangle gets drawn properly, but no text is drawn.

    I've tried looking at how QTextEdit draws its QTextDocument, and I can't see anything significantly different to what I am doing, but obviously there must be something..

    Any ideas?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting Rich Text

    Add:
    Qt Code:
    1. td.setPageSize( QSizeF( 400, 100 ) );
    To copy to clipboard, switch view to plain text mode 
    Probably you need also:
    Qt Code:
    1. td.documentLayout()->setPaintDevice( this );
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to jacek for this useful post:

    xanthine (16th April 2006)

  9. #8
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Painting Rich Text

    It's all working perfectly now. Thanks for all your help!

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. Rich text in QGraphicsTextItem
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2008, 21:08
  3. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 17:45
  4. Replies: 3
    Last Post: 13th December 2007, 17:10
  5. Rich Text Format
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 4th October 2006, 11:53

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.