1############################################################################# 2## 3## Copyright (C) 2019 The Qt Company Ltd. 4## Contact: http://www.qt.io/licensing/ 5## 6## This file is part of the Qt for Python examples of the Qt Toolkit. 7## 8## $QT_BEGIN_LICENSE:BSD$ 9## You may use this file under the terms of the BSD license as follows: 10## 11## "Redistribution and use in source and binary forms, with or without 12## modification, are permitted provided that the following conditions are 13## met: 14## * Redistributions of source code must retain the above copyright 15## notice, this list of conditions and the following disclaimer. 16## * Redistributions in binary form must reproduce the above copyright 17## notice, this list of conditions and the following disclaimer in 18## the documentation and/or other materials provided with the 19## distribution. 20## * Neither the name of The Qt Company Ltd nor the names of its 21## contributors may be used to endorse or promote products derived 22## from this software without specific prior written permission. 23## 24## 25## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 36## 37## $QT_END_LICENSE$ 38## 39############################################################################# 40 41import sys 42from os.path import abspath, dirname, join 43 44from PySide2.QtCore import QObject, Slot 45from PySide2.QtGui import QGuiApplication 46from PySide2.QtQml import QQmlApplicationEngine 47 48from style_rc import * 49 50 51class Bridge(QObject): 52 53 @Slot(str, result=str) 54 def getColor(self, color_name): 55 if color_name.lower() == "red": 56 return "#ef9a9a" 57 elif color_name.lower() == "green": 58 return "#a5d6a7" 59 elif color_name.lower() == "blue": 60 return "#90caf9" 61 else: 62 return "white" 63 64 @Slot(float, result=int) 65 def getSize(self, s): 66 size = int(s * 42) # Maximum font size 67 if size <= 0: 68 return 1 69 else: 70 return size 71 72 @Slot(str, result=bool) 73 def getItalic(self, s): 74 if s.lower() == "italic": 75 return True 76 else: 77 return False 78 79 @Slot(str, result=bool) 80 def getBold(self, s): 81 if s.lower() == "bold": 82 return True 83 else: 84 return False 85 86 @Slot(str, result=bool) 87 def getUnderline(self, s): 88 if s.lower() == "underline": 89 return True 90 else: 91 return False 92 93 94if __name__ == '__main__': 95 app = QGuiApplication(sys.argv) 96 engine = QQmlApplicationEngine() 97 98 # Instance of the Python object 99 bridge = Bridge() 100 101 # Expose the Python object to QML 102 context = engine.rootContext() 103 context.setContextProperty("con", bridge) 104 105 # Get the path of the current directory, and then add the name 106 # of the QML file, to load it. 107 qmlFile = join(dirname(__file__), 'view.qml') 108 engine.load(abspath(qmlFile)) 109 110 if not engine.rootObjects(): 111 sys.exit(-1) 112 113 sys.exit(app.exec_()) 114