Search code examples
pyqtqt-designer

QtDesigner being overzealous with HTML tags when formatting as richtext?


In plain code, a QLabel can be made bold by something as simple as:

myLabel = QtGui.QLabel("<strong>My bold label!</strong>")

However, QtDesigner insists on using:

myLabel = QLabel('<html><head/><body><p><span style=" font-weight:600;">My bold label!</span></p></body></html>')

Is this actually necessary, or is it another case of a WYSIWYG editor being massively overzealous?


Solution

  • The extra markup will only be added if you use the Rich Text tab of the editing dialog. Your own markup will be preserved verbatim if you:

    • directly edit the label (i.e. via double-click or pressing F2)
    • add it to the line-edit of the text property
    • use the Source tab of the editing dialog

    EDIT:

    To answer the question of whether the full markup is strictly necessary: no, not really. According to the Qt docs on the Supported HTML Subset, it is compliant with the HTML 4 spec. So, as with any reasonably competent html-rendering engine, it should have no difficulty dealing with an html document containing only the fragment <strong>My bold label!</strong>.