使用PyQt5开发带有opencv-python的应用时,如果桌面服务器是Xorg,那么会遇到一个错误:
QObject::moveToThread: Current thread (0x2f01f2c0) is not the object's thread (0x35738860).
Cannot move to target thread (0x2f01f2c0)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/anysets/.conda/envs/yolo/lib/python3.10/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: xcb, eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl.
zsh: IOT instruction (core dumped) /home/anysets/.conda/envs/yolo/bin/python
如果将桌面服务器切换为Wayland则不会报这个错误,并且程序能够正常运行。
为了解决这个问题,我在国内搜索引擎以及Google上搜了很久,普遍的解决方法是将opencv-python
包换为opencv-python-headless
。但是这样做将无法再使用imshow这样的函数,因为它不依赖于任何图形界面。除此之外,没有什么有效的解决方案。
但是也有人提到过这个报错是因为qt版本不兼容,于是我将PyQt5换成PyQt6进行尝试,程序成功运行。
因此,除非特别不兼容的地方,以后所有的上位机开发技术栈都将转为PyQt6,现有PyQt5应用也将逐步迁移至PyQt6。