我注意到当 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

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

有帮助吗?

解决方案

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

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

其他提示

看起来你在那里难以接受Qt限制。如果您需要跨线程进行通信的对象,请尝试使用信号或事件。

或者询问Qt民众。它似乎并不特定于PyQt。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top