简体   繁体   中英

Set line spacing in QTextEdit

I want to set the line spacing of a QTextEdit.

It's no problem to get that information with

QFontMetrics::lineSpacing();

But how to set that?

I tried with StyleSheets, but that didn't work:

this->setStyleSheet("QTextEdit{ height: 200%; }");

or

this->setStyleSheet("QTextEdit{ line-height: 200%; }");

Partial solution:

Well, I've found a solution - not the way I wanted it, but at least it's simple and it gives nearly my intended behavior, enough for my proof of concept.

On every new line there's some linespacing. But if you just type until the text is automatically wrapped to a new line you wont have line-spacing between this two lines. This hack only works with text blocks, see the code.

Just keep in mind it's brute force and a ugly hack. But it provides some kind of line-spacing to your beautiful QTextEdit. Call it everytime your text changes.

void setLineSpacing(int lineSpacing) {
    int lineCount = 0;
    for (QTextBlock block = this->document()->begin(); block.isValid();
            block = block.next(), ++lineCount) {
        QTextCursor tc = QTextCursor(block);
        QTextBlockFormat fmt = block.blockFormat();
        if (fmt.topMargin() != lineSpacing
                || fmt.bottomMargin() != lineSpacing) {
            fmt.setTopMargin(lineSpacing);
            //fmt.setBottomMargin(lineSpacing);
            tc.setBlockFormat(fmt);
        }
    }
}

The QFontMetrics contains (per the name) static properties that come from the font file. How wide a capital "C" is, etc. lineSpacing() gets you the natural distance in single-spacing that the person who designed the font encoded into the font itself. If you actually wanted to change that (you don't)...the somewhat complicated story of how is told here:

http://fontforge.sourceforge.net/faq.html#linespace

As for the line spacing in a QTextEdit...it looks (to me) like that is seen as one of the things that falls under Qt's extensibility model for specifying text "layouts":

http://doc.qt.io/qt-4.8/richtext-layouts.html

You would supply your own layout class to the QTextDocument instead of using the default. Someone tried it here but did not post their completed code:

http://www.qtcentre.org/threads/4198-QTextEdit-with-custom-space-between-lines

Applying blockformat to entire document rather than each line works.

QTextBlockFormat bf = this->textCursor().blockFormat();
bf.setLineHeight(lineSpacing, QTextBlockFormat::LineDistanceHeight) ;
this->textCursor().setBlockFormat(bf);

I know this is an old question, but I've spent a lot of time today trying to solve this for PyQt5 5.15.2. I'm posting my solution in case it is useful to others. The solution is for Python, but should be easily transferable.

The following code will change the line height to 150% for a populated QTextEdit widget in one go. Further editing will pick up the current block format, and continue applying it. I've found it to be very slow for large documents though.

textEdit = QTextEdit()

# ... load text into widget here ...

blockFmt = QTextBlockFormat()
blockFmt.setLineHeight(150, QTextBlockFormat.ProportionalHeight)
    
theCursor = textEdit.textCursor()
theCursor.clearSelection()
theCursor.select(QTextCursor.Document)
theCursor.mergeBlockFormat(blockFmt)

I have translated Jadzia626 's code to C++ and it works. Here is the information about setLineHeight()

qreal lineSpacing = 35;
QTextCursor textCursor = ui->textBrowser->textCursor();

QTextBlockFormat * newFormat = new QTextBlockFormat();
textCursor.clearSelection();
textCursor.select(QTextCursor::Document);
newFormat->setLineHeight(lineSpacing, QTextBlockFormat::ProportionalHeight);
textCursor.setBlockFormat(*newFormat);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM