Qt World Summit: Submit your Presentation

[Solved] QTextDocument::setHtml() changes font when setting toHtml() output

  • I have some HTML from a QML TextArea (part of qt-components) using Richttext formatting.

    On my c++ side, I have some code which creates a QTextDocument and calls setHtml() with that text. When calling toHtml() immediately on the same document, the output has different font settings than the input

    QTextDocument document;

    qDebug() << d->inputText;
    qDebug() << "------------";
    qDebug() << document.toHtml();


    Output (as image since Qt devnet cleans out html even in code tags):

    As you can see, the default font changed from "Nokia Pure Text" to "Sans Serif" and it created a new span :(

    I also tried QTextDocument::setDefaultFont() with Nokia Pure Text before and after the setHtml() call, but it does not change anything. How do I prevent QTextDocument from changing the font?

  • Ok, solved it ...

    It seems like you have to set the default font for QTextEdit because it won't read it from HTML. Don't know why it didn't work for me yesterday. The span creation can't be prevented.

Log in to reply