[PyQt] Problem with boundingRect in PyQt snapshot

Phil Thompson phil at riverbankcomputing.com
Thu Sep 17 22:04:00 BST 2009

On Wed, 16 Sep 2009 20:04:48 +0100, Jeremy Sanders
<jeremy at jeremysanders.net> wrote:
> Hi - for some reason the following code does not produce the 
> correct answer after updating PyQt.
> On Ubuntu with the PyQt 4.4.4 package the following returns 
> 18.0
> from PyQt4.QtGui import *
> from PyQt4.QtCore import *
> import sys
> app = QApplication(sys.argv)
> pix = QPixmap(128, 128)
> p = QPainter(pix)
> font = QFont("Times New Roman", 20)
> fm = QFontMetricsF(font, p.device())
> print fm.boundingRect(QChar('0')).height()
> On the same system with the same Qt (4.5.0), PyQt-x11-
> gpl-4.6-snapshot-20090906 and returns 32.0!
> The text is misaligned when using height to align it using 
> the snapshot.
> My guess is that the QChar is being promoted to QString. This 
> function returns a different result for QString, so it breaks 
> my code. The QString version returns the font height, not the 
> height of the character given.

I've just realised my confirmation of the cause of the problem is wrong
(though related). I removed the QChar overload of boundingRect() because I
thought (incorrectly) that the QString overload would do the same job. They
will be restored in tonight's snapshot.

I think the easiest solution for v2 of the QString API (ie. Python v3) is
to add extra methods as it only seems to be the width() and boundingRect()
methods of QFontMetrics and QFontMetricsF that need special handling.


More information about the PyQt mailing list