[PyQt] [PyKDE] KTextEditor interface classes

Jim Bublitz jbublitz at nwinternet.com
Sat Aug 30 01:34:51 BST 2008

On Friday 29 August 2008 16:19, Paul Giannaros wrote:
> On Fri, Aug 29, 2008 at 11:47 PM, Jim Bublitz <jbublitz at nwinternet.com> 
> > On Friday 29 August 2008 15:02, Paul Giannaros wrote:
> >> KTextEditor works with an interface class -- implementors of the
> >> interface subclass
> >> KTextEditor::View and KTextEditor::Document, with the subclasses also
> >> inheriting from any interfaces that they choose to support. This is a
> >> problem -- I can't see
> >> a way to cast to the required type.
> >>
> >> Even though I can check at run-time whether my view implements an
> >> interface
> >>
> >> with something like:
> >> >>> document = Kate.application().documentManager().documents()[0]
> >> >>> view = document.activeView()
> >> >>> view.inherits('KTextEditor::ConfigInterface')
> >>
> >> True
> >>
> >> I cannot use anything to cast to the appropriate type to get at the
> >> methods provided
> >> by the interface.
> >>
> >> Is there a solution to this problem?
> >
> > Are the parent classes also wrapped in your module?
> Firstly, it's not my module: ktexteditor is part of PyKDE4! Most of the
> interface classes are wrapped, yes (it looks like some may be
> missing, though; I can see KTextEditor::AnnotationInterface exists
> but no sip binding for it exists according to websvn).

I dropped it from PyKDE4 to make the package smaller - Simon apparently added 
it back.

You need to communicate with Simon Edwards and a) get the classes you need 
into the bindings and b) get the CTSCC blocks generated (it's done 
automatically via the project file).

> > If they are, you can use %ConvertToSubClassCode (CTSCC) - see it's use in
> > PyKDE or PyQt for examples and the sip docs (not all PyKDE classes have
> > inherits() available, so they use dynamic_cast; PyQt code uses
> > inherits()).
> >
> > For example, if a call creates a QWidget but returns (in C++) a QObject,
> > CTSCC will "cast" the returned object to QWidget in the Python program.
> I'm not sure how this will help. I don't want the KTextEditor::View object
> to always be returned to me as a KTextEditor::ConfigInterface. I need some
> way to get at a ConfigInterface (and a CursorInterface, SearchInterface,
> etc) from a View instance. I'd be happy if View objects had a method like
> configInterface() to return the interface where available.

Right - that's what %ConvertToSubClassCode will do for you. In PyKDE the code 
in the sip file (C++ code) tries to dynamic_cast to a subclass type and will 
return the most-derived type it can find.  I'm assuming CursorInterface, 
SearchInterface, etc are subclasses of ConfigInterface. It seems to me that's 
what you want, but maybe I'm misunderstanding.

If that's not what you want, then it seems you'll have to write C++ code and 
add it to the bindings, which is not that hard to do.


> > Otherwise there are ways to cast within Python using sip functions (see
> > the sip docs), but they probably aren't what you want.
> Unfortunately not. sip.cast only lets you cast to instances of
> superclasses.

More information about the PyQt mailing list