-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (81 loc) · 2.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# A simple PySide example
from PySide import QtCore, QtGui
import sys
import os
from PySide.QtCore import QObject
from PySide.QtGui import *
from PySide.QtDeclarative import *
import time
import datetime
WINDOW_TITLE = "PySide Example"
# enable running this program from absolute path
os.chdir(os.path.dirname(os.path.abspath(__file__)))
class PropertyExample(QObject):
"""
Python property provider
"""
def __init__(self):
QObject.__init__(self)
self.rootObject = None
#NOTE: the root object is needed only by Python properties
# that call QML code directly
@QtCore.Slot(result=str)
def getDate(self):
"""
return current date & time
"""
return str(datetime.datetime.now())
@QtCore.Slot(str)
def notify(self, text):
"""
trigger a notification using the
Qt Quick Components InfoBanner
"""
#NOTE: QML uses <br> instead of \n for linebreaks
self.rootObject.notify(text)
class ImagesFromPython(QDeclarativeImageProvider):
"""
Image provider example
"""
def __init__(self):
# this image provider supports QImage,
# as specified by the ImageType
QDeclarativeImageProvider.__init__(self, QDeclarativeImageProvider.ImageType.Image)
def requestImage(self, pathId, size, requestedSize):
# we ignore size & requested size for simplicity
# we use the path ID provided from the URL used
# in QML for a caption to paint on the image
text = pathId
# for an example image, PySide logo in SVG is used
image = QImage("pyside.svg")
image.scaled(requestedSize.width(),requestedSize.height())
painter = QtGui.QPainter(image)
painter.setPen("white")
painter.drawText(20, 20, text)
return image
if __name__ == '__main__':
app = QApplication(sys.argv) # create the application
view = QDeclarativeView() # create the declarative view
# add Python properties to the
# QML root context
rc = view.rootContext()
# add the example property
property = PropertyExample()
rc.setContextProperty("example", property)
# register image providers
# NOTE: the image provider name in the Image.source URL is automatically lower-cased !!
provider = ImagesFromPython()
view.engine().addImageProvider("from_python", provider)
# NOTE2: view.engine().addImageProvider("from_python", ImagesFromPython())
# doesn't work for some reason
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
view.setSource("main.qml")
rootObject = view.rootObject()
property.rootObject = rootObject
view.setWindowTitle(WINDOW_TITLE)
view.window().showFullScreen()
view.resize(480,854)
#view.resize(854,480)
view.show()
app.exec_()