Qt World Summit: Register Today!

QFontMetrics::tightBoundingRect acting strange

  • Hello all.
    I was trying hard to know why I’m getting larger rects for the text I’m using (mostly Arabic). Issue is that QFontMetrics::tightBoundingRect returns strange width sometimes.

    Check the width of these functions and you’ll see that they it is different, even though there is no change in the layout at all. I am using Noto Sans font.

    QFontMetrics::tightBoundingRect("a\u200Cb") // \u200C is ZWNJ character

  • Lifetime Qt Champion


    What version of Qt are you using ?
    On what OS ?
    What results are you getting ?

  • Hi!
    I’m using Qt 5.11.1 on ArchLinux and getting these results:

    PyQt5.QtCore.QRectF(3.0, -64.0, 91.0, 65.0) \\ "ab"
    PyQt5.QtCore.QRectF(3.0, -64.0, 113.0, 65.0) \\ "a\u200Cb"

    Yes I’m using PyQt, but I believe it has nothing to do with the binding.

    Edit: I'm using Noto Sans, point size 62.

  • Lifetime Qt Champion

    Can you show the complete code with font setup ?

  • Sure thing.

    # minimal code
    from PyQt5.QtGui import QGuiApplication, QFont, QFontMetricsF
    app = QGuiApplication(["myApp"])
    notoFont = QFont("Noto Sans", 62)
    notoMetrics = QFontMetricsF(notoFont)


    PyQt5.QtCore.QRectF(3.0, -64.0, 91.0, 65.0)
    PyQt5.QtCore.QRectF(3.0, -64.0, 113.0, 65.0)

  • Lifetime Qt Champion

    Which variation of the Note Sans font ? There's a lot of possibilities.

  • Well, I think that the font doesn’t really matter here. Whatever font I use I get the same problem.
    Reported bug: https://bugreports.qt.io/browse/QTBUG-70184