def main(): app = QApplication([]) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) # QML経由でアクセスするPythonTextクラスのインスタンスを生成する python_text = PythonText() # QMLのrootアイテムのコンテキストを取得する context = view.rootContext() """ QMLの rootアイテムにQML側からアクセスするPythonのクラスを登録する PySide2.QtQml.QQmlContext.setContextProperty(arg__1, arg__2) arg__1 – QML側からアクセスするためのQMLオブジェクト名 arg__2 - QML側からアクセスさせるPythonクラス """ context.setContextProperty("pythonText", python_text) url = QUrl("HelloWorld.qml") view.setSource(url) if view.status() == QQuickView.Error: sys.exit(-1) view.show() ret = app.exec_() del view sys.exit(ret)
def main(): qInstallMessageHandler(lambda x, y, msg: print(msg)) argv = sys.argv # Trick to set the style / not found how to do it in pythonic way #argv.extend(["-style", "universal"]) app = QGuiApplication(argv) qmlRegisterType(FigureCanvasQTAgg, "Backend", 1, 0, "FigureCanvasByPython") view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) view.setSource(QUrl(str(pathlib.Path(__file__).parent / 'Figure.qml'))) view.show() win = view.rootObject() qml_figure_canvas = win.findChild(QObject, "figure") fig = qml_figure_canvas.getFigure() print(fig) ax = fig.add_subplot(111) x = np.linspace(-5, 5) ax.plot(x, np.sin(x)) 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)
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 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
def main(): app = QApplication([]) """ 指定したクラスを、QMLモジュールのQMLタイプとしてバインディングする qmlRegisterType(class, uri, versionMajor, versionMinor, qmlName) class - QML側でQML Type定義するPythonクラス uri - 定義するQMLモジュール名 versionMajor - QMLモジュールのメジャーバージョン versionMinor - QMLモジュールのマイナーバージョン qmlName - 定義するQMLタイプ名 """ qmlRegisterType(PythonText, "FromPythonTextLibrary", 1, 0, "FromPythonText") view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) url = QUrl("HelloWorld.qml") view.setSource(url) if view.status() == QQuickView.Error: sys.exit(-1) view.show() ret = app.exec_() del view sys.exit(ret)
def brunchmark(): app = QApplication([]) view = QQuickView() url = QUrl("./brunchmark/brunchmark.qml") view.setSource(url) view.show() app.exec_() # app = QApplication([]) # engine = QQmlApplicationEngine() # engine.load("./brunchmark/brunchmark.qml") # win = engine.rootObjects()[0] # win.show() # sys.exit(app.exec_()) # if __name__ == "__main__": # app = QApplication(sys.argv) # widget = MyWidget() # widget.resize(800, 600) # widget.show() # sys.exit(app.exec_())
def testAbstractItemModelTransferToQML(self): view = QQuickView() model = ListModel() view.rootContext().setContextProperty("pythonModel", model) view.setSource(QUrl.fromLocalFile(adjust_filename('bug_814.qml', __file__))) root = view.rootObject() view.show()
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)
def main(): '''main entry point''' app = QApplication([]) view = QQuickView() view.setSource(QUrl("view.qml")) view.show() app.exec_()
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 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 main(argv): app = QtWidgets.QApplication(argv) view = QQuickView() url = QtCore.QUrl("basic.qml") view.setSource(url) view.show() sys.exit(app.exec_())
def hello_QML(): app = QApplication([]) view = QQuickView() url = QUrl("view.qml") view.setSource(url) view.show() app.exec_()
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 run(): app = QApplication(sys.argv) view = QQuickView() url = QUrl(os.path.join(os.path.dirname(__file__), 'view.qml')) view.setSource(url) view.setResizeMode(QQuickView.SizeRootObjectToView) view.show() app.exec_()
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 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 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_()
def testModelExport(self): view = QQuickView() dataList = [MyObject("Item 1"), MyObject("Item 2"), MyObject("Item 3"), MyObject("Item 4")] ctxt = view.rootContext() ctxt.setContextProperty("myModel", dataList) url = QUrl.fromLocalFile(adjust_filename('viewmodel.qml', __file__)) view.setSource(url) view.show() self.assertEqual(view.status(), QQuickView.Ready)
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 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): global paintCalled app = QGuiApplication([]) qmlRegisterType(Bug825, 'bugs', 1, 0, 'Bug825') self.assertRaises(TypeError, qmlRegisterType, A, 'bugs', 1, 0, 'A') view = QQuickView() view.setSource(QUrl.fromLocalFile(adjust_filename('bug_825.qml', __file__))) view.show() QTimer.singleShot(250, view.close) app.exec_() self.assertTrue(paintCalled)
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 testQQuickNetworkFactory(self): view = QQuickView() self.factory = CustomFactory() view.engine().setNetworkAccessManagerFactory(self.factory) url = QUrl.fromLocalFile(adjust_filename('hw.qml', __file__)) view.setSource(url) view.show() self.assertEqual(view.status(), QQuickView.Ready) self.app.exec_()
def testSignalArguments(self): view = QQuickView() obj = Obj() context = view.rootContext() context.setContextProperty("o", obj) view.setSource(QUrl.fromLocalFile(adjust_filename('signal_arguments.qml', __file__))) root = view.rootObject() self.assertTrue(root) button = root.findChild(QObject, "button") self.assertTrue(button) view.show() button.clicked.emit() self.assertEqual(obj.value, 42)
def testIt(self): app = QGuiApplication([]) qmlRegisterType(PieChart, 'Charts', 1, 0, 'PieChart') qmlRegisterType(PieSlice, "Charts", 1, 0, "PieSlice") view = QQuickView() view.setSource( QUrl.fromLocalFile(adjust_filename('registertype.qml', __file__))) view.show() QTimer.singleShot(250, view.close) app.exec_() self.assertTrue(appendCalled) self.assertTrue(paintCalled)
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 testQQuickViewList(self): view = QQuickView() dataList = ["Item 1", "Item 2", "Item 3", "Item 4"] ctxt = view.rootContext() ctxt.setContextProperty("myModel", dataList) url = QUrl.fromLocalFile(adjust_filename('view.qml', __file__)) view.setSource(url) view.show() self.assertEqual(view.status(), QQuickView.Ready) rootObject = view.rootObject() self.assertTrue(rootObject) self.assertTrue(QtQml.qmlEngine(rootObject)) self.assertTrue(QtQml.qmlContext(rootObject))
def main(): app = QApplication([]) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) url = QUrl("HelloWorld.qml") view.setSource(url) if view.status() == QQuickView.Error: sys.exit(-1) view.show() ret = app.exec_() del view sys.exit(ret)
class ppGCSMain(QWidget): def __init__(self): super().__init__() self.setWindowTitle("pppGCS : Plural Python Parrot GCS") self.DeviceInfo = ppDeviceInfoWidget() self.DeviceList = ppDiscoverWidget() self.leftcol_layout = QVBoxLayout() self.leftcol_layout.addWidget(self.DeviceInfo) self.leftcol_layout.addWidget(self.DeviceList) self.VideoViewer = ppVideoWidget() self.tabs = QTabWidget() self.tabs.addTab(self.VideoViewer, "Video stream") self.mapview = QQuickView() self.mapcontainer = QWidget.createWindowContainer(self.mapview, self) url = QUrl("map.qml") self.mapview.setSource(url) self.mapview.show() self.tabs.addTab(self.mapcontainer, "map view") self.FlightInfo = ppFlightInfoWidget() self.ControlInfo = ppControlWidget() self.rightcol_layout = QVBoxLayout() self.rightcol_layout.addWidget(self.tabs) self.lowrightcol_layout = QHBoxLayout() self.lowrightcol_layout.addWidget(self.FlightInfo) self.lowrightcol_layout.addWidget(self.ControlInfo) self.rightcol_layout.addLayout(self.lowrightcol_layout) self.layout = QHBoxLayout() self.layout.addLayout(self.leftcol_layout) self.layout.addLayout(self.rightcol_layout) self.setLayout(self.layout) self.DeviceList.DeviceChanged.connect(self.on_device_changed) def on_device_changed(self, device): self.VideoViewer.close_video() self.VideoViewer.open_video(device.sdpfile) self.DeviceInfo.device = device self.FlightInfo.device = device self.ControlInfo.device = device
def main(): """ Entry point into program """ app = QGuiApplication(sys.argv) # Register the Backend which can be used in the qml view qmlRegisterType(Backend, 'Interface', 1, 0, 'Backend') # Create a quickview widget and load the view view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) view.setSource(QUrl.fromLocalFile('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. # Sept 2019 * Not sure if we still need to do this * del view sys.exit(res)
class QuickWindow(MayaQWidgetBaseMixin, QMainWindow): """A window that can be used to display a QML UI in Maya.""" def __init__(self, url=None, *args, **kwargs): super(QuickWindow, self).__init__(*args, **kwargs) self.view = QQuickView() self.view.statusChanged.connect(self.onStatusChanged) self.widget = None if url: self.setSource(url) self.setFocusPolicy(Qt.NoFocus) def setContextProperty(self, name, value): """Set a context property on the window. Context properties are used to handle the interop between QML and Python :param name: Name of the property :param value: Instance of the property object """ self.view.engine().rootContext().setContextProperty(name, value) def addImportPath(self, path): """Add a new module import path to the window engine.""" self.view.engine().addImportPath(path) def setSource(self, url): if isinstance(url, string_types): url = QUrl.fromLocalFile(os.path.abspath(url)) self.view.setSource(url) size = self.view.size() self.widget = QWidget.createWindowContainer(self.view, self) self.setCentralWidget(self.widget) self.setFocusProxy(self.widget) self.resize(size) def onStatusChanged(self, status): if status == QQuickView.Error: errors = self.view.errors() for error in errors: logging.critical(error)
def main(): """ .qml ファイルのロードする準備 QGuiApplication と QQuickView のインスタンスを生成する """ app = QApplication([]) view = QQuickView() """ Qt Quickの表示方法の設定 PySide2.QtQuick.QQuickView.ResizeMode の enum 定義 表示サイズはQMLのrootのQML rootアイテム(topの QMLオブジェクト)で サイズ変更されます。 QQuickView.SizeViewToRootObject : 表示はQMLの rootアイテム (topのQMLオブジェクト)でサイズ変更する QQuickView.SizeRootObjectToView : 表示は、rootアイテムサイズに QMLのオブジェクトが自動的に調整する """ view.setResizeMode(QQuickView.SizeRootObjectToView) """ 画面表示するQMLコンポーネントの読み出し URL形式で画面表示するQMLファイルを設定する。 """ url = QUrl("HelloWorld.qml") view.setSource(url) if view.status() == QQuickView.Error: sys.exit(-1) # QMLコンポーネントの表示 view.show() # QApplicationのイベントループ ret = app.exec_() """ アプリケーションの終了処理 注意: アプリケーション終了前にQQuickViewのオブジェクトを delしてから終了させる。 """ del view sys.exit(ret)
def main(): # QApplication.setAttribute( Qt.AA_UseDesktopOpenGL ) # QSurfaceFormat.setDefaultFormat(utils.defaultFormat(False)) # from vtk 8.2.0 app = QGuiApplication(sys.argv) app.setApplicationName('QtVTK-Py') # qmlRegisterType(FboItem, "QmlVTK", 1, 0, "Interactor") qmlRegisterType(FboItem, "QtVTK", 1, 0, "VtkFboItem") view = QQuickView() # qml file must begin with Item, not ApplicationWindow view.setSource(QUrl.fromLocalFile(abs_path("main.qml"))) # view.setTitle ("QQuickView Load") # view.setResizeMode(QQuickView.SizeRootObjectToView) # view.setFlags(Qt.FramelessWindowHint) view.show() ''' In the unlikely event, you will encounter the following problems: the QML window is stuck. Please check if you: import pyside2, and use QmlApplicationEngine().load to load only one qml-vtk window(containing qmlRegisterType class) . If you have to do this, plus self.createRenderer () to __ init__ function, fboitem class. Then it works but an error window, vtkoutputwindow, will be displayed. Then try to close the vtkoutputwindow (maybe call the Win32 API findwindow?) Recommended solution: import PyQt5 instead of pyside2; Or using QQuickView().setSource to load qml window; Or load another independent general qml window before load the qml-vtk window. ''' # engine1 = QQmlApplicationEngine() # qml file must begin with ApplicationWindow, not Item # engine1.load(os.path.join(os.path.dirname(__file__), "main.qml")) # another independent general qml window # engine = QQmlApplicationEngine() # engine.load(os.path.join(os.path.dirname(__file__), "app.qml")) # the qml-vtk window # if not engine.rootObjects(): # sys.exit(-1) sys.exit(app.exec_())
def viewload(): app = QGuiApplication(sys.argv) view = QQuickView() view_model = URControlViewModel() ur_singal = view_model.gui view.rootContext().setContextProperty("urSingal", ur_singal) # qmlRegisterType(UIsignal, 'PySignal', 1, 0, 'PySignal') # implicit error when using slot and singal qmlRegisterType(ur_visual.FboItem, "QmlVTK", 1, 0, "VtkFboItem") view.setSource(QUrl.fromLocalFile(abs_path("qmlRobotGui\main.qml"))) view.setResizeMode(QQuickView.SizeRootObjectToView) view.setTitle("UR Controller") if view.status() == QQuickView.Error: sys.exit(-1) view.show() root_obj = view.rootObject() view_model.init_connect(root_obj) root_obj.destroyed.connect(view_model.disconnectRobot) 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
from PySide2.QtCore import QUrl from PySide2.QtQuick import QQuickView from PySide2.QtWidgets import QApplication app = QApplication([]) view = QQuickView() url = QUrl("view.qml") view.setSource(url) view.show() app.exec_()
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_())
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)