Results 1 to 9 of 9

Thread: Html text bounding rectangle

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Html text bounding rectangle

    Qt Code:
    1. /* virtual */ const unsigned int ZeInfoItem::UpdateHeight(const unsigned int width)
    2. {
    3. QTextDocument document;
    4. document.setHtml(mTextComponent.getText());
    5. document.setTextWidth(width);
    6.  
    7. mHeight = document.size().height();
    8. return mHeight;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Here is a function to retrieve the height of an HTML text bounding rectangle.
    Unfortunately it's quite slow, when called repetitively.

    Do you have a better solution ?
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Html text bounding rectangle

    Unfortunately it has to be slow, because the document has to be laid out. Try reducing the number of iterations.

  3. The following user says thank you to wysota for this useful post:

    bunjee (13th May 2008)

  4. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Html text bounding rectangle

    Befor yo update the document.setTextWidth(width);
    check the exist width ... large image or table ..
    and its become slow if paitdevice go outside paint the width..

    QGraphicsTextItem , QTextEdit or QTextBrowser, use a private class name QTextControl
    this class is very large ....

    to paint and edit text you need only QTextDocument,QTextCursor,QAbstractTextDocumentLay out

    show and test http://www.qt-apps.org/content/show....?content=80234


    Qt Code:
    1. /* QTextDocument *_d; */
    2. QRectF TextWriter::boundingRect() const
    3. {
    4. if (_d) {
    5. _d->adjustSize(); ///// !!!!!!
    6. QAbstractTextDocumentLayout *Layout = _d->documentLayout();
    7. return Layout->frameBoundingRect(_d->rootFrame());
    8. } else {
    9. return QRectF();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to patrik08 for this useful post:

    bunjee (13th May 2008)

  6. #4
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Html text bounding rectangle

    Thanks for replying,
    I'm not sure I understood.

    What impementation do you propose ?

  7. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Html text bounding rectangle

    Qt Code:
    1. painter.setRenderHint(QPainter::Antialiasing, false);
    2.  
    3. QTextDocument & document = getDisplayDocument();
    4. QFont & font = getDisplayFont();
    5. font.setFamily("Arial");
    6. font.setPointSize(8);
    7. document.setDefaultFont(font);
    8. document.setHtml(text);
    9. ZeSmileyController::get()->InsertSmiley(document);
    10. document.setTextWidth(textRect.width());
    11.  
    12. QAbstractTextDocumentLayout::PaintContext & context = getDisplayContext();
    13. context.clip = QRect(0, 0, textRect.width(), textRect.height());
    14. painter.translate(textRect.x(), textRect.y());
    15. document.documentLayout()->draw(&painter, context);
    16.  
    17. painter.restore();
    To copy to clipboard, switch view to plain text mode 

    I've come up with that implementation to display an HTML text paragraph.

    I'm trying to display an ItemView of HTML items.

    The performance are terrible at this point. Anyone has any idea to optimize this ? The paint event simply cannot lay the document at every painting event.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Html text bounding rectangle

    Try caching as much as you can. If the width didn't change, there is no need to rebuild the layout. I remember implementing a rich text delegate and it worked fine, so yours should too. You can also take a look at QwwRichTextButton from wwWidgets, it uses a QTextDocument internally to do rich text rendering, maybe you can steal some of my code...

  9. #7
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Html text bounding rectangle

    Thanks wysota,

    I've take a look at QwwRichTextButton source code.
    Nice widget extension you've got here.

    The biggest difference between your code and mine is that you "cache" the QTextDocument in every QwwRichTextButton.

    In my case since I plan displaying like 10 000 items like this.
    So in a widget context it's working fine since you'll draw one or two of these.
    But in an massive itemview list you cannot do that.

    Or I have to create a "pool" of textdocument :/. That's a lot of work just to display html paragraphes.

    I guess if you can make a compilable example displaying an itemview of 10 000 paragrahes items without caching it I would shut my mouth .

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Html text bounding rectangle

    Quote Originally Posted by bunjee View Post
    In my case since I plan displaying like 10 000 items like this.
    So in a widget context it's working fine since you'll draw one or two of these.
    But in an massive itemview list you cannot do that.
    Yes, that's true, but you can still cache the results as pixmaps and only regenerate them when it is really needed.

    I guess if you can make a compilable example displaying an itemview of 10 000 paragrahes items without caching it I would shut my mouth .
    Sorry, no time to do that now

  11. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Html text bounding rectangle

    Pixmap buffering sounds like a good idea.

    Where should I start ?

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. HTML text drawn with QPainter::drawText()
    By MarkoSan in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 01:23
  3. QPushButton and HTML text
    By ivan.cukic in forum Qt Programming
    Replies: 2
    Last Post: 20th March 2007, 13:48
  4. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  5. Writing Text on a Rectangle!!!
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2006, 10:23

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.