QLabel font differs when inserting a short HTML text
<p>I have noticed that the font size of a <code>QLabel()</code> in a PyQt GUI is not very consistent. Let's take a look at the following example. I've written a complete python file for a quick test. It will pop up a Qt window with two labels:</p>

<pre><code>from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import os

'''---------------------------------------------------------------------'''
'''                                                                     '''
'''                        T E S T I N G                                '''
'''                                                                     '''
'''---------------------------------------------------------------------'''


class TestWindow(QMainWindow):
    def __init__(self):
        super(TestWindow, self).__init__()

        # 1. Set basic geometry and color.
        # --------------------------------
        self.setGeometry(100, 100, 800, 600)
        self.setWindowTitle('Hello World')
        palette = QPalette()
        palette.setColor(QPalette.Window, QColor(200,200,200))
        self.setPalette(palette)
        self.show()

        # 2. Create the central frame.
        # ----------------------------
        self.centralFrame = QFrame(self)
        self.centralFrame.setFrameShape(QFrame.NoFrame)
        self.centralLayout = QVBoxLayout()
        self.centralFrame.setLayout(self.centralLayout)
        self.centralLayout.setSpacing(5)
        self.centralLayout.setContentsMargins(20,20,20,20)
        self.setCentralWidget(self.centralFrame)

        # 3. Do the test.
        # ----------------
        # TEST CASE 1
        # The label with html
        self.infoLbl = QLabel()
        self.infoLbl.setTextFormat(Qt.RichText)
        self.infoLbl.setFrameShape(QFrame.StyledPanel)
        self.infoTxt = \
'&lt;html&gt; \
&lt;head&gt; \
&lt;/head&gt; \
&lt;body&gt; \
&lt;font size="10"&gt; \
&lt;p style="margin-left:8px;"&gt;My html text, font = 10pt&lt;/p&gt; \
&lt;/font&gt; \
&lt;/body&gt; \
&lt;/html&gt; '
        self.infoLbl.setText(self.infoTxt)
        self.infoLbl.setMaximumHeight(50)
        self.infoLbl.setMaximumWidth(500)

        # TEST CASE 2
        # The label with a normal string
        self.normalLbl = QLabel()
        self.normalLbl.setFrameShape(QFrame.StyledPanel)
        self.normalLbl.setText('My normal text, font = 10pt')
        font = QFont()
        font.setFamily("Arial")
        font.setPointSize(10)
        self.normalLbl.setFont(font)
        self.normalLbl.setMaximumHeight(50)
        self.normalLbl.setMaximumWidth(500)

        # 4. Add both labels to the central layout.
        # ------------------------------------------
        self.centralLayout.addWidget(self.infoLbl)    # &lt;- The label with html snippet
        self.centralLayout.addWidget(self.normalLbl)  # &lt;- The normal label
        self.centralLayout.addWidget(QLabel())        # &lt;- Just a spacer


if __name__== '__main__':
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Fusion'))
    testWindow = TestWindow()
    app.exec_()
    app = None
</code></pre>

<p>If you run this code, this is what you get:</p>

<p><a href="http://i.stack.imgur.com/mjSbX.png" rel="nofollow"><img src="http://i.stack.imgur.com/mjSbX.png" alt="Font size issue in QLabel"></a></p>

<p>Why are the font sizes in both labels not equal?</p>

<p>For completeness, this is my system:</p>

<ul>
<li>Operating system: Windows 10, 64-bit</li>
<li>Python version: v3 (anaconda package)</li>
<li>Qt version: PyQt5</li>
</ul>

<p>As you've already discovered it seems you need to use style instead. More info about it here:</p>

<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font</a></li>
<li><a href="http://www.w3schools.com/tags/tag_font.asp" rel="nofollow">http://www.w3schools.com/tags/tag_font.asp</a></li>
</ul>

<p>It says:</p>

<blockquote>
  <p>This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.</p>
</blockquote>

<p>and </p>

<blockquote>
  <p>The  tag is not supported in HTML5. Use CSS instead.</p>
</blockquote>

python html qt pyqt pyqt5