Thread error in Python & PyQt
I noticed that when the setModel function is executed on a parallel thread (I tried threading.Timer or threading.thread) I get this:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QHeaderView(0x1c93ed0), parent thread is QThread(0xb179c0), current thread is QThread(0x23dce38)
QObject::startTimer: timers cannot be started from another thread
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTreeView(0xc65060), parent thread is QThread(0xb179c0), current thread is QThread(0x23dce38)
QObject::startTimer: timers cannot be started from another thread
Is there a way to solve this?
source to share
It is indeed a fact of life that multithreading using Qt (and other rich frameworks) is a delicate and arduous task requiring explicit attention and care - see the Qt docs for excellent coverage (for readers with experience with threads in general, with suggested readings for those who are not yet).
If possible, I would suggest what I always suggest as the most robust architecture for streaming in Python: let each subsystem be owned and used by one dedicated thread; communicate between threads through instances Queue.Queue
, that is, by passing messages. This approach can be a little limited, but it provides a good foundation on which to specifically define and carefully thought out exceptions (based on thread pools, random new threads, spawned, locks, condition variables, and other such useful things ;-). In the latter category, I would also classify Qt-specific things like interconnecting with a signal / communication slot via queues in a queue .
source to share