You can only do GUI operations in the main thread. I would emit signals from your worker thread that are connected to operations in your main threads that do the actual gui manipulation.
-Brent

Ole Streicher wrote:
Hi,

When I try to update a table from another thread, my program crashes or
gives really unexpected results.

Here is some example code:
---------------------------------8<--------------------------------------
import threading
import sys
import time

from PyQt4 import QtGui, QtCore

app = QtGui.QApplication(sys.argv)

main = QtGui.QMainWindow()
table = QtGui.QTableWidget(2, 2, main)
main.setCentralWidget(table)

class MyThread(threading.Thread):
    def run(self):
        time.sleep(3)
        table.insertRow(2)
        table.setCellWidget(2, 0, QtGui.QLabel('Hallo Welt'))
MyThread().start()

main.show()
app.exec_()
---------------------------------8<--------------------------------------

The sleep() function just simulates some calculation.
When I start the program, it always shows warnings like
QObject::setParent: Cannot set parent, new parent is in a different thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QObject::installEventFilter(): Cannot filter events for objects in a different 
thread.

QObject::installEventFilter(): Cannot filter events for objects in a different 
thread.
QObject::installEventFilter(): Cannot filter events for objects in a different 
thread.
QCoreApplication::sendPostedEvents: Cannot send posted events for objects in 
another thread
QCoreApplication::sendPostedEvents: Cannot send posted events for objects in 
another thread

Sometimes it shows the QLabel in an extra window which re-openes when I
try to close it. Sometimes this program crashes with the additional
printout

python: ../../src/xcb_io.c:176: process_responses: Assertion `!(req && current_request 
&& !(((long) (req->sequence) - (long) (current_request)) <= 0))' failed.
Aborted

or
X Error: BadGC (invalid GC parameter) 13
  Major opcode: 56 (X_ChangeGC)
  Resource id:  0x2803500
table.py: Fatal IO error: client killed

What is wrong with my code and how can I add a table row and fill it
with a QWidget from another thread? I already tried to set the parent of
the QLabel to table or to main.

Qt version 4.5.0, PyQt4 version 4.4.4 (kubuntu 9.04)
Qt version 4.4.3, PyQt4 version 4.4.3 (SuSE 11.1)

Best regards

Ole

_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to