Python和PyQt中的线程错误

Kir*_*tov 2 python multithreading pyqt

我注意到当函数setModel在并行线程中执行时(我尝试了threading.Timer或threading.thread),我得到了这个:

QObject: Cannot create children for a parent that is in a different thread.
(Parent is QHeaderView(0x1c93ed0), parent's 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's thread is QThread(0xb179c0), current thread is QThread(0x23dce38)
QObject::startTimer: timers cannot be started from another thread
Run Code Online (Sandbox Code Playgroud)

有什么方法可以解决这个问题吗?

Ale*_*lli 5

多线程使用Qt(和其他丰富的框架)是一项微妙而艰巨的工作,需要明确的关注和关注,这确实是生活中的事实 - 请参阅Qt的文档以获得对该主题的出色报道(对于一般的线程化读者而言,对那些尚未提供的建议读数.

如果可能的话,我会建议我总是建议用Python作为最健全的线程架构:让每个子系统由一个专用线程拥有和使用; 通过实例Queue.Queue,即通过消息传递在线程之间进行通信.这种方法可能有点限制,但它提供了一个良好的基础,可以明确地识别和精心设计的异常(基于线程池,偶尔生成的新线程,锁,条件变量和其他类似的挑剔事物;-).在后一类中,我还将通过排队连接对特定于Qt的事物进行分类,例如跨线程信号/插槽通信.