Results 1 to 8 of 8

Thread: line spaceing in QTextDocument

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default line spaceing in QTextDocument

    Following my previous post, i`d need custom line spacing in a QTextDocument.

    No proper solution was found here:
    http://www.qtcentre.org/threads/1929...t-line-spacing

    But after the post, an addition was supposedly made to Qt in 4.8.0:
    https://bugreports.qt-project.org/browse/QTBUG-14470

    Status: Closed Closed
    Priority: P2: Important P2: Important
    Resolution: Done
    Affects Version/s: 4.7.0
    Fix Version/s: 4.8.0
    However, the thread fails to mention what exactly was added/changed, and i am unable to find anything in Qt 4.8.1. :/

    I figured i am supposed to layout every single paragraph in the document by subclassing this:
    http://doc.qt.nokia.com/4.7-snapshot...entlayout.html

    but i still wouldnt know how to change the line space...


    Funny, Qt offers so much awesomeness, and then it`s these little things that you are left spending time on...

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

    Toniy (2nd November 2016)

  3. #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: line spaceing in QTextDocument

    Hi,

    you can iterate through the blocks of QTextDocument and set the line spacing with QTextBlock::blockFormat() + QTextBlockFormat::setLineHeight().

  4. The following user says thank you to Lykurg for this useful post:

    Toniy (2nd November 2016)

  5. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: line spaceing in QTextDocument

    thanks!

    It wont work though...
    I decided to go bruteforce onthis:


    Qt Code:
    1. testform::testform(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5.  
    6.  
    7. ui.textEdit->setText("asdfasdfasdf\r\nbbbbbbbbbbbbbbbbbbbb\r\nccccccccccccccccc\r\nddddddddddddddddddf");
    8.  
    9. for(int i=0; i<ui.textEdit->document()->blockCount(); i++)
    10. {
    11. ui.textEdit->document()->findBlockByNumber(i).blockFormat().setLineHeight(12, QTextBlockFormat::LineDistanceHeight);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    But it doesnt work.

  6. The following user says thank you to tuli for this useful post:

    Toniy (2nd November 2016)

  7. #4
    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: line spaceing in QTextDocument

    Of course it does not work. You have to set the modified format back to the document! blockFormat() simply returns a copy of the format, no pointer nor reference!
    Qt Code:
    1. te.setText("asdfasdfasdf\nbbbbbbbbbbbbbbbbbbbb\nccccccccccccccccc\nddddddddddddddddddf");
    2. QTextCursor c(te.document());
    3. c.movePosition(QTextCursor::Start);
    4. do {
    5. QTextBlockFormat f = c.blockFormat();
    6. f.setLineHeight(12, QTextBlockFormat::LineDistanceHeight);
    7. c.setBlockFormat(f);
    8. } while (c.movePosition(QTextCursor::NextBlock));
    9. te.show();
    To copy to clipboard, switch view to plain text mode 

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

    Toniy (2nd November 2016)

  9. #5
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: line spaceing in QTextDocument

    ouch...yeah, that was it. That makes a great second post, doesnt it...

    So now i have this code to render a document to the screen:

    Qt Code:
    1. painter->translate(x, y);
    2. document()->setTextWidth(w);
    3. document()->setDefaultFont(painter->font());
    4.  
    5.  
    6. QTextCursor c(document());
    7. c.movePosition(QTextCursor::Start);
    8. do {
    9. QTextBlockFormat f = c.blockFormat();
    10. f.setLineHeight(h, QTextBlockFormat::LineDistanceHeight);
    11. c.setBlockFormat(f);
    12. } while (c.movePosition(QTextCursor::NextBlock));
    13.  
    14.  
    15. document()->drawContents(painter, QRect(0,0,w,h));
    To copy to clipboard, switch view to plain text mode 

    This works perfectly for single-line strings, but not when strings with linebreaks in it have been assigned:
    Qt Code:
    1. document().setPlainText("aaaaa\nbbbbbbb\ncccccc");
    To copy to clipboard, switch view to plain text mode 

    any idea?

  10. The following user says thank you to tuli for this useful post:

    Toniy (2nd November 2016)

  11. #6
    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: line spaceing in QTextDocument

    Quote Originally Posted by tuli View Post
    any idea?
    Yes. The second parameter of drawContents clips the painting. So you have to alter "h" when you have more lines than one or just leave it empty.

  12. The following user says thank you to Lykurg for this useful post:

    Toniy (2nd November 2016)

  13. #7
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: line spaceing in QTextDocument

    h provide sufficient space, and

    document()->drawContents(painter);
    provides same result... :/

  14. The following user says thank you to tuli for this useful post:

    Toniy (2nd November 2016)

  15. #8
    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: line spaceing in QTextDocument

    Then please make a minimal compile able example which illustrate your problem.

  16. The following user says thank you to Lykurg for this useful post:

    Toniy (2nd November 2016)

Similar Threads

  1. QTextDocument - line spacing
    By mazurekwrc in forum Qt Programming
    Replies: 5
    Last Post: 25th February 2011, 00:23
  2. Horizontal line in QTextDocument
    By Ginsengelf in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2011, 10:37
  3. Getting start index and length of a line in QTextDocument
    By slandvogt in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2010, 12:13
  4. QTextDocument and line height
    By vooft in forum Qt Programming
    Replies: 0
    Last Post: 25th November 2008, 17:47
  5. QTextDocument line number
    By bunjee in forum Qt Programming
    Replies: 14
    Last Post: 10th June 2008, 14:49

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.