Results 1 to 3 of 3

Thread: How obtain QTextDocument height ?

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How obtain QTextDocument height ?

    Hi,
    I'm trying to paint different blocks of html text in a column; the text to be written is read from a database so it changes at runtime; the text must fit the column's width so it must wordwrap.

    For obtaing this i have used QTextDocument and its ability to draw html text; since QTextDocument doesn't have a function which returns the final height I've used a code (by marcel) found in an other thread:

    Qt Code:
    1. QFontMetrics fm(Strings::proconsFont());
    2. float rw = float(columnWidth);
    3. float tw = fm.width(text);
    4. float ratio = tw/rw;
    5. int lines = std::ceil(ratio);
    6. QAbstractTextDocumentLayout::PaintContext context;
    7. doc.setDefaultFont(Strings::proconsFont());
    8. doc.setTextWidth(columnWidth);
    9. doc.setHtml(text);
    10. doc.documentLayout()->draw(painter, context);
    11. painter->translate(0, lines*fm.height());
    To copy to clipboard, switch view to plain text mode 

    This works quite good but sometime the last row of a block and the first of that just below overlap.

    QTextDocument organizes the painted text not as is supposed with the lines trick:

    Qt Code:
    1. |-------cloumnWidth------|
    2. this is a very long senten
    3. ce wrapped in two rows.
    To copy to clipboard, switch view to plain text mode 

    but:

    Qt Code:
    1. |-------cloumnWidth------|
    2. this is a very long
    3. sentence wrapped in two
    4. rows.
    To copy to clipboard, switch view to plain text mode 

    So, instead of two rows the text is painted in three and cause overlaps with the subsequent block that is painted at 2*fm.height() instead of 3*fm.height() (vertically).

    Is there a different manner for obtaining QTextDocument height? Or is there a totally different approach not involving QTextDocument?

    Thanks, and sorry for my english.

    Giuseppe
    Giuseppe CalÃ

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

    Default Re: How obtain QTextDocument height ?


  3. The following 2 users say thank you to jacek for this useful post:

    jiveaxe (26th October 2007), Toniy (1st April 2016)

  4. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How obtain QTextDocument height ?

    Thank you jacek,
    you tips work perfectly; doc.documentLayout()->documentSize().height() returns the desired value.

    Regards
    Giuseppe CalÃ

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

    Toniy (1st April 2016)

Similar Threads

  1. How can I get the Systray height ?
    By Nyphel in forum Qt Programming
    Replies: 3
    Last Post: 12th March 2007, 09:04
  2. height()
    By mickey in forum Newbie
    Replies: 1
    Last Post: 22nd March 2006, 20:32

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.