How to set QTextEdit formatting to preserve quote indentation, superscripts, and subscripts?

72 Views Asked by At

I'm working on making an application that has a rich text editor that saves in Markdown format. As Markdown obviously isn't capable of displaying many rich text features, I'm limiting the formatting features I expose to only just those that are compatible with GitHub Flavored Markdown (the default Markdown format supported by QTextEdit, and one which has a couple nice features I'd like).

The problem I'm having is that not all of the formatting that ought to be compatible with GFM is getting preserved. Bold, italic, and strikethrough all seem to be preserved when I use QTextEdit::toMarkdown(), but quote blocks (which are an essential feature for me) are not preserved for some reason.

Currently the way I'm indenting a block of text is to use code similar to this:

void MainWindow::applyBlockFormat(QTextBlockFormat format)
{
    ui->documentTextEdit->textCursor().setBlockFormat(format);
}

...

QTextBlockFormat format;
format.setIndent(1);
applyBlockFormat(format);

(for clarity, ui is my application's main window object, while documentTextEdit is a QTextEdit object.)

This works perfectly fine in the rich text editor, but when I actually save the document (using QTextEdit::toMarkdown() to get a QString with the document's contents), I can open the saved file and see that all indented text is not prefixed with a > marker like I would expect. It seems to be treated exactly as if it weren't indented.

At this point I tried to debug by writing a Markdown file by hand with a quote block, then loading it using QTextEdit::setMarkdown(). The quote block rendered as I expected (indented), however after inspecting things with some qDebug() statements I saw that the block's left margin was set to 40 rather than its indent being set to 1. So I went and changed my code so that it used format.setLeftMargin(40) instead of format.setIndent(1), then tried writing a file with indented text in it again. The saved file still did not end up with a quote block marker (>).

Finally, the last odd thing. If I load a file that has block quotes, modify it, save it, and then open the resulting file in a text editor, I can see that file does have quote blocks in it. I can even type a newline followed by some text in an indented block, and it will make a new indented line (complete with quote block marker).

What setting am I missing here? I've tried setting the Qt::BlockQuoteLevel property on the format object to 1, to no avail.

I'm using Qt 5.15.3 on Kubuntu 22.04 LTS.

0

There are 0 best solutions below