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);
        }
    }
}
Edit
Report