Wednesday, 27 April 2011

Converting an application from PyQt to PySide

I'm in the middle of porting an application from PyQt to PySide. I put it off for a while because it's a big app, about a dozen files and maybe 7,000 lines of code. Some of those files are 2,000 lines or more. I blame only myself!

Actually so far it's been fairly painless, if laborious. Nokia provides reasonable documentation on the process and I'd been fairly sensible about building the app in logical functional modules so that I can build up the converted version in stages, adding functionality and testing it as I go. As far as I can tell so far, PySide is as stable and comparable in performance to PyQt.

I found that my oldest code used old style signals and slots, while the newer modules used the new improved syntax so I've been regularising them all to the new syntax as that's the only one PySide supports. Eli Bendersky has a good post on the advantages of the new style, as well as a neat trick using lambda to pass extra arguments to a slot.

Perhaps the biggest problem, in terms of drudgery, is getting rid of every QVariant and QString and replacing them with ordinary Python objects. The app uses the Qt MVC framework where values are passed to and from the model as QVariants, so I had a lot to change there. In particular, when you pass an integer to the model it arrives as a QVariant which you then 'un pack' to a Python integer and a boolean like this:

            elif column == POPULATION :
                new_index, ok = value.toInt()

The boolean just tells you whether the conversion worked or not, and I was actually just throwing it away. This now gets changed to...

            elif column == POPULATION :
                new_index = value

Much nicer!

No comments:

Post a Comment