Python and Qt programming with Roberto Alsina

Roberto Alsina recently posted a series of tutorials about Python and PyQt on his blog. I don't think they appeared on Planet KDE, so I'm forwarding them on. ;-) It covers typical Qt GUI programming using Qt Designer.

Most of the material is directly relevant if you want to develop KDE applications using Python, PyQt and PyKDE. Some differences of note are:

  • KDE widgets - KDE has many widgets that replace standard Qt widgets and extend them with better integration with the KDE desktop and more functionality. Assuming you've got the right packages installed, you should have extra KDE widgets available inside Qt Designer.
  • pykdeuic4 - The pyuic4 tool doesn't understand or support KDE's widgets. Use pykdeuic4 instead. It's basically pyuic4 with added KDE support.
  • XMLGUI for menus and toolbars - KDE has support for defining menus, menu items and toolbars in an external XML file which will automatically be used if your application uses the KXmlGuiWindow class for its main window. This makes it easy for people to customise your application and saves you from having to write a lot of tedious and boring set up code.
  • Build system - Python takes a pretty low ceremony approach to most things including build systems. Usually you don't need one, except perhaps during installation and/or packaging. In PyKDE in the kdebindings module, there should be an example Python project and directory called tools/cmake_project (exact location depends on your distribution). It is a basic project template which uses the CMake build system to correctly install your application and its data files, and doing things like generating .pyc files, and compiling .ui files to .py. It is a good base which can be expanded and customised. Some prior knowledge of CMake is required though.
  • Resources - We, or at least I, don't use Qt style resources for handling images and data files etc. In a KDE application you'll be installing your files, possibly using CMake and using the methods on the KStandardDirs class (via KGlobal.dirs()) to locate your data files.

Here are the links. Thanks Roberto!

Session 1
Session 2
Session 3
Session 4
Session 5


do such projects already exist?

Actually I am working on one (playground/games/kmj), and I want to use cmake with it. But there is also an icon, a kmjui.rc and translations - things that your sample_project does not have. Do cmake makros exist to handle such files and to install them into the correct kde directories?

Should every project have its own copy of cmake/modules or is there a central directory with typical cmake modules for pykde?

BTW the last rparen in sample_project/CMakeLists.txt should probably be ), not }

By wrohdewald at Thu, 04/02/2009 - 10:37

guidance-power-manager in extragear, printer-applet in kdeutils and system-config-printer-kde in kdeadmin.

With KDE 4.2 kdelibs ships with the PyKDE cmake files so you shouldn't need your own copy.

By Jonathan Riddell at Thu, 04/02/2009 - 14:00

thank you, guidance-power-manager looks very interesting. I will have to copy some of your code around def main()

By wrohdewald at Fri, 04/03/2009 - 09:59