简体   繁体   English

QPainter带有小数点大小的drawText

[英]QPainter drawText with fractional pointsizes

Is there a way to draw text with fractional point sizes in Qt 5. I'm trying to use QFont::setPointSizeF() but it doesn't seem to work on any platform I tried on (mac/linux/windows) and point size is always rounded. 有没有一种方法可以在Qt 5中绘制带有小数点大小的文本。我正在尝试使用QFont::setPointSizeF()但它似乎不适用于我在(mac / linux / windows)上尝试过的任何平台并指向大小总是四舍五入。

QFontDatabase::isScalable and QFontDatabase::isSmoothlyScalable returns true for the font in all cases. QFontDatabase::isScalableQFontDatabase::isSmoothlyScalable在所有情况下QFontDatabase::isSmoothlyScalable返回true

I tried setting various QFont::fontHintingPreference and QPainter::RenderHint . 我尝试设置各种QFont::fontHintingPreferenceQPainter::RenderHint

I might be able to work around this using QFont::setPixelSize and QPainter::scale , but seem odd that QFont::setPointSizeF is broken?! 我也许可以使用QFont::setPixelSizeQPainter::scale解决此问题,但是QFont::setPointSizeF坏了似乎很奇怪?

Am I missing something or doing something wrong? 我是否缺少某些东西或做错了什么?

Simple program that shows the problem: 显示问题的简单程序:

#include <QtWidgets>

class MyWidget : public QWidget
{
public:
    MyWidget() : QWidget(0)
    {
    }

protected:
    void paintEvent(QPaintEvent */*e*/)
    {
        QPainter p(this);
        int y=10;

        for (qreal i = 10; i < 20; i += 0.2) {
            QFont font("Times"); // or any font font in the system
            font.setPointSizeF(i);
            p.setFont(font);
            p.drawText(1, y, QString("This should be point size %1 but is %2!").arg(font.pointSizeF()).arg(QFontInfo(font).pointSizeF()));
            y += i;
        }
    }
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    MyWidget widget;
    widget.resize(400, 740);
    widget.show();
    return app.exec();
}

This is not an unexpected behavior. 这不是意外的行为。 See the lines below: 请参阅以下几行:

"This should be point size 10 but is 9.75!"
"This should be point size 10.2 but is 10.5!"
"This should be point size 10.4 but is 10.5!"
"This should be point size 10.6 but is 10.5!"
"This should be point size 10.8 but is 10.5!"
"This should be point size 11 but is 11.25!"
"This should be point size 11.2 but is 11.25!"
"This should be point size 11.4 but is 11.25!"
"This should be point size 11.6 but is 11.25!"
"This should be point size 11.8 but is 12!"
"This should be point size 12 but is 12!"
"This should be point size 12.2 but is 12!"
...

Then, check also the documentation: 然后,还检查文档:

Sets the point size to pointSize. The point size must be greater than zero. The requested precision may not be achieved on all platforms.

It seems that Qt calculates pixel size based on point size and that ends up at QFont::setPixelSize which takes int as parameter so it gets rounded (or something like that). 看来Qt会根据点的大小来计算像素大小,最后以QFont :: setPixelSize结束,它以int作为参数,因此会四舍五入(或类似的东西)。

So to get better precision I can do something like: 因此,为了获得更高的精度,我可以执行以下操作:

void paintEvent(QPaintEvent * /*e*/)
{
    QPainter p(this);
    int y=10;

    for (qreal i = 10; i < 20; i += 0.2) {
        QFont font("Times"); // or any font
        font.setPointSizeF(i); // this has round to int error (on 72 dpi screen 10.2 would be rounded to 10 and 10.6 to 11.0 etc)
        p.setFont(font);

        qreal piX = i * p.device()->logicalDpiX() / 72.0;
        qreal piY = i * p.device()->logicalDpiY() / 72.0;
        qreal xscale = piX / qRound(piX);
        qreal yscale = piY / qRound(piY);

        //p.save();
        p.translate(1, y);
        p.scale(xscale, yscale);
        p.drawText(0, 0, QString("This should be point size %1 but is %2!").arg(font.pointSizeF()).arg(QFontInfo(font).pointSizeF() * xscale));
        p.resetTransform();
        //p.restore();
        y += i;
    }
}

I can get desired result this way. 这样我可以得到理想的结果。

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

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