Java知识分享网 - 轻松学习从此开始!    

Java知识分享网

Java1234官方群25:java1234官方群17
Java1234官方群25:838462530
        
SpringBoot+SpringSecurity+Vue+ElementPlus权限系统实战课程 震撼发布        

最新Java全栈就业实战课程(免费)

springcloud分布式电商秒杀实战课程

IDEA永久激活

66套java实战课程无套路领取

锋哥开始收Java学员啦!

Python学习路线图

锋哥开始收Java学员啦!
当前位置: 主页 > Java文档 > Java基础相关 >

Developing MeeGo apps with Python and QML PDF 下载


分享到:
时间:2020-09-14 10:14来源:http://www.java1234.com 作者:小锋  侵权举报
Developing MeeGo apps with Python and QML PDF 下载
失效链接处理
Developing MeeGo apps with Python and QML PDF 下载

 
本站整理下载:
提取码:dgxc 
 
 
相关截图:
 
主要内容:

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).

 

------分隔线----------------------------

锋哥公众号


锋哥微信


关注公众号
【Java资料站】
回复 666
获取 
66套java
从菜鸡到大神
项目实战课程

锋哥推荐