简体   繁体   中英

HTML inserted into QTextEdit does not conform to style sheet

I have a docked QTextEdit which I am using to emulate a debug terminal in a QT c++ gui, and have it set to a black background with white text.

I am trying to use it to print out error messages from QXmlSchemaValidator , but the messages from the schema validator are in html format, and whenever I insert them into the QTextEdit , it reverts to it's default font, and I end up with black text on a black background.

The actual message is something like:

    <html xmlns='http://www.w3.org/1999/xhtml/'>
    <body>
        <p>Content of element 
            <span class='XQuery-keyword'>minValue</span> does not match its type definition: <span class='XQuery-data'>fu</span> is not valid according to <span class='XQuery-type'>xs:decimal</span>..
        </p>
</body>
</html>

using setAcceptRichText(false) doesn't solve the problem, and if I use insertPlainText() to add text to the lineEdit , it removes all the line breaks and leaves the html tags in the error message, which is unacceptable.

Is there some way I can display the HTML rich text, but without blowing away my style sheet font?

I tested it on my computer. It works perfectly. Use this:

edit->setHtml(htmlDescription);
edit->selectAll();
edit->setTextColor(Qt::green);

//Ok, but clear selection
QTextCursor cur = edit->textCursor();
cur.clearSelection();
edit->setTextCursor(cur);

It's sort of a kludge, but the workaround that I went with was to remove the HTML tags from the schema validator message before adding it to the text edit.

QString errorMsg = msg.statusMessage().remove(QRegExp("<[^>]*>"));
textEdit->setText(errorMsg);

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