有没有一种方法可以在Qt 5中以小数点大小绘制文本。
我正在尝试使用QFont::setPointSizeF(),但它似乎不适用于我在(mac / linux / windows)上尝试过的任何平台,并且磅值总是四舍五入。

在所有情况下,QFontDatabase::isScalableQFontDatabase::isSmoothlyScalable返回字体的true

我尝试设置各种QFont::fontHintingPreferenceQPainter::RenderHint

我也许可以使用QFont::setPixelSizeQPainter::scale解决此问题,但是QFont::setPointSizeF损坏了似乎很奇怪?

我是否缺少某些东西或做错了什么?

显示问题的简单程序:

#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 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!"
...

然后,还检查文档:
Sets the point size to pointSize. The point size must be greater than zero. The requested precision may not be achieved on all platforms.

关于c++ - QPainter带有小数点大小的drawText,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19036047/

10-15 05:54