title: QT5 font rendering different on various platforms
body: <p>I want to make reproductible tests of some custom widgets renderings. In order to do that, I paint them onto a QImage and save the result as PNG. The output is really different on Windows compared to MacOSX.</p>
  
  <p>I took care of :</p>
  
  <ul>
  <li>Selecting the same font on all platform (I provide the "TTF" font file and point the code to it) </li>
  <li>Drawing onto a QImage and not a QPixmap, as the documentation says QImage painter is supposed to be platform independant </li>
  <li>I also selected Antialisating and TextAntialiasing hints</li>
  <li>Requesting the font via QFontDatabase::font() so that pointSize is specified and not pixelSize</li>
  </ul>
  
  <p>How can I make sure the rendering is exactly the same on all platforms so that my test runs are reproductible ? In other words, is it maybe possible to force QT5 to use the same font engine on all platforms (for instance freetype) ?</p>
  
  <p>**</p>
  
  <p>I nailed down the issue to a simple rendering test program.
  So the code is looks like :</p>
  
  <pre><code>QFontDatabase fontDb;
  fontDb.addApplicationFont(".../fonts/Vera.ttf");
  
  QImage   result(width, height, QImage::Format_RGB32);
  QPainter painter(&amp;result);
  painter.setRenderHint(QPainter::Antialiasing);
  painter.setRenderHint(QPainter::TextAntialiasing);
  
  
  QBrush background(QColor(205, 205, 205));
  painter.fillRect(0, 0, 800, 600, background);
  
  QFont font = fontDb.font("Bitstream Vera Sans", "Normal", 10);
  painter.setFont(font);
  
  painter.setPen(QColor(0, 0, 0));
  painter.drawText(10, 10, "ABCD abcd 01234567");
  </code></pre>
  
  <p>The Bitstream Vera font can be downloaded on fontsquirel.com for instance.</p>
  
  <p>See the result on MacOSX (left) and on Win32 (right), which are very different:</p>
  
  <p><img src="https://i.stack.imgur.com/EXWeR.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>Following answer and comments by N1ghtLight below, and after reading the links he suggested, I changed the code to get the font to :</p>
  
  <pre><code>QFont font = fontDb_-&gt;font(("Bitstream Vera Sans", "Normal", -1);
  
  qreal screenDPI  = QApplication::primaryScreen()-&gt;physicalDotsPerInch();
  qreal RENDER_DPI = 72;
  
  int pixelSize = (int)((qreal)10 * screenDPI / RENDER_DPI);
  font.setPixelSize(pixelSize);
  </code></pre>
  
  <p>This seems to mostly solve the problem of the font of very different size. At least on MacOSX, the font is exactly 10 pixels high now. On Windows though the font renders much thinner and a bit smaller too. I'm still lost and confused...</p>
  
  <p>Here is the new result (left MacOSX, right Windows). The white scale indicates true 10 pixels size.</p>
  
  <p><img src="https://i.stack.imgur.com/XnoXu.png" alt="enter image description here"></p>
  
  <hr>
  
  <p>Following answer by G_G below I adapted the code (what about Linux ? Mobile platforms ? This gets very complicated...). Now the fonts are 10 pixels in the output on both Windows and MacOSX, still the rending remains very different (still MacOSX on the left, Windows on the right).</p>
  
  <p><img src="https://i.stack.imgur.com/7EVFz.png" alt="enter image description here"></p>
  
  <p>Thanks.</p>