c++ - QPainter drawText 小数点

标签 c++ qt qt5 qpainter

有没有办法在 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!"
...

然后,还要检查文档:

将磅值设置为 pointSize。磅值必须大于零。可能无法在所有平台上实现请求的精度。

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

相关文章:

macos - 如何在 OS X Mavericks 上从 git 编译 Qt 5.2

c++ - 无法删除和删除 vector 上的对象指针

c++ - 函数调用参数中的构造函数样式转换

c++ - “CalucateNumbers”缺少异常规范 'noexcept'

qt - QML 加载并显示具有颜色属性的 .ply 网格

c++ - QSerialPort 读取字符串

c++ - Qt5 中 QX11EmbedWidget 的对应物是什么?

c++ - 在可变参数构造函数中初始化 const 数组

c++ - QtMainWindow 初始化事件

c++ - Qt Pushbutton 必须在延迟后处理信号