Python/Qt 0.1.2

Quick Class Reference

Unimplemented are in brackets,
* means new in this release,
+ means completed in this release,
- means not complete)

New Python/Qt Examples

widgets.py Python version of the Qt demo
layout.py Python version of one of the Qt layout demos
pref.py Python version of the Qt demo


PYTHON-QT version 0.1.2

Python-Qt is a set of Python bindings for Qt.

SWIG version 1.1 (Beta 5p2) was used for development. The file swigptr.swg should be used to replace the ..../swig_lib/swigptr.swg to fix a bug in SWIG found during Python-Qt development. Thanks to Dave (SWIG developer) for fixing this so quickly and effortlessly (effortless for me at least!)

SWIG enables me, as the Python-Qt developer to do much of the coding in C++, without having to worry about parsing PyObject's and the like. SWIG also generates Python shadow-classes so, once in Python, the C++ classes can be used as if they were Python classes.

Development consists of wrapping each Qt class in my own C++ class. This is necessary so that I can emulate signals/slots (interestingly, a Qt signal becomes a slot in the wrapper class!)

Compilation

In an ideal world, untar the archive and type `make'. (Like this will really work!) If make attempts to run swig, and you don't have it installed, try touching pythonqt_wrap.c and pythonqt.py, or just remove the rule from the makefile.

Your version of Python must be compiled to allow dynamic loading of C/C++ modules. If you have dlfcn.h on your system then this should be done automatically. My system is capable of dynamic loading, but for some reason I don't have dlfcn.h. Typing `touch /usr/include/dlfcn.h' (creating a zero-length file called dlfcn.h) fixed this -- it makes the configure script find the file, and allow dynamic linking in the Python it generates.

Read the Python documentation if you are not running an ELF system -- dynamic loading is still possible. Included in this distribution is a pythonqtcmodule.so compiled on my Linux system.

Using it

Once you have your shared module produced you need to ensure that it can be found by Python. This is done by setting the environment variable PYTHONPATH to include `.'. Alternatively you could copy the module to /usr/local/lib/python1.4/sharedmodules or any other directory in PYTHONPATH. To find out all the directories that Python is currently searching, type the following in Python:

>>> import sys
>>> for x in sys.path: 
... print x 
... 

(paths will be printed here)

Ok, so lets assume that this is all ok now! From your Python prompt type "from pythonqt import *". This will import everything from the pythonqt module. If this fails try "from pythonqtc import *". If the second one works then you have not installed pythonqt.py in the correct directory. If it doesn't work, then you are in trouble!! Mail me for help (address at end).

Lets assume that you have imported pythonqt without any problems (hah!). Try a simple program to test it:

>>> from pythonqt import * 
>>> App = Application() 
>>> def Quit(): 
... App.quit() 
... 
>>> b = PushButton("Hello World") 
>>> b.setAutoResize(1) 
>>> b.connect("clicked", Quit) 
>>> b.show() 
>>> App.setMainWidget(b) 
>>> App.execLoop() 

This will generate a window with a push button in it. Clicking on the button should exit the application. At the moment only one Application/execLoop can happen inside any one Python session. I'm working on fixing this....

Sample programs

I've included four sample programs:

test.py This was my first test program
widgets.py This program is a port of $(QTDIR)/examples/widgets
lsm.py This program is included to make Martin happy! Try typing `python-qt' under title and `0.1' under `version' and then clicking on the Load button. File names are generated from these two fields, and the program can easily be used to generate lsm's for KDE applications! (Martin, if there's a problem with the format it generates, please tell me and I'll fix it.) It uses a font from the freefonts package. If you don't have it, I don't know what font you'll get! Change it if it's bad!

    layout.py

    This is a port of one of the Qt layout demonstration programs.
    pref.py This is a port of $(QTDIR)/examples/pref

    If you've got any neat ideas for more example programs, then I'd love to see them and include them in the distribution!

    To do:


    Comments would be appreciated! I'm going to work on getting all of the Widget member functions working. I haven't quite decided how to get events working. The ideas I have at the moment could cause some differences to Qt.


    Far fetched future plan: Modify swig, or write my own generator that can deal with signals and slots, and can generate wrappers for a class automatically....maybe one day this will happen.


    Even more far fetched future plan: Design & implement a KDE/Qt programming language that has all the best bits of C++, moc, Python + everything else!


    More sensible future plan: Rewrite krpm in Python-Qt.


    Enjoy, and have fun!

    Contact

    Mail: damyanp@cogs.susx.ac.uk or any of the kde mailing lists. (any chance of an email on kde.org?)