Realtime TextItem update

199 views
Skip to first unread message

Toni

unread,
Jul 20, 2017, 8:49:18 AM7/20/17
to pyqtgraph
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.
 

       

Sebastian Höfer

unread,
Jul 20, 2017, 9:15:11 AM7/20/17
to pyqtgraph
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!'))



Regards
Sebastian

Toni

unread,
Jul 20, 2017, 9:57:42 AM7/20/17
to pyqtgraph
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.
Reply all
Reply to author
Forward
0 new messages