Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Custom widgets using PyQt

A short tu­to­ri­al ex­plain­ing how to cre­ate re­us­able wid­gets us­ing PyQt.

taj / 2006-04-03 06:38:

Nice tutorial. I have one somewhat-related gripe, though, and that is pyuic.



A dynamic language like python should be dealing with UI files dynamically, and pyuic doesn't quite fit into that philosophy. At the moment, PyQt doesn't seem to implement QWidgetFactory. But since that creates QWidget objects rather than subclasses, its not the ideal solution either.



Now that python provides a much nicer way to create types etc on the fly, a better system would be to dynamically load UI files as modules. Then the module user can dynamically instantiate them at runtime, or import (using an import hook) and subclass them as one does with the current pyuic generated classes.



I might have a go at implementing this over the weekend.

Peter Gordon / 2006-04-03 06:39:

Roberto,



Your tutorials are great and I hope you will have more. It is great way to learn

Roberto Alsina / 2006-04-03 06:39:

Well, if you can make that work, I will fedex you a beer :-)

G_os / 2006-04-03 06:42:

Hi,



Thanks a lot. Great to learn :) Both python and qtdesigner



G_os

jmoiron / 2006-04-03 06:42:

Your live action tutorial was great and this one is just as informative.



The first comment seems to be describing the behavior gtk's "libglade"; but I actually prefer the way pyuic does things, since it means that I can have my program's structure in precompiled byte code instead of being parsed and hacked together at run time.

taj / 2006-04-03 06:44:

jmoiron,



Yeah libglade does that, as does designer via the QWidgetFactory interface. I can understand the desire to have stuff precompiled and ready to run, but I personally don't like the intermediate build step. To me it goes against the python idea of not having a build step before running the code.



Incidentally, I was wrong about QWidgetFactory not being implemented by PyQt. It is available in the qtui module.

Roberto Alsina / 2006-04-03 06:44:

But... QWidgetFactory only would work well if there was a way to write a QWidgetPlugin.



Or if your app only uses standard widgets. Which I seem unable to limit myself to :-)



Being able to create custom gadgetry easily is one of the greatest things about Qt. It´s even easier in PyQt. But it does break QWidgetFactory :-P



One other solution is to use another design pattern, and write a class that manages a group of widgets while not being itself a widget. You can even do event overriding (on PyQt, not on Qt), but I find the code ugly. Useful! But ugly.


Contents © 2000-2024 Roberto Alsina