def _load_qml(self, engine, qml_file): self._component = QtQml.QQmlComponent(engine) url = QtCore.QUrl( os.path.dirname(os.path.realpath(__file__)) + "/" + qml_file) # Apparently QQmlEngine caches loaded components so there shouldn't # be a big performance penalty creating many instances of the same # component using this code. self._component.loadUrl(url, QtQml.QQmlComponent.PreferSynchronous) if self._component.isError(): for error in self._component.errors(): print(error.toString())
def initWebengine(self): component = QtQml.QQmlComponent(self.engine()) component.setData(''' import QtQuick 2.4 import WebEngineWrapper 1.0 WebEngineWrapper { Component.onCompleted: { initialize() } } ''', QtCore.QUrl('')) item = component.create() item.setParentItem(self.rootObject())
def main(): app = QtWidgets.QApplication(sys.argv) view = QtQuick.QQuickView() view.setResizeMode(QtQuick.QQuickView.SizeRootObjectToView) # Create a declarative view component = QtQml.QQmlComponent(view) topLevelItem = component.create() qmlFile = QtCore.QUrl("source.qml") view.setSource(qmlFile) # Declare we are using instant coding tool on this view qic = QmlInstantCoding(view, ReloadComponent(qmlFile, component, topLevelItem), verbose=True) # Add any source file (.qml and .js by default) in current working directory qic.addFilesFromDirectory(os.getcwd()) view.show() app.exec_()
osname = os.name.lower() sysplatform = sys.platform.lower() windows = os.name.lower() == "nt" and sysplatform.startswith("win") # PyQt class name, QML URI, major version, minor version, QML type name QtQml.qmlRegisterType(MyModel, 'MyModel', 1, 0, 'MyModel') QtQml.qmlRegisterType(MyItem, 'MyItem', 1, 0, 'MyItem') app = QtWidgets.QApplication(sys.argv) # Create the QML engine engine = QtQml.QQmlEngine(app) engine.quit.connect(app.quit) # Load the main.qml file and create the toplevel component component = QtQml.QQmlComponent(engine) currentFilePath = os.path.dirname(os.path.abspath(__file__)) mainFilepath = os.path.join(currentFilePath, "main.qml") if windows: mainFilepath = mainFilepath.replace('\\', '/') qmlFile = QtCore.QUrl("file:///" + mainFilepath) component.loadUrl(qmlFile) if component.status() != QtQml.QQmlComponent.Ready: for error in component.errors(): print(error.toString()) sys.exit(-1) topLevelItem = component.create() if not topLevelItem: for error in component.errors(): print(error.toString())