Hello Sebastian,
thank you very much for your fast and useful answer, everything works well
now with signals, and the tutorial you've linked above help me understand
what I've done.
I'll post right here the fixed version of my code in case someone has the
same problem.
class Test(QtCore.QObject):
my_signal = QtCore.pyqtSignal()
def __init__(self):
QtCore.QObject.__init__(self)
# ... stuffs
app = QtGui.QApplication([])
## Create window with GraphicsView widget
win = pg.GraphicsLayoutWidget()
# ... window settings
## Create image item
self.vis = pg.ImageItem()
# ... image settings
## Create obstacle histogram
plot = pg.PlotItem()
# ... plot settings
self.text = pg.TextItem('Loading...')
self.view.addItem(self.text)
self.my_signal.connect(lambda: self.text.setText(self.textGen()))
# ... other stuffs
QtGui.QApplication.instance().exec_()
# ... methods and stuffs
def callback(self, ...):
# ...
self.my_signal.emit()
def textGen(self):
my_string = "..."
return my_string
Thank you again.
Toni.
Il giorno giovedì 20 luglio 2017 15:15:11 UTC+2, Sebastian Höfer ha scritto:
>
> Hello Toni,
>
> judging from the error message the problem ist not in the code you postet,
> but in the way you use the callback. Like the error message states: you
> shouldn't call methods of QObjects from other threads. Even if you don't
> use threads explicitly that happens easily if you use another
> framework/module that makes use of threads internally. So your callback is
> probably invoked in another thread.
>
> You can work around that by using signals.
> Define your own signal... for example
> my_signal = QtCore.pyqtSignal()
>
>
> and in your callback function you trigger the signal
> my_signal.emit()
>
>
> In your GUI code you connect your new signal with the update function
> my_signal.connect(lambda: self.text.setText('Hello world!'))
>
>
> If the concept is new to you, here's a tutorial:
>
> http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/
>
> <http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/>
>
>
> Regards
> Sebastian
>
>
> Am Donnerstag, 20. Juli 2017 14:49:18 UTC+2 schrieb Toni:
>>
>> Hi everyone,
>>
>> I'm trying to update the text inside a TextItem object from a callback,
>> so that I can stream float values that come from outside my python script.
>> The problem I'm facing is that once my callback is invoked from the
>> framework I'm using, the following error is thrown:
>>
>> QObject: Cannot create children for a parent that is in a different
>>> thread.
>>>
>>
>> I don't know how to avoid that and I'm not sure if there is actually a
>> way to do what I want,
>>
>> This is the code I have written until now:
>>
>> QtGui.QApplication([])
>>
>> ## Create window with GraphicsView widget
>>
>> win = pg.GraphicsLayoutWidget()
>> win.show() ## show widget alone in its own window
>> win.setWindowTitle('...')
>>
>> # ... window settings
>>
>> ## Create image item
>> self.vis = pg.ImageItem()
>>
>> # ... image settings
>>
>> ## Create obstacle histogram
>> plot = pg.PlotItem()
>>
>> # ... plot settings
>>
>> self.text = pg.TextItem('Loading...')
>> self.view.addItem(self.text)
>>
>>
>> And from the callback mentioned above (where the problem is) I do:
>>
>> self.text.setText('Hello world!')
>>
>>
>> Does anyone know how to avoid this problem and implement this behavior?
>>
>> Thank you in advance for your answers, I hope the question is clear.
>>
>>
>>
>>
>>
>>
>
--
You received this message because you are subscribed to the Google Groups
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pyqtgraph/e93abed7-21e5-4b13-bb2e-a3b2b74266a7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.