def testSignalEmission(self): qmlRegisterType(MyItem, "my.item", 1, 0, "MyItem") view = QQuickView() view.setSource(QUrl.fromLocalFile(adjust_filename('bug_951.qml', __file__))) self.app.exec_() self.assertTrue(MyItem.COMPONENT_COMPLETE_CALLED)
def test_qml_type(self): qmlRegisterType(TestClass, "JavaScriptExceptions", 1, 0, "JavaScriptExceptions") view = QQuickView() qml_url = QUrl.fromLocalFile(adjust_filename("javascript_exceptions.qml", __file__)) view.setSource(qml_url) self.assertTrue(test_1) self.assertTrue(test_2)
def main(): app = QGuiApplication([]) # reg qml types here register_qml_types() # force gc to run gc.collect() view = QQuickView() url = QUrl(__file__.replace(".py", ".qml")) view.setSource(url)
def testIt(self): app = QGuiApplication([]) qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart'); qmlRegisterType(PieSlice, "Charts", 1, 0, "PieSlice"); view = QQuickView() view.setSource(QUrl.fromLocalFile(helper.adjust_filename('registertype.qml', __file__))) view.show() QTimer.singleShot(250, view.close) app.exec_() self.assertTrue(appendCalled) self.assertTrue(paintCalled)
def testReturnPolicy(self): view = QQuickView() item1 = QQuickItem() item1.setObjectName("Item1") # TODO: This existed in QDeclarativeView but not in QQuickView. # Have to rewrite this to the QQuickView equivalent view.scene().addItem(item1) self.assertEqual(item1.objectName(), "Item1") # check if the item still valid item2 = QQuickItem() item2.setObjectName("Item2") item1.scene().addItem(item2) item1 = None self.assertEqual(item2.objectName(), "Item2") # check if the item still valid view = None
def testAbstractItemModelTransferToQML(self): view = QQuickView() view.setSource(QUrl.fromLocalFile(adjust_filename('bug_814.qml', __file__))) root = view.rootObject() model = ListModel() root.setProperty('model', model) view.show()
def testIt(self): app = QGuiApplication([]) qmlRegisterType(MyClass,'Example',1,0,'MyClass') view = QQuickView() view.setSource(QUrl.fromLocalFile(adjust_filename('bug_926.qml', __file__))) self.assertEqual(len(view.errors()), 0) view.show() QTimer.singleShot(0, app.quit) app.exec_()
def testQQuickNetworkFactory(self): view = QQuickView() url = QUrl.fromLocalFile(adjust_filename('hw.qml', __file__)) view.setSource(url) view.show() self.assertEqual(view.status(), QQuickView.Ready) self.app.exec_()
from __future__ import print_function import sys from PySide2.QtCore import QObject, QUrl from PySide2.QtGui import QGuiApplication import PySide2.QtQml from PySide2.QtQuick import QQuickView def sayThis(s): print(s) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() view.setSource(QUrl('view.qml')) root = view.rootObject() root.textRotationChanged.connect(sayThis) root.buttonClicked.connect( lambda: sayThis("clicked button (QML top-level signal)")) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)
import sys from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl if __name__ == '__main__': app = QApplication(sys.argv) view = QQuickView() url = QUrl('view.qml') view.setSource(url) view.show() sys.exit(app.exec_())
import sys from PySide2.QtCore import QUrl from PySide2.QtQuick import QQuickView from PySide2.QtWidgets import QApplication if __name__ == '__main__': app = QApplication([]) view = QQuickView() url = QUrl('views/main.qml') view.setSource(url) view.setResizeMode(QQuickView.SizeRootObjectToView) view.show() sys.exit(app.exec_())
from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QObject, Signal, Slot, QUrl, Qt from PySide2 import QtGui class MyClass(QObject): @Slot(str) # 输入参数为str类型 def outputString(self, string): print(string) app = QApplication([]) app.setWindowIcon(QtGui.QIcon('./img/icon.ico')) view = QQuickView() #链接quit 到函数中去 view.engine().quit.connect(app.quit) #链接函数到mql 用js执行 con = MyClass() context = view.rootContext() context.setContextProperty("con", con) context.setContextProperty("mainwindow", view) view.setSource(QUrl("./qml/s.qml")) #透明和无标题 view.setFlags(Qt.FramelessWindowHint) view.setColor(Qt.transparent) view.show()
if role == Qt.DisplayRole: return d['name'] elif role == Qt.DecorationRole: return Qt.black elif role == PersonModel.MyRole: return d['myrole'] return None def populate(self): self._data.append({'name': 'Qt', 'myrole': 'role1'}) self._data.append({'name': 'PySide', 'myrole': 'role2'}) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) myModel = PersonModel() myModel.populate() view.rootContext().setContextProperty("myModel", myModel) qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) view.show() app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order.
## and conditions see https://www.qt.io/terms-conditions. For further ## information use the contact form at https://www.qt.io/contact-us. ## ## GNU General Public License Usage ## Alternatively, this file may be used under the terms of the GNU ## General Public License version 3 as published by the Free Software ## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT ## included in the packaging of this file. Please review the following ## information to ensure the GNU General Public License requirements will ## be met: https://www.gnu.org/licenses/gpl-3.0.html. ## ## $QT_END_LICENSE$ ## ############################################################################# from helper import adjust_filename, UsesQApplication from PySide2.QtGui import QGuiApplication from PySide2.QtQuick import QQuickView app = QGuiApplication([]) view = QQuickView(adjust_filename('bug_995.qml', __file__)) view.show() view.resize(200, 200) contentItem = view.contentItem() item = contentItem.childAt(100, 100) # it CAN NOT crash here print(item)
if role == Qt.DisplayRole: return d['name'] elif role == Qt.DecorationRole: return Qt.black elif role == PersonModel.MyRole: return d['myrole'] return None def populate(self): self._data.append({'name': 'Qt', 'myrole': 'role1'}) self._data.append({'name': 'PySide', 'myrole': 'role2'}) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) myModel = PersonModel() myModel.populate() view.rootContext().setContextProperty("myModel", myModel) view.setSource('view.qml') view.show() app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view
import sys import os import PySide2 from PySide2.QtWidgets import QApplication from PySide2.QtCore import QUrl, QObject from PySide2.QtQuick import QQuickView if __name__ == "__main__": app = QApplication(sys.argv) view = QQuickView() qml = QUrl("mainwindow.qml") view.setSource(qml) view.show() root = view.rootObject() text = root.findChild(QObject, "mytext") button = root.findChild(QObject, "mybutton") button.clicked.connect(lambda: text.setProperty("text", "Hello Qt World!")) sys.exit(app.exec_())
def testIncubateWhileCall(self): app = QGuiApplication(sys.argv) view = QQuickView() controller = CustomIncubationController(self) view.engine().setIncubationController(controller) view.setResizeMode(QQuickView.SizeRootObjectToView) view.setSource( QUrl.fromLocalFile( adjust_filename('qqmlincubator_incubateWhile.qml', __file__))) view.show() root = view.rootObject() # The QML code will issue an interrupt signal after half of its items are loaded. root.shouldInterrupt.connect(controller.interrupter) res = app.exec_() itemsToCreate = root.property("itemsToCreate") loadedItems = root.property("loadedItems") self.assertEqual(loadedItems, itemsToCreate / 2) # Finish incubating the remaining items. controller.incubateFor(1000) loadedItems = root.property("loadedItems") self.assertEqual(loadedItems, itemsToCreate) # Deleting the view before it goes out of scope is required to make sure all child QML # instances are destroyed in the correct order. del view del app
from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl from controller import CalculatorController if getattr(sys, 'frozen', False): current_dir = sys._MEIPASS else: current_dir = os.path.dirname(os.path.abspath(__file__)) os.environ["QT_QUICK_CONTROLS_CONF"] = os.path.join(current_dir, "view", "qtquickcontrols2.conf") os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1" app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) import_path = os.path.join(current_dir, "view", "imports") view.engine().addImportPath(import_path) controller = CalculatorController() view.rootContext().setContextProperty("controller", controller) filename = os.path.join(current_dir, "view", "Main.qml") url = QUrl.fromLocalFile(filename) view.setSource(url) if view.status() == QQuickView.Error: sys.exit(-1)
# print(index.row(),role) # Print requested row and role for debugging if role == QtCore.Qt.DisplayRole: # On DisplayRole return name return self.city_list[index.row()]["muniLabel"] elif role == self.Roles.LOCATION.value: # On location role return coordinates return self.city_list[index.row()]["location"] elif role == self.Roles.AREA.value: # On area role return area return self.city_list[index.row()]["area"] elif role == self.Roles.POPULATION.value: # On population role return population return self.city_list[index.row()]["population"] def roleNames(self) -> typing.Dict[int, QByteArray]: """Returns dict with role numbers and role names for default and custom roles together""" # Append custom roles to the default roles and give them names for a usage in the QML roles = super().roleNames() roles[self.Roles.LOCATION.value] = QByteArray(b'location') roles[self.Roles.AREA.value] = QByteArray(b'area') roles[self.Roles.POPULATION.value] = QByteArray(b'population') print(roles) return roles app = QGuiApplication(sys.argv) view = QQuickView() url = QUrl(VIEW_URL) citylist_model = CityListModel(CITY_LIST_FILE) ctxt = view.rootContext() ctxt.setContextProperty('cityListModel',citylist_model) view.setSource(url) view.show() app.exec_()
from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl import sys import os app = QApplication([]) view = QQuickView() current_path = os.path.abspath(os.path.dirname(__file__)) qml_file = os.path.join(current_path, 'view.qml') url = QUrl(qml_file) view.setSource(url) #view.setSource(QUrl.fromLocalFile(qml_file)) if view.status() == QQuickView.Error: sys.exit(-1) # When programming for Desktop: view.setResizeMode( QQuickView.SizeRootObjectToView ) # force the outer QML rectangle to resize along with the outer window view.show() res = app.exec_() # To ensure the correct destruction order, del must be invoked on the view object # before quitting the application. del view sys.exit(res)
import os import sys from PySide2.QtCore import QUrl from PySide2.QtGui import QGuiApplication import PySide2.QtQml from PySide2.QtQuick import QQuickView # This example uses a QML file to show a scrolling list containing # all the items listed in dataList. if __name__ == '__main__': dataList = ["Item 1", "Item 2", "Item 3", "Item 4"] app = QGuiApplication(sys.argv) view = QQuickView() ctxt = view.rootContext() ctxt.setContextProperty("myModel", dataList) qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) view.show() app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view
#!/bin/python import sys from OutWritter import OutWritter from file import read_file from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl, QTimer app = QApplication([]) writter = OutWritter() view = QQuickView() url = QUrl("components/list_view.qml") view.setResizeMode(QQuickView.SizeRootObjectToView) view.setSource(url) timer = QTimer() timer.timeout.connect(lambda: None) timer.start(100) list = view.rootObject() list.setProperty('entries', read_file("./config/bookmarks")) context = view.rootContext() context.setContextProperty("out", writter) view.show() sys.exit(app.exec_())
def setName(self, value): self._name = value name = Property(text_type, getName, setName) def getPieSlice(self): return self._pieSlice def setPieSlice(self, value): self._pieSlice = value self._pieSlice.setParentItem(self) pieSlice = Property(PieSlice, getPieSlice, setPieSlice) if __name__ == '__main__': app = QGuiApplication(sys.argv) qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart') qmlRegisterType(PieSlice, "Charts", 1, 0, "PieSlice") view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) view.setSource(QUrl.fromLocalFile('app.qml')) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)
@Slot('double') def output(self, s): print(s) @Slot(str) def outputStr(self, s): print(s) @Slot('double') def outputFloat(self, x): print(x) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() # Instantiate the Python object. con = Console() # Expose the object to QML. context = view.rootContext() context.setContextProperty("con", con) qml_file = os.fspath(Path(__file__).resolve().parent / 'qml_test2.qml') view.setSource(qml_file) if view.status() == QQuickView.Error: sys.exit(-1) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances
import os import sys from PySide2.QtCore import QObject, QUrl from PySide2.QtGui import QGuiApplication import PySide2.QtQml from PySide2.QtQuick import QQuickView def sayThis(s): print(s) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) root = view.rootObject() button = root.findChild(QObject, "buttonMouseArea") button.clicked.connect( lambda: sayThis("clicked button (signal directly connected)")) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view
# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. from PySide2.QtCore import SIGNAL, SLOT, QObject from PySide2.QtGui import QGuiApplication from PySide2.QtQuick import QQuickView from PySide2.QtQml import QQmlEngine from PySide2.QtCore import QUrl url = QUrl("generate_raster_assets.qml") app = QGuiApplication([]) view = QQuickView() engine = view.engine() QObject.connect(engine, SIGNAL("quit()"), app, SLOT("quit()")) view.setSource(url) view.show() app.exec_()
def main(argv): data = [ [4, 9, 2], [1, 0, 0], [3, 5, 0], [3, 3, 2], [7, 8, 9], ] myModel = BasicModel(data) app = QtWidgets.QApplication(argv) view = QQuickView() view.rootContext().setContextProperty("myModel", myModel) view.setResizeMode(QQuickView.SizeRootObjectToView) view.resize(640, 480) url = QtCore.QUrl("table.qml") result = view.setSource(url) # TODO somehow connect myModel python to QML Table view. view.show() sys.exit(app.exec_())
def main(): argv = sys.argv app = QGuiApplication(argv) qmlRegisterType(FigureCanvasQTAggToolbar, "Backend", 1, 0, "FigureToolbarByPython") # this should work in the future # qmlRegisterType( # QUrl.fromLocalFile( str(pathlib.Path(backend_qquick5agg.__file__)/'SubplotTool.qml')), # "Backend", 1, 0, "SubplotTool") imgProvider = MatplotlibIconProvider() view = QQuickView() view.engine().addImageProvider("mplIcons", imgProvider) view.setResizeMode(QQuickView.SizeRootObjectToView) view.setSource( QUrl(str(pathlib.Path(__file__).parent / 'FigureToolbar.qml'))) win = view.rootObject() fig = win.findChild(QObject, "figure").getFigure() ax = fig.add_subplot(111) x = np.linspace(-5, 5) ax.plot(x, np.sin(x)) view.show() rc = app.exec_() # There is some trouble arising when deleting all the objects here # but I have not figure out how to solve the error message. # It looks like 'app' is destroyed before some QObject sys.exit(rc)
# This Python file uses the following encoding: utf-8 import os import sys from PyQt5 import QtWidgets from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QStringListModel, Qt, QUrl from PySide2.QtGui import QGuiApplication from PySide2.QtQml import QQmlApplicationEngine # from style_rc import * if __name__ == "__main__": app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) qmlFile = os.path.join(os.path.dirname(__file__), "view.qml") view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) # Show the window if view.status() == QQuickView.Error: sys.exit(-1) view.showMaximized() # view.show() # execute main loop and cleanup app.exec_() del view
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ## ## $QT_END_LICENSE$ ## ############################################################################# """PySide2 port of the QML Polar Chart Example from Qt v5.x""" import sys import os from PySide2.QtQuick import QQuickView from PySide2.QtCore import Qt, QUrl from PySide2.QtWidgets import QApplication, QMainWindow if __name__ == '__main__': app = QApplication(sys.argv) viewer = QQuickView() viewer.engine().addImportPath(os.path.dirname(__file__)) viewer.engine().quit.connect(viewer.close) viewer.setTitle = "QML Polar Chart" qmlFile = os.path.join(os.path.dirname(__file__), 'main.qml') viewer.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) viewer.setResizeMode(QQuickView.SizeRootObjectToView) viewer.show() sys.exit(app.exec_())
# This Python file uses the following encoding: utf-8 import sys from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl ''' class CupPong(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.setSource(QUrl('qrc:///main.qml')) def init_ui(self): self.resize(480, 320) ''' if __name__ == "__main__": app = QApplication([]) #window = CupPong() #window.show() view = QQuickView() view.setSource(QUrl('main.qml')) view.setResizeMode(QQuickView.SizeViewToRootObject) view.show() sys.exit(app.exec_())
if role == Qt.DisplayRole: return d['name'] elif role == Qt.DecorationRole: return Qt.black elif role == PersonModel.MyRole: return d['myrole'] return None def populate(self): self._data.append({'name':'Qt', 'myrole':'role1'}) self._data.append({'name':'PySide', 'myrole':'role2'}) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) myModel = PersonModel() myModel.populate() view.rootContext().setContextProperty("myModel", myModel) qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) view.show() app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order.
import sys from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl if __name__ == '__main__': app = QApplication( []) # can use sys.argv in place of [] for command line arguments view = QQuickView() url = QUrl('basic_qml.qml') view.setSource(url) view.setResizeMode( QQuickView.SizeRootObjectToView) # makes the view scale w/ the window view.show() sys.exit(app.exec_()) # run the app until closed
self._name = value colorChanged = Signal() color = Property(QColor, getColor, setColor, notify=colorChanged) name = Property(text_type, getName, setName) chartCleared = Signal() @Slot() # This should be something like @Invokable def clearChart(self): self.setColor(Qt.transparent) self.update() self.chartCleared.emit() if __name__ == '__main__': app = QGuiApplication(sys.argv) qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart'); view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) qmlFile = os.path.join(os.path.dirname(__file__), 'app.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)
from helper import adjust_filename, UsesQApplication from PySide2.QtGui import QGuiApplication from PySide2.QtQuick import QQuickView app = QGuiApplication([]) view = QQuickView(adjust_filename('bug_995.qml', __file__)) view.show() view.resize(200, 200) # TODO: is there QQuick alternative to tis? item = view.itemAt(100, 100) # it CAN NOT crash here print(item)
import sys from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl app = QApplication([]) view = QQuickView() url = QUrl("view.qml") view.setSource(url) view.show() sys.exit(app.exec_())
from PySide2.QtWidgets import QApplication from PySide2.QtCore import QObject from PySide2.QtQuick import QQuickView from PySide2.QtQml import QQmlApplicationEngine from PySide2.QtCore import QUrl app = QApplication([]) view = QQuickView() view.engine().addImportPath("./Modules") url = QUrl("demo/view.qml") view.setSource(url) view.show() app.exec_()
# -*- coding: utf-8 -*- from PySide2.QtCore import QUrl, QObject, Slot from PySide2.QtGui import QGuiApplication from PySide2.QtQuick import QQuickView class MyClass(QObject): @Slot(str) # 输入参数为str类型 def outputString(self, string): """ 功能: 创建一个槽 参数: 输出的数据string 返回值: 无 """ print(string) if __name__ == '__main__': path = 'test.qml' # 加载的QML文件 app = QGuiApplication([]) view = QQuickView() con = MyClass() context = view.rootContext() context.setContextProperty("con", con) view.engine().quit.connect(app.quit) view.setSource(QUrl(path)) view.show() app.exec_()
cart = Cart() uidmap = UidService() app = QGuiApplication(sys.argv) for trfile in ['view_de.qm']: translator = QTranslator() translator.load(os.path.join("i18n", trfile)) app.installTranslator(translator) font = QFont("Helvetica", 16) app.setFont(font) view = QQuickView() view.width = 800 view.height = 460 view.setResizeMode(QQuickView.SizeRootObjectToView) # Expose the data to the Qml code ctx = view.rootContext() ctx.setContextProperty("drinks", drinks) ctx.setContextProperty("cart", cart) ctx.setContextProperty("logbook", cart.log) ctx.setContextProperty("uidmap", uidmap) ctx.setContextProperty("dayFee", DAY_FEE) qml_file = os.path.join(os.path.dirname(__file__), "view.qml") view.setSource(QUrl.fromLocalFile(os.path.abspath(qml_file)))
from PySide2.QtWidgets import QApplication from PySide2.QtQuick import QQuickView from PySide2.QtCore import QUrl, QObject, Slot class Tunnel(QObject): @Slot(str) def on_ticket(self, ticket): print("Got ticket: %s" % (ticket)) app = QApplication([]) tunnel = Tunnel() view = QQuickView() view.rootContext().setContextProperty("tunnel", tunnel) url = QUrl("view.qml") view.setSource(url) view.show() app.exec_()
from __future__ import print_function import os import sys from PySide2.QtCore import QObject, QUrl from PySide2.QtGui import QGuiApplication import PySide2.QtQml from PySide2.QtQuick import QQuickView def sayThis(s): print(s) if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() qmlFile = os.path.join(os.path.dirname(__file__), 'view.qml') view.setSource(QUrl.fromLocalFile(os.path.abspath(qmlFile))) if view.status() == QQuickView.Error: sys.exit(-1) root = view.rootObject() root.textRotationChanged.connect(sayThis) root.buttonClicked.connect(lambda: sayThis("clicked button (QML top-level signal)")) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)
class RotateValue(QObject): def __init__(self): super(RotateValue, self).__init__() self.r = 0 # If a slot returns a value, the return value type must be explicitly # defined in the decorator. @Slot(result=int) def val(self): self.r = self.r + 10 return self.r if __name__ == '__main__': app = QGuiApplication(sys.argv) view = QQuickView() rotatevalue = RotateValue() context = view.rootContext() context.setContextProperty("rotatevalue", rotatevalue) view.setSource(QUrl('view.qml')) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)
def __init__(self): QQuickView.__init__(self) self.setSource(QUrl.fromLocalFile(adjust_filename('bug_847.qml', __file__))) self.rootObject().setProperty('pythonObject', self)
name = Property(text_type, getName, setName) def getPieSlice(self): return self._pieSlice def setPieSlice(self, value): self._pieSlice = value self._pieSlice.setParentItem(self) pieSlice = Property(PieSlice, getPieSlice, setPieSlice) if __name__ == '__main__': app = QGuiApplication(sys.argv) qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart'); qmlRegisterType(PieSlice, "Charts", 1, 0, "PieSlice"); view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) qmlFile = os.path.join(os.path.dirname(__file__), 'app.qml') view.setSource(QUrl.fromLocalFile(qmlFile)) if view.status() == QQuickView.Error: sys.exit(-1) view.show() res = app.exec_() # Deleting the view before it goes out of scope is required to make sure all child QML instances # are destroyed in the correct order. del view sys.exit(res)