[PyQt] Re: Bug ovverriding sizeHint

Phil Thompson phil at riverbankcomputing.co.uk
Mon Feb 25 14:28:01 GMT 2008


On Monday 25 February 2008, Giovanni Bajo wrote:
> On 2/25/2008 10:29 AM, Phil Thompson wrote:
> >> =======================================================
> >> from PyQt4.Qt import *
> >> app = QApplication([])
> >> w = QWidget()
> >> L = QVBoxLayout(w)
> >> L.addWidget(QLabel("ciao", w))
> >>
> >> called_class = []
> >> class MyScrollArea(QScrollArea):
> >>      def sizeHint(self):
> >>          called_class.append(1)
> >>          return QSize(100,100)
> >>
> >> called_func = []
> >> def mySizeHint(*args):
> >>      called_func.append(1)
> >>      return QSize(100,100)
> >>
> >> sv = MyScrollArea(w)
> >> sv.sizeHint = mySizeHint    # the trick!
> >>
> >> L.addWidget(sv)
> >> L.activate()
> >> assert not called_class
> >> assert called_func
> >> =======================================================
> >> Traceback (most recent call last):
> >>    File "sizehint.py", line 24, in ?
> >>      assert called_func
> >> AssertionError
> >>
> >> My understanding is that the function "mySizeHint()" should be called
> >> while calculating the size, as it was overridden. What happens is that,
> >> misteriously, *neither* sizeHint() function is called.
> >>
> >> Of course, if you comment the marked line, the overridden method is
> >> called, as expected.
> >
> > SIP is looking for a method to invoke - not just a callable. See the
> > implementation of sip_api_is_py_method() in siplib.c.
>
> I'm not sure I understand -- the comment seems to hint at a performance
> problem with PyCallable_Check. Besides the performance, would that work?

Yes. I've changed the test so that it specifically excludes only wrapped C++ 
functions rather than specifically includes only Python methods.

There are a couple of things to be aware of though...

1. Non-method callables will never be garbage collected (it needs incompatible 
SIP changes).

2. Patching will have no effect if the virtual has already been invoked 
(because there is an internal cache).

Phil


More information about the PyQt mailing list