[PyKDE] The thingie that makes your user interfaces....

Torsten Marek shlomme at gmx.net
Tue Apr 5 19:18:18 BST 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Giovanni Bajo schrieb:
> Torsten Marek <shlomme at gmx.net> wrote:
> 
> 
>>>I created a new release (0.2.1, same place as yesterday) showing two
>>>techniques
>>>of putting implementations and UI files together.
>>>
>>>The first one uses some deeper Python magic to create types on the fly
> 
> and
> 
>>>what
>>>not. A lot of implicit stuff and some restraints (?) like not having to
> 
> call
> 
>>>the
>>>base class constructor. Furthermore, you inherit from PyQtUI but this is
> 
> not
> 
>>>your base class at all.
>>>
>>>The second one is more typing on the user side and you need to make sure
> 
> that
> 
>>>you inherit the correct base class and call your base class constructor,
> 
> but
> 
>>>it's more obvious what happens.
>>>
>>>Preferences, anybody?
>>>Although I like the dark stuff happing in (1), I'd go for (2).
> 
> 
> It's not clear to me what happens in the second case if whatever you bring
> up from the UI parser is not a QDialog, since you already constructed a
> QDialog.
The widget creation code checks for a correct base class and throws an exception
if it is wrong, but if this wasn't there, it works - and might introduce the
strangest, most subtle errors nobody could possibly ever comprehend;-)


> The other solution proposed by Truls has another side effect, in that you
> lose the ability of having the class defined at import time, with the
> obvious fallouts (if you have to inherit 10 other classes from it in 10
> different modules, things get harder and harder).

> 
> I would go for something like:
> 
> I like your first solution, but it's not clear to me why you are using
> __new__, it does not seem to bring any benefit, and makes us violate the
> usual paradigm of calling __init__ on the base class. I think the better way
> would be something like:
> 
> 
> class PyQtUI(object):
>    def __init__(self, uifile, *args, **kwargs):
>         newcls = p.parse_withType(uifile)
>         self.__class__.__bases__ = (newcls, )
>         newcls.__init__(self, *args, **kwargs)

parse_withType does not return a type, but an instance.
But this is not a stopper, since self.__class__ can be passed to a parse
function, which then sets __bases__.
I have to play around with this a bit, but I'm not sure what happens if you
create several widgets, because __bases__ may get several types assigned.

> 
> 
> class MyWindow(PyQtUI):
>     def __init__(self, parent):
>         PyQtUI.__init__(self, "mywindow.ui", parent)
> 
>         # now it's a QDialog!
>         assert isinstance(self, QDialog)
>         self.setModal(False)
> 
> (untested code)

> 
> The fact that isinstance() works as expected has other benefits (for
> instance, you can find such widgets through queryList() and the such).

isinstance works for the other versions as well. What I do not like about the
versions that create types or fiddle with __bases__ (I had something like that
in the very beginning) is that a lot of implicit "stuff" happens - and I'd
rather have it explicit, even if it takes two or three more lines of code (aka
"pythonic").
On the other hand, providing several methods is pretty well possible.

greetings

Torsten

- --
Torsten Marek <shlomme at gmx.net>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146  894C 7CC5 451E A244 C858
Keyserver: subkeys.pgp.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCUtZqfMVFHqJEyFgRAgo8AKCHCz668fDf8IVXIyP1vWtT2cS8bACfeZKo
936iDOOBftLpwiTeKRNhgq4=
=SrEC
-----END PGP SIGNATURE-----




More information about the PyQt mailing list