Skip to content

Instantly share code, notes, and snippets.

@narodnik
Created February 1, 2019 12:31
Show Gist options
  • Save narodnik/a0f5571369ebcc1f41ea2928f7a8a6a9 to your computer and use it in GitHub Desktop.
Save narodnik/a0f5571369ebcc1f41ea2928f7a8a6a9 to your computer and use it in GitHub Desktop.
from PySide2.QtCore import QObject, Signal, Property, QUrl
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtWebEngine import QtWebEngine
from PySide2.QtWidgets import QApplication
import sys
class SourceTextProperty(QObject):
def __init__(self, text):
QObject.__init__(self)
self._source_text = text
print("init fin")
def _text(self):
return self._source_text
def _set_text(self, text):
print("set called")
self._source_text = text
self.text_changed.emit()
@Signal
def text_changed(self):
print("text changed")
text = Property(str, _text, _set_text, notify=text_changed)
def main():
app = QApplication([])
QtWebEngine.initialize()
engine = QQmlApplicationEngine()
source_text_property = SourceTextProperty(open('sample.md').read())
engine.rootContext().setContextProperty('source_text', source_text_property)
engine.load(QUrl.fromLocalFile('view.qml'))
if not engine.rootObjects():
return -1
source_text_property.text = 'hello'
return app.exec_()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment