title: Custom font in QTextDocument
body: <p>I'm attempting to generate a PDF file from <code>QTextDocument</code>, which is working just fine. The issue comes when I want to use a custom font for just a specific <code>&lt;p&gt;&lt;/p&gt;</code> object. I tried using the standard means of <code>QFontDatabase::addApplicationFont()</code> which successfully gets the font loaded into the application, but for some reason it writes all of the <code>QString</code> based variables that are added to <code>messageBody</code> in this font instead of that specified by <code>QTextFocument.setDefaultFont()</code>, or even a manually via <code>style={font-family:}</code>.</p>
  
  <p>Am I doing this wrong, or overlooking something here, or is this some kind of bug?</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QPrinter&gt;
  #include &lt;QTextDocument&gt;
  #include &lt;QFontDatabase&gt;
  #include &lt;QFile&gt;
  #include &lt;QDebug&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc,argv);
  
          int id =  QFontDatabase::addApplicationFont(":/BarcodeAV.ttf");
          qDebug()&lt;&lt; "font"&lt;&lt; QFontDatabase::applicationFontFamilies(id)[0];
          QString serialNumber = "ABC123";
          QString messageBody;
          messageBody += "&lt;p style='font-size: large;'&gt;&lt;b&gt;Serial Number:&lt;/b&gt; " + serialNumber + "&lt;/p&gt;";
          messageBody += "&lt;p style='font-size: large; font-family=\"Console\"'&gt;&lt;b&gt;Serial Number:&lt;/b&gt; " + serialNumber + "&lt;/p&gt;&lt;/p&gt;";
  
          QTextDocument document;
          document.setHtml(messageBody);
  
          QPrinter printer(QPrinter::PrinterResolution);
          printer.setOutputFormat(QPrinter::PdfFormat);
          printer.setOutputFileName("test.pdf");
  
          document.print(&amp;printer);
  
          return 0;
  }
  </code></pre>
  
  <p><hr> 
  The output PDF looks like so:  </p>
  
  <p><a href="https://i.stack.imgur.com/DEWVc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DEWVc.png" alt="enter image description here"></a></p>
  
  <p>I can't wrap my head around why/how it would differentiate the base QString (messageBody) from those that are concatenated into it (serialNumber)?  </p>
  
  <hr>  
  
  <p><strong>Edit::</strong> No change in output after attempting to implement the style.css solution suggested below  </p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QPrinter&gt;
  #include &lt;QTextDocument&gt;
  #include &lt;QFontDatabase&gt;
  #include &lt;QFile&gt;
  #include &lt;QDebug&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc,argv);
  
          int id =  QFontDatabase::addApplicationFont(":/BarcodeAV.ttf");
          qDebug()&lt;&lt; "font"&lt;&lt; QFontDatabase::applicationFontFamilies(id)[0];
          QString serialNumber = "ABC123";
          QString messageBody;
          messageBody += "&lt;p style='font-size: large;'&gt;&lt;b&gt;Serial Number:&lt;/b&gt; " + serialNumber + "&lt;/p&gt;";
          messageBody += "&lt;p style='font-size: large;'&gt;&lt;b&gt;Serial Number:&lt;/b&gt;  &lt;span id=\"barcode\"&gt;" + serialNumber + "&lt;/span&gt;&lt;/p&gt;";
  
          QTextDocument document;
          QFile file(":/style.css");
          if(file.open(QFile::ReadOnly|QFile::Text)){
              qDebug() &lt;&lt; "Opened CSS";
          }
          else{
              qDebug() &lt;&lt; "Failed to open CSS:" &lt;&lt; file.errorString();
          }
          document.setDefaultStyleSheet(file.readAll());
          document.setHtml(messageBody);
  
          QPrinter printer(QPrinter::PrinterResolution);
          printer.setOutputFormat(QPrinter::PdfFormat);
          printer.setOutputFileName("test.pdf");
  
          document.print(&amp;printer);
  
          return 0;
  }
  </code></pre>
  
  <p><strong>style.css</strong></p>
  
  <pre><code>@font-face {
  font-family: K-O-D-E-39 Hidden;
  src: url(':/BarcodeAV.ttf');
  }
  
  #barcode{
  font-family: 'K-O-D-E-39 Hidden';
  font-size: larger;
  }
  </code></pre>