PDA

View Full Version : [PyQt4] Crash when writing to QTextEdit in a thread



robtheg
13th May 2009, 03:50
Running PyQT4 on Kubuntu 7.x

I'm getting segfaults when running operations in a spawned thread. I create a dialog with a QTextEdit in it and pass a reference to the dialog into the thread. The thread then does some work while writing information to the QTextEdit. The segfaults don't seem to happen if I don't create the window, or if I create the window and don't write to it.

I've tried this with both python threads and QThread with the same results. Funny thing is, the "DoUtilityOperation" function usually completes all its tasks before the crash happens.


Update: I actually do get problems before the tasks are completed. One of these two errors appears and hangs the dialog:
QTreeView::rowsInserted internal representation of the model has been corrupted, resetting.
*** glibc detected *** python: realloc(): invalid next size: 0x0000000000fe1430 ***

Am I simply going about this the wrong way? Is this way inherently unstable? Is there a smarter way to do this?

Thanks for any help!

A simplified version of my code follows:


#MyOperation button clicked
@pyqtSignature("")
def on_button_MyOperation_clicked(self):
#pass to the operation handler
self.utilityButtonClicked('MyOperation')


def utilityButtonClicked(self, operation):
#get selected items
selectedItems = self.treeWidget_Files.selectedItems()
if len(selectedItems) > 0:
#progressViewer is a dialog that contains a QTextEdit widget
window = progressViewer.progressViewer(self)
window.setVisible(True)
window.show()
utilityThread = None
#create the thread to do the utility operation
utilityThread = threading.Thread( None, self.DoUtilityOperation, None, ( operation, selectedItems, window ) )
utilityThread.start()
self.threads.append( utilityThread ) #store the thread



def DoUtilityOperation( self, operation, selectedItems, window ):
for item in selectedItems: #run the operation on each item
self.DoUtilityOperationOnItem( operation, item, window )



def DoUtilityOperationOnItem( self, operation, item, window ):
#do some stuff

#output text using this code several times
window.textEdit_ProgressDisplay.append( 'some text' )
window.textEdit_ProgressDisplay.moveCursor( QTextCursor.End )

#do some other stuff