2

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

有没有办法解决这个问题?

4

2 回答 2

5

确实,Qt(和其他丰富的框架)的多线程使用是一项微妙而艰巨的工作,需要明确的关注和关心——请参阅Qt 的文档以获得对该主题的出色报道(对于一般具有线程经验的读者) ,为那些还没有的人建议阅读)。

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

于 2009-09-17T01:52:29.193 回答
0

看起来您在那里遇到了 Qt 限制。如果您需要对象跨线程通信,请尝试使用信号或事件。

或者向 Qt 人员询问这个问题。它似乎并不特定于 PyQt。

于 2009-09-16T18:58:17.033 回答