Here's a way to do it with a Python dict structure. Given the QML:
import QtQuick 1.0
Rectangle
{
id: Rect1
anchors.centerIn: parent
width: 800
height: 480
color: "red"
Rectangle
{
anchors.fill: parent
color: "white"
Rectangle
{
id: Rect2
anchors.centerIn: parent
width: Rect1.width / 2
height: 200
color: "black"
Image
{
anchors {fill: parent; topMargin: 6; bottomMargin: 6}
source: "logo.png"
width: 64
height: 64
MouseArea
{
id: "mousey",
anchors.fill: parent
onClicked:
{
console.log("click")
}
}
}
}
}
}
you can translate to Python like so:
from QtQuick import Rectangle, parent
def log_click():
print "Click"
qml = \
{
Rectangle('Rect1'):
{
'anchors': {'centerIn': parent},
'width': 800,
'height': 480,
'color': 'red',
Rectangle():
{
'anchors': {'fill': parent},
'color': 'white',
Rectangle('Rect2'):
{
'anchors': {'centerIn': parent},
'width': Rectangle('Rect1').width / 2,
'height': 200,
'color': black,
Image():
{
'anchors': {'fill': parent, 'topMargin': 6, 'bottomMargin': 6},
'source': 'logo.png',
'width': 64,
'height': 64,
MouseArea('mousey'):
{
'anchors': {'fill': parent},
'onClicked': log_click,
}
}
}
}
}
}
Calling Rectangle() creates a new, anonymous rectangle; calling
Rectangle('Rect1') gives that rectangle the ID 'Rect1'. Calling it again
returns the previously-created rectangle with that ID so you can refer to it
later. The 'parent' object would be a placeholder to let the layout engine know
the right thing to do.
dan
----
Daniel Ashbrook, PhD
Senior Researcher, New Mobile Forms and Experiences
Nokia Research Center
Media Technologies Lab, Santa Monica
[email protected]
On Jul 12, 2011, at 7:43a, ext Renato Araujo Oliveira Filho wrote:
> Hi guys,
>
> Last week I spent some time working on a idea I had some time ago,
> this is a way to describe QML scenes without QML :D. In other words is
> a new way to describe scenes only using Python.
> With that you remove the necessity of use QML code in your python programs.
>
> My idea is in early stage then I would like to discuss with you is,
> which is the best way to describe scenes in python?. I'm not a heavy
> python user, then I'm not familiar with all python features, because
> of that I want your opinions.
>
> My first try is something like that:
>
> class Rectangle1(Rectangle):
> anchors = Anchor(centerIn = parent)
> width = 800
> height = 480
> color = 'red'
> class Rectangle2(Rectangle):
> anchors = Anchor(fill = parent)
> color = 'white'
> class Rectangle3(Rectangle):
> anchors = Anchor(centerIn = parent)
> width = 200
> height = 200
> color = 'black'
> class Image1(Image):
> anchors = Anchor(centerIn = parent)
> source = 'logo.png'
> width = 64
> height = 64
> class MouseArea2(MouseArea):
> anchors = Anchor(fill = parent)
> def onClick(self):
> print "show"
>
>
>
> This describe a scene with 3 rectangle, 1 image and a mouse area, this
> is very similar to QML code I only translate the QML code to python,
> but I do not know if there is some a best pythonic way to do that.
>
> What do you think about this way to describe scenes?
>
> Do you have any suggestions to make this more pythonic?
>
>
> The initial code, it can be found on my github repostiry[1], this is
> very early to report bugs or even try use that, first I need to figure
> out the best way to describe the scenes and then I will work on new
> features and stability.
>
> I hope someone can give some feedback and help me to develop the idea.
>
> [1] https://github.com/renatofilho/pysideQML
>
> Thanks
>
>
> --
> Renato Araujo Oliveira Filho
> Instituto Nokia de Tecnologia - INdT
> _______________________________________________
> PySide mailing list
> [email protected]
> http://lists.pyside.org/listinfo/pyside
_______________________________________________
PySide mailing list
[email protected]
http://lists.pyside.org/listinfo/pyside