def testQMLFunctionCall(self): app = QtGui.QGuiApplication(sys.argv) view = QtQuick.QQuickView() obj = PythonObject() context = view.rootContext() context.setContextProperty("python", obj) view.setSource( QtCore.QUrl.fromLocalFile(adjust_filename('bug_451.qml', __file__))) root = view.rootObject() root.simpleFunction() self.assertEqual(obj.called, "simpleFunction") root.oneArgFunction(42) self.assertEqual(obj.called, "oneArgFunction") self.assertEqual(obj.arg1, 42) root.twoArgFunction(10, app) self.assertEqual(obj.called, "twoArgFunction") self.assertEqual(obj.arg1, 10) self.assertEqual(obj.arg2, app) rvalue = root.returnFunction() self.assertEqual(obj.called, "returnFunction") self.assertEqual(rvalue, 42)
def testSlotRetur(self): view = QtQuick.QQuickView() proxy = ProxyObject() context = view.rootContext() context.setContextProperty("proxy", proxy) view.setSource(QtCore.QUrl.fromLocalFile(adjust_filename('bug_726.qml', __file__))) root = view.rootObject() button = root.findChild(QtCore.QObject, "buttonMouseArea") view.show() button.entered.emit() self.assertEqual(proxy._receivedName, "PySideObject")
def testFailConnection(self): self.buttonClicked = False self.buttonFailClicked = False view = QtQuick.QQuickView() view.setSource(QtCore.QUrl.fromLocalFile(adjust_filename('connect_python_qml.qml', __file__))) root = view.rootObject() button = root.findChild(QtCore.QObject, "buttonMouseArea") self.assertRaises(TypeError, QtCore.QObject.connect, [button,QtCore.SIGNAL('entered()'), self.onButtonFailClicked]) button.entered.connect(self.onButtonClicked) button.entered.emit() view.show() self.app.exec_() self.assertTrue(self.buttonClicked)
def testQMLFunctionCall(self): ownerData = QtQml.QQmlPropertyMap() ownerData.insert('name', 'John Smith') ownerData.insert('phone', '555-5555') ownerData.insert('newValue', '') view = QtQuick.QQuickView() ctxt = view.rootContext() ctxt.setContextProperty('owner', ownerData) view.setSource( QtCore.QUrl.fromLocalFile(adjust_filename('bug_997.qml', __file__))) view.show() QtCore.QTimer.singleShot(1000, self.app.quit) self.app.exec_() self.assertEqual(ownerData.value('newName'), ownerData.value('name'))
def testSlotRetur(self): view = QtQuick.QQuickView() rotatevalue = RotateValue() timer = QtCore.QTimer() timer.start(2000) context = view.rootContext() context.setContextProperty("rotatevalue", rotatevalue) view.setSource( QtCore.QUrl.fromLocalFile(adjust_filename('bug_456.qml', __file__))) root = view.rootObject() button = root.findChild(QtCore.QObject, "buttonMouseArea") view.show() button.entered.emit() self.assertEqual(rotatevalue.rotation, 100)
def testQQuickItemGrabToImageSharedPointer(self): view = QtQuick.QQuickView() view.setSource( QtCore.QUrl.fromLocalFile( adjust_filename('qquickitem_grabToImage.qml', __file__))) view.show() # Get the QQuickItem objects for the blue Rectangle and the Image item. root = view.rootObject() blueRectangle = root.findChild(QtQuick.QQuickItem, "blueRectangle") imageContainer = root.findChild(QtQuick.QQuickItem, "imageContainer") # Start the image grabbing. grabResultSharedPtr = blueRectangle.grabToImage() # Implicit call of operator bool() of the smart pointer, to check that it holds # a valid pointer. self.assertTrue(grabResultSharedPtr) self.grabbedColor = None def onGrabReady(): # Signal early exit. QtCore.QTimer.singleShot(0, self.app.quit) # Show the grabbed image in the QML Image item. imageContainer.setProperty("source", grabResultSharedPtr.url()) # Wait for signal when grabbing is complete. grabResultSharedPtr.ready.connect(onGrabReady) self.app.exec_() # Get the first pixel color of the grabbed image. self.image = grabResultSharedPtr.image() self.assertTrue(self.image) self.grabbedColor = self.image.pixelColor(0, 0) self.assertTrue(self.grabbedColor.isValid()) # Compare the grabbed color with the one we set in the rectangle. blueColor = QtGui.QColor("blue") self.assertEqual(self.grabbedColor, blueColor)
# coding: utf-8 from PySide2 import QtWidgets, QtCore, QtGui, QtQuick, QtQml import sys import os from resources import qmlB if __name__ == '__main__': # app = QtWidgets.QApplication(sys.argv[1:]) app = QtGui.QGuiApplication(sys.argv[1:]) # import path 是qml中用来improt 的路径,类似python sys.path # plugin path 是能读到qmdir的路径 # root是item 只能用view来启动 view = QtQuick.QQuickView() view.setResizeMode(QtQuick.QQuickView.SizeRootObjectToView) view.engine().addImportPath(os.path.join(os.path.dirname(__file__), 'imports')) view.setSource("qrc:/main2.qml") #注意,qrc中的qml,貌似存在刷新问题,建议直接读取qml文件 view.show() sys.exit(app.exec_())
def initialize(self): # create a MonitorModel to communicate with the QML view self.monitor_model = MonitorModel() self.monitors = self.monitor_model.monitors # create a SettingsModel to communicate with the settings drawer # in the QML view self.settings_model = SettingsModel(self) # connect the statAdded and statRemoved signals self.settings_model.statAdded.connect(self.add_stat) self.settings_model.statRemoved.connect(self.remove_stat) if self.qapp is None: self.qapp = QtWidgets.QApplication(sys.argv) # add custom fonts font_db = QtGui.QFontDatabase() font_paths = [ self.get_asset_path('Raleway-Regular.ttf'), self.get_asset_path('RobotoMono-Regular.ttf') ] for font_path in font_paths: font_id = font_db.addApplicationFont(font_path) if font_id == -1: logging.warn(f'Could not load font ({font_path})') font = QtGui.QFont('Raleway') self.qapp.setFont(font) # set favicon icon_info = [('icons/favicon-16x16.png', (16, 16)), ('icons/favicon-32x32.png', (32, 32)), ('icons/android-chrome-192x192.png', (192, 192)), ('icons/android-chrome-256x256.png', (256, 256))] app_icon = QtGui.QIcon() for path, size in icon_info: app_icon.addFile(self.get_asset_path(path), QtCore.QSize(*size)) self.qapp.setWindowIcon(app_icon) for stat in self.initial_stats: self.add_stat(stat, add_to_config=False) view = QtQuick.QQuickView() view.setResizeMode(QtQuick.QQuickView.SizeRootObjectToView) root_context = view.rootContext() # make monitor model and settings model available in QML root_context.setContextProperty('monitorModel', self.monitor_model) root_context.setContextProperty('settingsModel', self.settings_model) # qml/view.qml is the root QML file qml_file = os.path.join(os.path.dirname(__file__), 'qml', 'view.qml') view.setSource(QtCore.QUrl.fromLocalFile(os.path.abspath(qml_file))) if view.status() == QtQuick.QQuickView.Error: sys.exit(-1) def signal_handler(signal, frame): # the app can not gracefully quit # when there is a keyboard interrupt # because the QAbstractListModel catches all errors # in a part of its code print() os._exit(0) signal.signal(signal.SIGINT, signal_handler) view.show() self.qapp.exec_() self.quit()
import PySide2.QtWidgets as Qw import PySide2.QtQuick as Qq import PySide2.QtCore as Qc app = Qw.QApplication([]) view = Qq.QQuickView() url = Qc.QUrl('view.qml') # Expand green rectangle with the View view.setResizeMode(Qq.QQuickView.SizeRootObjectToView) view.setSource(url) view.show() app.exec_()