You certainly can't insert new syntax structures in Python code. The "best"
you can do in that way is doing it all as a string (with triple-quoted
strings to help) and parse it out of that. But then you don't get any
syntax highlighting in text editors or things like that.
One way which could be done is with context managers (which would make
Python 2.6 the minimum version, or 2.5 with "from __future__ import
with_statement"), which could end up with code looking like this:
with Rectangle() as Rectangle1: # I think this could work, but it could
need to be "with Rectangle('Rectangle1'):"
anchors = Anchor(center_in=parent) # I would welcome the opportunity to
change "centerIn" to "center_in" or "centerin"
width = 800
height = 480
color = 'red'
with Rectangle() as Rectangle2:
anchors = Anchor(fill=parent)
color = 'white'
with Rectangle() as Rectangle3:
anchors = Anchor(center_in=parent)
width = 200
height = 200
color = 'black'
with Image() as Image1:
anchors = Anchor(center_in=parent)
source = 'logo.png'
width = 64
height = 64
with MouseArea(): # "as MouseArea2" can be optional.
Auto-assign an ID.
anchors = Anchor(fill=parent)
def onClick(self):
print "show"
I'm not very familiar with QML though, not having used it at all (only
looked at it superficially). There could be some problems which couldn't be
resolved in this way; I am not certain.
Basically, the context manager would be used to create scope (not hard, just
doing some munging of f_locals() of the outer frame in __enter__ and
__exit__ to get the things defined inside correctly). If we're only dealing
with a certain set of possible attribute names, this is quite reasonable,
but if you can put in custom ones then it wouldn't work (if you set a value
to the same thing inside the block as outside, it wouldn't be picked up
unless it's been cleared out of the local "scope" by the context manager).
And how about functions - is it only certain function names which are
allowed?
Being comparatively magic stuff, this might not be the best way to do it
(i.e. you'd do it this sort of a way in Ruby (if you ever used it in the
first place!) without a second thought, but in Python you'd think twice
before doing it). But hey, it'd be fun!
Concerning using Python code in the QML, I don't like that notion. It feels
icky (from __future__ import braces, for starters). And indentation and
syntax highlighting of Python code would be messy; no text editors would
support it already, whereas lots of text editors support Python code.
Anyway, there's my mad way of doing it. You who know QML and can see what I
mean, please find all the problems in it! It wouldn't surprise me if it is
impractical (though I think it could be practical).
I think the originally specified way of doing it with classes may be the
best way of doing it, though probably with a metaclass added to do some
things (though I don't know what, yet).
-- Chris Morgan
_______________________________________________
PySide mailing list
[email protected]
http://lists.pyside.org/listinfo/pyside