def updatePaintNode(self, oldNode, updatePaintData): node = None geo = None if not oldNode: node = QtQuick.QSGGeometryNode() geo = QtQuick.QSGGeometry(QtQuick.QSGGeometry.defaultAttributes_Point2D(), self._count) geo.setLineWidth(2) geo.setDrawingMode(QtQuick.QSGGeometry.DrawLineStrip) node.setGeometry(geo) node.setFlag(QtQuick.QSGNode.OwnsGeometry) else: node = oldNode geo = node.geometry() geo.allocate(self._count) itemSize = self.size() verts = geo.vertexData() # 出现了bug,这里pyside返回不正常 for x in xrange(self._count): t = x / (self._count - 1) invt = 1 - t pos = invt * invt * invt * self._p1 + 3 * invt * invt * t * self._p2 + 3 * invt * t * t * self._p3 + t * t * t * self._p4 x = pos.x() * itemSize.width() y = pos.y() * itemSize.height() verts[x].set(x, y) node.markDirty(QtQuick.QSGNode.DirtyGeometry) return node
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 updatePaintNode(self, old_node, data): if self.__has_image: if self.__node is None: self.__node = QtQuick.QSGNode() new_texture_node = QtQuick.QSGSimpleTextureNode() self.__node.appendChildNode(new_texture_node) texture_node = self.__node.firstChild() if self.__should_refresh_node_texture: new_texture = self.window().createTextureFromImage( self.__image) texture_node.setFiltering(QtQuick.QSGTexture.Linear) texture_node.setTexture(new_texture) self.__should_refresh_node_texture = False # Get size values for aspect ratio calculation bounding_rect = self.boundingRect() texture_size = texture_node.texture().textureSize() # Make the texture node (which is a child of the main node) fill the full component bounds texture_node.setRect(bounding_rect) if ( texture_size.width() > texture_size.height() ): # Account for Spotify images that are wider than their container # Calculate portion of texture to use to correct for container's aspect ratio slice_width = (bounding_rect.width() / bounding_rect.height()) * texture_size.height() slice_x = (texture_size.width() / 2) - (slice_width / 2) # Use calculated slice as area of texture to apply to the texture node texture_node.setSourceRect(slice_x, 0, slice_width, texture_size.height()) else: # Account for album images being applied to a wider container slice_height = (bounding_rect.height() / bounding_rect.width()) * texture_size.width() slice_y = (texture_size.height() / 2) - (slice_height / 2) texture_node.setSourceRect(0, slice_y, texture_size.width(), slice_height) return self.__node
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_()