本文介绍了QT5字体在各种平台上渲染不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想对一些自定义小部件渲染进行可重复的测试。为了做到这一点,我把它们绘制到一个QImage上,并将结果保存为PNG。在Windows上,输出与MacOSX完全不同。 我照顾了: 在所有平台上选择相同的字体(我提供TTF字体文件并将代码指向它) 绘制到QImage而不是QPixmap,如文档所述QImage画家应该是平台独立的 我也选择了Antialisating和TextAntialiasing提示 通过QFontDatabase :: font()请求字体,是指定的,而不是pixelSize 如何确保所有平台上的渲染完全相同,以便我的测试运行可重现?换句话说,是否可以迫使QT5在所有平台上使用相同的字体引擎(例如freetype)? ** 我把问题归结为一个简单的渲染测试程序。 所以代码如下所示: QFontDatabase fontDb; fontDb.addApplicationFont(... / fonts / Vera.ttf); QImage结果(宽度,高度,QImage :: Format_RGB32); QPainter画家(&结果); painter.setRenderHint(QPainter :: Antialiasing); painter.setRenderHint(QPainter :: TextAntialiasing); QBrush背景(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); Bitstream Vera字体可以在fontsquirel.com下载。 请参阅MacOSX(左)和Win32(右)上的结果,这两者是非常不同的: //i.stack.imgur.com/EXWeR.pngalt =在这里输入图片描述> 回答和评论N1ghtLight下面,并阅读他建议的链接后,我改变了代码,以获得字体: QFont ()font-font-font-font-family =font-size:font-family:font-family =font-family: b qreal RENDER_DPI = 72; int pixelSize =(int)((qreal)10 * screenDPI / RENDER_DPI); font.setPixelSize(pixelSize); $ b这似乎主要解决了字体大小不同的问题,至少在MacOSX上,字体现在正好是10个像素。在Windows上,虽然字体呈现很多更薄,也更小。我仍然迷茫和困惑... 这是新的结果(左MacOSX,正确的Windows)。白色比例表示真正的10个像素大小。 以下是G_G的回答,我修改了代码(Linux?Mobile平台是什么?复杂...)。现在Windows和MacOSX的输出字体都是10个像素,仍然有很大的不同(左边是MacOSX,右边是Windows)。 谢谢。解决方案 您的渲染DPI变量应该为96,Windows和72为OSX根据 b $ b 到: http://www.rfwilmut.clara.net/about/fonts .html 在Macintosh显示器上,名义分辨率为72点每英寸(dpi) ,所以一个72像素宽的图形在概念上将是1英寸宽的b $ b--尽管显然实际大小取决于单独的监视器。但是,它总是会打印一英寸宽。 但是在Windows显示器上,分辨率通常是96 dpi。这意味着,虽然图片仍然是72像素宽,它将在0.75英寸打印。 qreal screenDPI = QApplication :: primaryScreen() - > physicalDotsPerInch (); #ifdef WINDOWS qreal RENDER_DPI = 96; #else qreal RENDER_DPI = 72; #endif int pixelSize =(int)((qreal)10 * screenDPI / RENDER_DPI); font.setPixelSize(pixelSize); 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.I took care of :Selecting the same font on all platform (I provide the "TTF" font file and point the code to it)Drawing onto a QImage and not a QPixmap, as the documentation says QImage painter is supposed to be platform independantI also selected Antialisating and TextAntialiasing hintsRequesting the font via QFontDatabase::font() so that pointSize is specified and not pixelSizeHow 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) ?**I nailed down the issue to a simple rendering test program.So the code is looks like :QFontDatabase fontDb;fontDb.addApplicationFont(".../fonts/Vera.ttf");QImage result(width, height, QImage::Format_RGB32);QPainter painter(&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");The Bitstream Vera font can be downloaded on fontsquirel.com for instance.See the result on MacOSX (left) and on Win32 (right), which are very different:Following answer and comments by N1ghtLight below, and after reading the links he suggested, I changed the code to get the font to :QFont font = fontDb_->font(("Bitstream Vera Sans", "Normal", -1);qreal screenDPI = QApplication::primaryScreen()->physicalDotsPerInch();qreal RENDER_DPI = 72;int pixelSize = (int)((qreal)10 * screenDPI / RENDER_DPI);font.setPixelSize(pixelSize);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...Here is the new result (left MacOSX, right Windows). The white scale indicates true 10 pixels size.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).Thanks. 解决方案 your render DPI variable should be 96 for Windows and 72 for OSXaccording to:http://www.rfwilmut.clara.net/about/fonts.html On a Macintosh monitor, the notional resolution is 72 dots-per -inch (dpi), so that a graphic 72 pixels wide would notionally be 1 inch wide - though obviously the actual size would depend on the individual monitor. However it will always print one inch wide. But on a Windows monitor the resolution is (usually) 96 dpi. This means that though the picture is still 72 pixels wide, it will print at 0.75 inches.QFont font = fontDb_->font(("Bitstream Vera Sans", "Normal", -1);qreal screenDPI = QApplication::primaryScreen()->physicalDotsPerInch();#ifdef WINDOWSqreal RENDER_DPI = 96;#elseqreal RENDER_DPI = 72;#endifint pixelSize = (int)((qreal)10 * screenDPI / RENDER_DPI);font.setPixelSize(pixelSize); 这篇关于QT5字体在各种平台上渲染不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-04 22:54