[PyKDE] ConvertToSubClassCode

Gerard Vermeulen gvermeul at grenoble.cnrs.fr
Tue Nov 18 08:35:01 GMT 2003


Hi,

My difficulties to translate this C++ idiom to Python:

bool Plot::eventFilter(QObject *object, QEvent *e)
{
    if ( e->type() == QEvent::Resize )
    {
        const QSize &size = ((QResizeEvent *)e)->size();
        if ( object == (QObject *)axis(yLeft) )  // HOW TO THIS IN PYTHON?
        {
	// ...
        }
    }

    return QwtPlot::eventFilter(object, e);
}

were due to the fact that I did not implement ConvertToSubClassCode for
the Qwt widgets.  Therefore, the 'is' operator did not work as expected.

Since Qwt is more of a bunch of widgets than a neat hierarchy like PyKDE,
the most natural choice is to convert down from a QObject to a specific
subclass.

As far as I understand, the sipc module knows how to handle more than
one "static  PyObject *sipSubClass_QObject(const QObject *sipCpp)" (as
long as they are implemented in different modules), but sip does not
support it.

I sneaked my way out (for sip-3.8) by coding:

--------------------------------------------------------------------------------
%C++Code

// Hack: more than 1 sipSubClass_QObject() is not supported by sip.
// So we do it, with a little bit of help from pyqwt_sip_output_patch.py

static PyObject *sipSubClass_QObject(const QObject *sipCpp)
{
    // The table of Python class objects indexed by their names.
    // The table must be sorted by name.
    ...
    return sipMapStringToClass(
        sipCpp->className(), map, sizeof(map)/sizeof(map[0]));
}
%End
--------------------------------------------------------------------------------

And patching sip's output like this:

--------------------------------------------------------------------------------
static PyObject *registerClasses(PyObject *,PyObject *)
{
        if (sipRegisterClasses(&sipModule,-1) < 0)
                return NULL;

        // pyqwt_sip_output_patch.py

        sipRegisterSubClassConvertor(sipClass_QObject,(PyObject *(*)(const void*
))sipSubClass_QObject);

        /* Register the module's sub-class convertors. */
	...
---------------------------------------------------------------------------------

Question: wouldn't it be possible to implement a

%ConvertToSubClassCodeFrom FromObject

directive to handle the cases that extensionmodules want to convert down
from QObject (or other used superclasses)?


Gerard
 




More information about the PyQt mailing list