[PyKDE] controlling C++/Qt apps with PyQt

Phil Thompson phil at river-bank.demon.co.uk
Fri Nov 30 18:42:18 GMT 2001


Austin Bingham wrote:
> 
> I'm working on some python extensions for automating C++/Qt applications
> (i.e. for stress-testing). The extension provides facilities for getting
> QWidget pointers by name from the QApplication's widget tree. If this
> extension is used in the context of a python interpreter embedded in the
> C++ application, the python objects have access to the widgets in the
> application.
> 
> What I'm curious about is if PyQt could be used to then manipulate these
> QWidget pointers. From what I've seen of PyQt, it is designed for building
> Qt applications in python. I would like to be able to, in a sense,
> is attach PyQt objects to existing QWidget pointers (i.e. the ones
> extracted from the C++ process) so that I could leverage all of the hard
> work that's aready gone into proxying Qt in python. It's more-or-less the
> reverse of what PyQt seems to be designed for.
> 
> I've noticed that most (all?) of the PyQt widget-based classes have a
> constructor that takes a QWidget*, and this seems like a reasonable way to
> get what I am looking for. What I'd like to find out is if I'm going to
> run head-frist into design assumptions in the PyQt module. Any thoughts on
> this would be greatly appreciated.

The SIP API makes it fairly easy to create Python objects for existing
Qt instances - the highest level interface is sipMapCppToSelf(). If you
have an existing QWidget *w then...

	sipMapCppToSelf(w,sipClass_QWidget);

...will return a Python object that is a Python QWidget instance object.

Things to note...

- it is assumed Python doesn't have responsibility for deleting the C++
QWidget (ie. deleting the last reference to the Python object does not
result in a call to the C++ dtor), but the API allows you to change this

- you have no access to any of QWidget's protected methods from Python

- you cannot re-implement any of QWidget's virtual methods in Python.

Phil




More information about the PyQt mailing list