失效链接处理 |
Developing MeeGo apps with Python and QML PDF 下载
本站整理下载:
相关截图:
主要内容:
Basic QML tutorial examples
This section should give you a short overview with code examples on how to do
simple things with PySide and QML. More examples can be found on the
homepage of this tutorial on http://thp.io/2010/meego-python/
Hello World
This example shows you how to create a minimalistic Hello world QML
application with Python. We already subclass QObject here and provide a
simple property ('greeting') that we can then access from QML. Let's dive right
into the source code.
The Python source (HelloMeeGo.py)
For our first hello world program, we want to generate a greeting in Python and
show it in a QML UI. We do this by subclassing QObject, giving our subclass a
property called “greeting” and exposing an instance of that object to the QML
root context, where we can access it from the QML file.
# -*- coding: utf-8 -*-
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *
We need the “sys” module (a Python standard module) to access the command
line arguments (we need to pass them to the constructor of QApplication). From
PySide, we need QtCore (which contains core objects, like QObject in our
example) and QtGui (which contains Qapplication, which we need for the Qt
main loop). The QML view is provided by the QtDeclarative module – it provides
QDeclarativeView.
class Hello(QObject):
def get_greeting(self):
return u'Hello, Meego!'
greeting = Property(unicode, get_greeting)
This is the definition of our “Hello” class – we provide a getter method for the
greeting, and return a unicode string “Hello, MeeGo!” in it – if you want, you
can also customize this greeting, i.e. add the time and date using the
“datetime” Python module. In order for QML to be able to access this property,
we have to declare it as such by using “Property” (which is in QtCore). The first
parameter of Property is the type (unicode means unicode string) and the
second one is the getter method – if you want the property to be modifyable,
you need to add a setter method, and if you want it to be dynamically updated,
you also need a notifyable property.
app = QApplication(sys.argv)
hello = Hello()
view = QDeclarativeView()
Here we create instances of the classes we need:
• QApplication is needed by every Qt application and handles commandline arguments, sets up the graphics system and does other
initializations. It also provides the main loop through the “exec_” method.
• Hello is our class defined above. We create an instance of it that we later
pass to QML using context properties.
• QDeclarativeView is the window / view in which we can load QML content
and display it on the screen.
context = view.rootContext()
context.setContextProperty('hello', hello)
For QML to be able to access our “hello” object, we need to expose it as a
context property to the view's root context – this is done using
setContextProperty. As property name we use “hello”, so we can access the
greeting of that object later using “hello.greeting” in QML.
view.setSource(__file__.replace('.py', '.qml'))
Here the QML file is loaded and displayed in the view. As the QML file has the
same name as our Python script (just a different file extension), we can use
__file__.replace('.py', '.qml') to always get the correct file name – this also
allows you to easily rename both files and still have them work together as
expected (for example, if you want to try to create different variants of this
example).
|