+ Reply to Thread
Results 1 to 5 of 5

Thread: QTextEdit with custom space between lines

  1. #1
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextEdit with custom space between lines

    Hi,
    I cannot figure it out how to have a TextEdit area where I can specify the space between the lines, I could subclass a QWidget and calculate every single line and paint it with drawText.
    I think there's a better way to do that with TextLayout but I don't know how to use it, any help is appreciated
    Thanks in advance
    Bye

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,258
    Thanks
    3
    Thanked 4,706 Times in 4,507 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QTextEdit with custom space between lines

    You have to do your own layouting. If you take a look at QTextLayout docs, there is a pseudo-code snippet that demonstrates how the layout is done. It contains references to line height. If you want more light between lines, you have to adjust the "height" variable there and relayout lines.

  3. #3
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit with custom space between lines

    Thanks for the tip,
    I created a widget with the TextLayout and it works fine, but I noticed that newline are ignored by QtextLayout, so if I do:

    Qt Code:
    1. QFontMetrics fontMetrics(QApplication::font());
    2.  
    3. textLayout.setText("Test \n \n Hello");
    4.  
    5. int leading = fontMetrics.leading();
    6. int height = 0;
    7. qreal widthUsed = 0;
    8. textLayout.beginLayout();
    9. while (1) {
    10. QTextLine line = textLayout.createLine();
    11. if (!line.isValid())
    12. break;
    13.  
    14. line.setLineWidth(width());
    15. height += leading;
    16. line.setPosition(QPoint(0, height));
    17. height += line.height()+space;
    18. widthUsed = qMax(widthUsed, line.naturalTextWidth());
    19. }
    20. textLayout.endLayout();
    To copy to clipboard, switch view to plain text mode 

    when drawing there's no white lines between Test and Hello but they're both written on the same line,

    is there a way to make Qtetxlayout consider \n ?

    thanks
    Bye

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,258
    Thanks
    3
    Thanked 4,706 Times in 4,507 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QTextEdit with custom space between lines

    I don't think so. QTextLayout is used to lay out a single paragraph of text, so it won't handle multiple paragraphs by itself. I think you need to break text into paragraph and iterate over them with QTextLayout yourself.

    Sorry for the long delay.

  5. #5
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit with custom space between lines

    thanks for replying, I'll try to spilt my text into paragraphs, I just thought there was something similar already done in Qt, maybe with the oppurtunity to format the text style using Html just like in QTextEdit.

+ Reply to Thread

Similar Threads

  1. space between lines in QTextEdit
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 19th September 2006, 13:05
  2. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2006, 23:11

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