0%

Ubuntu conda环境PyQt报错 QObject::moveToThread: Current thread (0x223e600) is not the object's thread

1 问题描述

在Ubuntu conda环境中运行python代码时会报错:

1
2
3
QObject::moveToThread: Current thread (0x223e600) is not the object's thread
...
Could not load the Qt platform plugin "xcb" in "/home/..." even though it was found

2 问题解决

2.1 网上相关文章

在网上搜索该问题(Could not load the Qt platform plugin "xcb" in "/home/..." even though it was found)时发现很多人遇到了同样的问题,很多文章提到可以打开PyQt的debug模式export QT_DEBUG_PLUGINS=1,然后查看报错内容。部分情况是由于缺少动态链接库导致的,解决方案可参考这篇文章,但是我并不属于这个报错类型。后来又搜索QObject::moveToThread: Current thread (0x223e600) is not the object's thread时找到了适用于本人情况的解决方案,此处参考该文章,并在此记录一下解决方案。

2.2 解决方案

文章指出出现上述报错是由于使用不同方式安装PyQt和OpenCV,两软件版本不兼容导致的。文中提到的解决方案是:

  1. 卸载当前conda安装的PyQt:

    1
    conda uninstall pyqt
  2. 通过pip安装PyQt:

    1
    2
    pip install PyQt5
    pip install opencv-python

然而,通过上述操作后仍会出现同样的报错,问题并没有得到解决……

不过,在查看评论区时意外发现了真正解决该问题的方法:

1
pip install opencv-python-headless

2.3 OpenCV版本

opencv-python和opencv-python-headless的区别可参考该文章。主要区别在于opencv-python是完整版,包含图像处理、视觉算法、GUI操作等;而opencv-python-headless不包含任何GUI相关组件,其余功能与完整版本一致。PyQt是适用于python语言的GUI工具包,猜想该解决方法有效的原因是这种功能性互补的操作可以避免出现版本不兼容的问题。