在动态创建的 QQuickPaintedItem 中访问引擎根上下文
Access engine root context in dynamically created QQuickPaintedItem
是否可以在没有全局引用的情况下访问动态创建的 QQuickPaintedItem 中的引擎根上下文?
class LiveStream(QQuickPaintedItem):
def __init__(self):
QQuickPaintedItem.__init__(self)
self.backend = root_ctx.contextProperty("backend") # <- I want to remove/change this global reference
# self.getRootContext() <- Is there such a function, I cant find such a function on documentaion
...
if __name__ == "__main__":
app = QApplication()
engine = QQmlApplicationEngine()
url = QUrl.fromLocalFile(MAIN_QML)
root_ctx = engine.rootContext()
main = MainWindow(root_ctx, engine)
root_ctx.setContextProperty("backend", main)
qmlRegisterType(LiveStream, "video", 1, 0, "LiveStream")
engine.load(url)
在 QML 方面我这样做
function add_stream(camera, pane, pane_id) {
Qt.createQmlObject(`
import video 1.0
LiveStream {
width: ${pane.width}
height: ${pane.height}
stream_id: '${camera.id}'
stream_name: '${camera.name}'
anchors.fill: parent
}`, pane);
}
我对 pyside 或 qt/qml 很陌生,所以任何帮助都会非常好。
谢谢
添加 Qt 属性 到 Livestream 并在 qml 端引用它。
是否可以在没有全局引用的情况下访问动态创建的 QQuickPaintedItem 中的引擎根上下文?
class LiveStream(QQuickPaintedItem):
def __init__(self):
QQuickPaintedItem.__init__(self)
self.backend = root_ctx.contextProperty("backend") # <- I want to remove/change this global reference
# self.getRootContext() <- Is there such a function, I cant find such a function on documentaion
...
if __name__ == "__main__":
app = QApplication()
engine = QQmlApplicationEngine()
url = QUrl.fromLocalFile(MAIN_QML)
root_ctx = engine.rootContext()
main = MainWindow(root_ctx, engine)
root_ctx.setContextProperty("backend", main)
qmlRegisterType(LiveStream, "video", 1, 0, "LiveStream")
engine.load(url)
在 QML 方面我这样做
function add_stream(camera, pane, pane_id) {
Qt.createQmlObject(`
import video 1.0
LiveStream {
width: ${pane.width}
height: ${pane.height}
stream_id: '${camera.id}'
stream_name: '${camera.name}'
anchors.fill: parent
}`, pane);
}
我对 pyside 或 qt/qml 很陌生,所以任何帮助都会非常好。 谢谢
添加 Qt 属性 到 Livestream 并在 qml 端引用它。