python - Qt 和 opencv 应用程序无法在虚拟环境中运行
问题描述
我使用 pyqt5 和 opencv 创建了一个 GUI 应用程序。该应用程序可以在不激活虚拟环境的情况下正常工作,但是当我激活虚拟环境并运行该应用程序时,它会显示此错误:
QObject::moveToThread: Current thread (0x125b2f0) is not the object's thread (0x189e780).
Cannot move to target thread (0x125b2f0)
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/deepak/Desktop/SampleApp/lib/python3.9/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.
Aborted
我尝试运行一个示例 pyqt5 代码(不导入 opencv)和另一个代码(仅使用 opencv)都在虚拟环境中运行良好。
操作系统:Parrot OS 4.11
Python版本:3.9.2
解决方案
问题是编译 opencv 的 Qt 版本与 PyQt5 使用的版本不相似,导致冲突。
一个可能的解决方案是指示使用 PyQt5 使用的 Qt 插件。
import os
from pathlib import Path
import PyQt5
from PyQt5.QtWidgets import QWidget # others imports
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath(
Path(PyQt5.__file__).resolve().parent / "Qt5" / "plugins"
)
# ...
对于 PySide2:
import os
from pathlib import Path
import PySide2
from PySide2.QtWidgets import QWidget # others imports
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath(
Path(PySide2.__file__).resolve().parent / "Qt" / "plugins"
)
# ...
更新:
更好的选择是使用 QLibraryInfo 来获取插件文件夹路径:
import os
from PyQt5.QtCore import QLibraryInfo
# from PySide2.QtCore import QLibraryInfo
import cv2
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(
QLibraryInfo.PluginsPath
)
推荐阅读
- python - 如何遍历整个 html 表并转换为 json 数据?
- r - 带有R的图像功能在里面打勾
- html - 如何创建响应式网格(电影网站)
- android - 在 Android 和 iOS 中通过 BCrypt 进行密码散列
- authentication - Id 令牌与访问令牌
- excel - 用一个值填充一行中的多个单元格
- java - WebDelegatingSmartContextLoader 无法检测到默认值
- c++ - 在 Windows 上设置编译器路径
- python - 如何将 Hovertool 用于多行?
- ios - 滑动过快或连续左右滑动非常快时如何跟踪pageViewController的索引