2

Qt 5 で分数のポイント サイズでテキストを描画する方法はありますQFont::setPointSizeF()か?

QFontDatabase::isScalableすべての場合にフォントをQFontDatabase::isSmoothlyScalable返します。true

色々QFont::fontHintingPreferenceと設定してみQPainter::RenderHintました。

QFont::setPixelSizeと を使用してこれを回避できるかもしれませんが、壊れているのはQPainter::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();
}
4

2 に答える 2

1

これは予期しない動作ではありません。以下の行を参照してください。

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

于 2013-09-26T19:43:17.990 に答える
0

Qtはポイントサイズに基づいてピクセルサイズを計算し、それがintをパラメータとして取るQFont::setPixelSizeで終わるようです(またはそのようなもの)。

したがって、精度を上げるために、次のようなことができます。

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;
    }
}

このようにして、望ましい結果を得ることができます。

于 2013-09-26T22:47:26.853 に答える