简体   繁体   English

Linux上QPainter :: drawText的性能不佳

[英]Bad Performance of QPainter::drawText on Linux

I have noticed that QPainter::drawText is horribly slow on Linux when using it with a scaled window mapping. 我注意到,当使用缩放窗口映射时,QPainter :: drawText在Linux上非常慢。 Is there anything I can do about this? 我能做些什么吗? I already checked whether disabling anti-aliasing or enabled the raster-renderer makes a difference, but it doesn't. 我已经检查过禁用消除锯齿或启用光栅渲染器是否有所作为,但事实并非如此。

Example: When using a viewport size of (450px, 200px), a window size of factor 100 (45000, 20000) and thus font sizes scaled up by factor 100 as well (1400pt), rendering 30 times the text "hello" takes about 4(!) seconds on Linux - both on OpenSuse and Ubuntu. 示例:当使用视口大小(450px,200px)时,窗口大小为因子100(45000,20000),因此字体大小按比例缩放100倍(1400pt),渲染文本“hello”的30倍左右在Linux上4(!)秒 - 在OpenSuse和Ubuntu上。

The same sample renders in a snap on Windows and Mac. 相同的示例在Windows和Mac上快速呈现。

Just for clarification: although the font size is scaled up, the text appears in "normal" size on screen due to the described window<->viewport mapping. 只是为了澄清:虽然字体大小按比例放大,但由于所描述的窗口< - >视口映射,文本在屏幕上显示为“正常”大小。

Here is the simple sample code I am using: 这是我正在使用的简单示例代码:

void Widget::paintEvent(QPaintEvent *event)
{
    const int scaleFactor = 100;

    QPainter painter(this);

    // Setup font
    QFont font;
    font.setPointSize(14*scaleFactor);
    painter.setFont(font);

    // Setup mapping
    painter.setWindow(0, 0, width() * scaleFactor, height() * scaleFactor);

    // Render the text
    for (int i = 0; i < 30; i++)
        painter.drawText(qrand() % (width() * scaleFactor), qrand() % (height() * scaleFactor), "Hello");
}

Any help would be awesome. 任何帮助都是极好的。

Note: I am using Qt 4.8.5 注意:我使用的是Qt 4.8.5

This question is quite old but as be Qt bug still seems to be unresolved here we go... 这个问题已经很老了但是因为Qt bug似乎仍未解决,所以我们去...

Not sure if this might be an option but in two projects I worked for we implemented labels which internally rendered into a pimap/image first which was then drawn. 不确定这可能是一个选项,但在我工作的两个项目中,我们实现了标签,这些标签首先在内部渲染为pimap / image然后绘制。 So caching your text in an image whith transparent background should solve the problem. 因此,在透明背景的图像中缓存文本应该可以解决问题。

I do not think it makes a difference here, but you might also check if QStaticText has a beneficial influence on performance in your case. 我不认为它在这里有所作为,但您也可以检查QStaticText是否对您的案例中的性能产生有益影响。

Problem found! 发现问题!

The FontConfig developer libraries where not installed on my Linux system. 我的Linux系统上没有安装的FontConfig开发人员库。 This caused Qt to be built against XLFD, which obviously doesn't work well with scaled mappings (see report above). 这导致Qt针对XLFD构建,这显然不适用于缩放映射(参见上面的报告)。

After installing the FontConfig dev libs and rebuilding Qt the text now gets rendered nice and fast. 在安装了FontConfig dev libs并重建Qt之后,文本现在变得非常快速。 I did additionally specify the "-fontconfig" parameter when rebuilding Qt, just to be sure, but according to the Qt guys this shouldn't be necessary. 我确实在重建Qt时另外指定了“-fontconfig”参数,但是根据Qt的说法,这不应该是必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM