Reputation: 300
I've got python 2.5 and PyQt 4.8.6 installed. Os - Windows Xp Sp2. I use the following code to fill TreeWidget:
def updateTreeWidget(self, widget, results):
""" Updates the widget with given results """
widget.clear()
for item in results:
temp = QtGui.QTreeWidgetItem()
j = 0
for elem in item:
temp.setText(j , str(elem))
j += 1
widget.addTopLevelItem(temp)
for column in range(widget.columnCount()):
widget.resizeColumnToContents(column)
It causes crashes at the second use of it. If I comment out one of the following lines:
widget.addTopLevelItem(temp)
or
widget.clear()
it works without problems.
I call the function in the thread every 60s. Here is the definition of MyThread class.
class MyThread(threading.Thread):
def __init__(self, db, widget, function, script, parameter):
threading.Thread.__init__(self)
self.db = db
self.function = function
self.script = script
self.parameter = parameter
self.widget = widget
self.event = threading.Event()
def run(self):
while 1:
self.event.wait(60)
parameter = [getCurrentTimeStr()] + self.parameter
res = self.db.getQuery(self.script % tuple(parameter))
self.function(self.widget, res)
This thread is started when in main window init():
class MainWnd(QtGui.QMainWindow):
def __init__(self, parent = None):
# some code
self.db = DbAccess()
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_mainWnd()
self.ui.setupUi(self)
self.thread = MyThread(self.db, self.ui.treeWidget, self.updateTreeWidget, self.script, self.param)
self.thread.start()
The widget was created using Qt Designer.
Upvotes: 6
Views: 5423
Reputation: 17535
Updating GUI elements from a thread that is not the same as Qt's event loop is a big no-no.
The customary way to solve this is to used a queued signal/slot connection or a singleshot QTimer to pass thread boundaries and execute your function on the main thread.
Upvotes: 3