PySide and Qt Properties: Connecting signals from Python to QML -
i trying run simple pyside appliccation design made in qml. call function in qml python in separated thread.
i have following qml view:
import qt 4.7 rectangle { visible: true width: 100; height: 100 color: "lightgrey" text { id: datestr objectname: "datestr" x: 10 y: 10 text: "init text" font.pixelsize: 12 function updatedata(text) { datestr.text = qstr(text) console.log("you said: " + text) } } }
here application:
some imports....
#!/usr/bin/python import sys import time pyside import qtdeclarative, qtcore pyside.qtgui import qapplication pyside.qtdeclarative import qdeclarativeview
the qthread worker....
#worker thread class worker(qtcore.qthread): updateprogress = qtcore.signal(int) def __init__(self, child): qtcore.qthread.__init__(self) self.child = child def run(self): while true: time.sleep(5) #call function updatedata - not work self.child.updatedata("other string ")
and here view assembled:
if __name__ == '__main__': # create qt application app = qapplication(sys.argv) # create view view = qdeclarativeview() # connect qml view.setsource(qtcore.qurl.fromlocalfile('main_ui.qml')) # show ui view.show() #get root object , find child datestr root = view.rootobject() child = root.findchild(qtcore.qobject, "datestr") #call function updatedata - works child.updatedata("string") worker = worker(child) worker.start() sys.exit(app.exec_())
if qml function updatedata(text)
called qthread, datestr.text
not set console message produced. if qml function called within main function, update of datestr.text
works normally.
question
how proper way call qml function qtthread?
links
i found answer in other thread:
stackoverflow: pyside settext() not updating qlabel
in case widget not repainted , therefore needed force application repaint it. used code (see pyside docs: qapplication):
def updateallwidgets(): widget in qapplication.allwidgets(): widget.update()
this function can called example timer:
timer = qtcore.qtimer() timer.start(1000) timer.timeout.connect(updateallwidgets)
Comments
Post a Comment