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): 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(): 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 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 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 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
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))
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. del view
@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) 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() 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)
url = 'http://country.io/names.json' response = urllib.request.urlopen(url) data = json.loads(response.read()) # Format data _list = sorted(data.values()) #Setup the application window app = QApplication(sys.argv) view = QQuickView() view.setResizeMode(QQuickView.SizeRootObjectToView) url = QUrl("main.qml") # Expose a model to the Qml code my_model = QStringListModel() my_model.setStringList(_list) view.rootContext().setContextProperty('myModel', my_model) #Load the QML file view.setSource(url) # Show the window if view.status() == QQuickView.Error: sys.exit(-1) view.show() # Execute and cleanup app.exec_() del view
txt_file = os.path.join(current_path, 'default.txt') f = QtCore.QFile(txt_file) f.open(QtCore.QIODevice.ReadOnly) all = str(f.readAll(), 'utf-8') model = TreeModel(str(all)) f.close() app = QApplication(sys.argv) if True: current_path = os.path.dirname( sys.argv[0]) #os.path.abspath(os.path.dirname(__file__)) ui_path = os.path.join(current_path, '') qml_file = os.path.join(ui_path, 'main.qml') url = QUrl.fromLocalFile(qml_file) view = QQuickView() view.rootContext().setContextProperty("theModel", model) view.setSource(url) view.setResizeMode(QQuickView.SizeRootObjectToView) if view.status() == QQuickView.Error: oops = view.errors() print(oops) import sys sys.exit(-1) else: #view.rootContext().setContextProperty("theModel", model) view.show() view.rootContext().setContextProperty("theModel", model) app.exec_() else: view = QTreeView() view.setWindowTitle("Simple Tree Model")
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) view.show() res = app.exec_() del view sys.exit(res)
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_()
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. del view
if self.data(self.index(i, 0), self.OPENEDFLAG): return m = "" currentmark = self.data(self.index(i, 0), self.MARK) if currentmark == "": m = "*" self.markedCount += 1 elif currentmark == "*": m = "?" self.markedCount -= 1 elif currentmark == "?": m = "" self.setData(self.index(i, 0), m, self.MARK) if __name__ == '__main__': app = QApplication(sys.argv) view = QQuickView() minesModel = MinesModel() ctx = view.rootContext() ctx.setContextProperty("minesModel", minesModel) view.engine().quit.connect(app.quit) view.setSource(QUrl("view.qml")) view.show() sys.exit(app.exec_())
class QTUI(UIBase): def xyToIndex(self, x, y): return y * 8 + x def indexToXY(self, index): return (index % 8, index // 8) def ioThread(self, inputQueue, emitter): while True: io = inputQueue.get() if (io["type"] == UIMessageType.SETUP): player = io["data"]["player_name"] emitter.sendName(player[WHITE], player[BLACK]) elif (io["type"] == UIMessageType.BOARD): for index in range(0, 64): col, row = self.indexToXY(index) item = io["data"][row][col] if (item == BLACK): emitter.send(index, True) elif (item == WHITE): emitter.send(index, False) time.sleep(0.2) elif (io["type"] == UIMessageType.SCORE): emitter.sendScore(io["data"]["o"], io["data"]["x"]) elif (io["type"] == UIMessageType.TURN): turn = io["data"] if turn == "x": emitter.sendMark(True) else: emitter.sendMark(False) elif (io["type"] == UIMessageType.FORFEIT): print("Tidak ada langkah mungkin, skip") elif (io["type"] == UIMessageType.DOTURN): pass elif (io["type"] == UIMessageType.QUIT): break self.inputQueue.task_done() def threadWorker(self): self.app = QGuiApplication() self.view = QQuickView() self.view.setResizeMode(QQuickView.SizeRootObjectToView) #Load the QML file qml_file = os.path.join(os.path.dirname(__file__), "view.qml") self.view.setSource(QUrl.fromLocalFile(os.path.abspath(qml_file))) root = self.view.rootObject() ioHandler = Handler(self.moveQueue) context = self.view.rootContext() context.setContextProperty("handler", ioHandler) #Show the window if self.view.status() == QQuickView.Error: sys.exit(-1) ioSignaler = ioObject(root) input_thread = threading.Thread(target=self.ioThread, args=(self.inputQueue, ioSignaler)) input_thread.start() self.view.show() self.app.exec_() self.inputQueue.put({"type": UIMessageType.QUIT}) input_thread.join() self.outputQueue.put({"type": UICommandType.QUIT}) self.moveQueue.put({"x": -1, "y": -1})