简体   繁体   English

当我在 PyQt5 中使用 QFontMetrics 时,我得到的数字与我使用 drawText 绘制的文本大小不匹配

[英]When I use QFontMetrics in PyQt5, the numbers I get don't match the size of the text that I'm drawing with drawText

I'm trying to get the size of a text, so I can scale it accordingly to fit inside a box.我正在尝试获取文本的大小,因此我可以相应地缩放它以适合一个盒子。 But unfortunately the QFontMetrics.width() seems to give wrong outputs.但不幸的是, QFontMetrics.width() 似乎给出了错误的输出。

Here's a code that draws a text, and uses values from QFontMetrics to draw a rect that should be similar size.这是一个绘制文本的代码,并使用 QFontMetrics 中的值绘制一个大小应该相似的矩形。 But it's not.但事实并非如此。 As you can see in the picture below, the values from QFontMetrics (drawn rect) are about half of the one that I'm drawing.如下图所示,QFontMetrics 中的值(绘制为矩形)大约是我绘制的值的一半。 And unfortunately I can't just multiply it by 2, because depending on the text, the factor might be 1.85 or 1.95.不幸的是,我不能只将它乘以 2,因为根据文本,该系数可能是 1.85 或 1.95。

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPainter, QTextDocument, QFont, QFontMetrics
from PyQt5.QtCore import QRect, Qt, QRectF
import sys

font = QFont("times",10)
fm = QFontMetrics(font)



class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.InitWindow()

    def InitWindow(self):
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.show()

    def paintEvent(self, event):
        painter = QPainter(self)

        painter.setFont(font)

        sText = 'Hello World!'
        painter.drawText(0,100, sText)

        pixelsWide = fm.width(sText)
        pixelsHigh = fm.height()
        painter.drawRect(0, 100, pixelsWide, pixelsHigh)




App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

在此处输入图像描述

As explained in the QFont documentation :QFont 文档中所述:

Note that a QGuiApplication instance must exist before a QFont can be used.请注意,在使用 QFont 之前,必须存在 QGuiApplication 实例。

This obviously including usage of the QFont as a QFontMetrics constructor.这显然包括使用 QFont 作为 QFontMetrics 构造函数。

The reason is simply logic and quite obvious: the QApplication must be aware of the UI environment in order to properly compute font metrics, which might depend on the paint device they're going to be drawn upon.原因很简单,也很明显:QApplication必须知道 UI 环境才能正确计算字体指标,这可能取决于它们将被绘制的绘图设备。 Consider the common case of font scaling or High DPI settings: without a QGuiApplication, Qt has absolutely no meanings of knowing those aspects, and QFont shouldn't (nor could) obviously take care of that in its constructor, as QFontMetrics wouldn't.考虑字体缩放或高 DPI 设置的常见情况:如果没有 QGuiApplication,Qt 绝对没有了解这些方面的意义,QFont 显然不应该(也不能)在其构造函数中处理这些问题,因为 QFontMetrics 不会。

Move the QFont and QFontMetrics constructor somewhere else, which could be in any moment after the QApplication creation and before their actual usage.将 QFont 和 QFontMetrics 构造函数移到其他地方,这可能是在 QApplication 创建之后和实际使用之前的任何时刻。

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

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