[PyQt] Problem with boundingRect in PyQt snapshot
Jeremy Sanders
jeremy at jeremysanders.net
Thu Sep 17 12:06:48 BST 2009
On Thu, 17 Sep 2009, Phil Thompson wrote:
> On Thu, 17 Sep 2009 11:39:09 +0100, Mark Summerfield <list at qtrac.plus.com>
> wrote:
>> On 2009-09-16, Phil Thompson wrote:
>>> 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.
>>>
>>> Your guess is correct.
>>>
>>> You could use tightBoundingRect() instead.
>>>
>>> I can back out the automatic conversion from QChar to QString, but that
>>> leaves the question of what should happen with v2 of the QString API,
> ie.
>>> should QChar not be dropped?
>>
>> I think it would be a pity to keep it. Most of its methods are available
>> in Python anyway, and for those few that aren't couldn't you provide
>> some static methods that either take a one character string or that only
>> consider the first character?
>
> Do you want to go through the whole API and tell me the functions that
> behave slightly differently given a QChar rather than a QString of length
> one?
>
> I don't have a problem with keeping it - it is a very specialised class and
> you'd only use it if you really needed to.
I think it is probably necessary to keep it. You could check for a single
character string, but this will break the use cases where you expect the
function to work for any number of characters in the same way. For a
string it should give height(), not the real bounding box of the
character.
It's a shame they used the same function name for both operations.
An alternative would be to have a second function, say boundingRect_char
for the specialsed case. This might break existing apps (like mine) that
currently depend on QChar working. THere probably aren't too many of
those.
Jeremy
--
Jeremy Sanders <jeremy at jeremysanders.net>
http://www.jeremysanders.net/ Cambridge, UK
Public Key Server PGP Key ID: E1AAE053
More information about the PyQt
mailing list