qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr

Linux环境用pycharm搞OpenCV人脸识别遇到的问题:

最终解决办法:

在pycharm程序中加入以下语句

from PyQt5.QtCore import QLibraryInfo
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(
    QLibraryInfo.PluginsPath
)

然后将opencv-python和opencv-contrib-python换成4.2或4.1版本即可

(后面是尝试过的不大成功的方法)

第一步:设置环境变量(w11:设置里直接搜索高级系统设置就可以找到)

qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第1张图片qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第2张图片

qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第3张图片

 qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第4张图片

然后点击确定,再运行程序,就可显示出更详细的错误:

QObject::moveToThread: Current thread (0x1e19270) is not the object's thread (0x207bdb0).
Cannot move to target thread (0x1e19270)

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/root/PycharmProjects/pythonProjectenvb/python3.8/site-packages/cv2/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.

 

第二步:去虚拟机相应目录下查看libqxcb.so这个文件是否有问题,参考

xxxUbuntu18.04下解决Qt出现qt.qpa.plugin:Could not load the Qt platform plugin “xcb“问题 - 盼小辉丶 - 博客园

按顺序依次执行以下命令:
cd /root/PycharmProjects/pythonProject/venv/lib/python3.8/site-packages/cv2/qt/plugins
ls 
cd platforms
ls
ldd libqxcb.so


qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第5张图片

 效果如上图

第三步:我的没有问题,就去pycharm里加入以下代码:

envpath = '/root/PycharmProjects/pythonProject/venv/lib/python3.8/site-packages/cv2/qt/plugins/platforms'
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = envpath

 qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第6张图片

找不到路径的也可以直接添加以下代码试试:

envpath = '/data/xx/xx/venv/lib/python3.6/site-packages/cv2/qt/plugins/platforms'
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = envpath

 把对应的python版本号改一下就行

最后:出现了QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'这个错误

完美解决XDG_RUNTIME_DIR not set, defaulting to ‘/tmp/runtime-root‘_源代码杀手的博客-CSDN博客_xdg_runtime_dir

 去尝试了这个方法,发现它还是存在,但突然发现这个界面可以用

qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第7张图片

然后点击右边窗口时,又出现了AttributeError: module 'cv2' has no attribute 'face_LBPHFaceRecognizer_create'

qt.qpa.plugin: Could not load the Qt platform plugin xcb in /root/PycharmProjects/pythonPr_第8张图片

 正待解决。。。

(刚开始那个错误看很多设环境变量解决的,自己也去设了,但发现在虚拟机下好像没什么用,然后杂七杂八的错误,好几天才搞到这,好像pycharm直接添加第二种路径就可以用图形界面了,自己以为那不行,又退回去搞了好久。。。)

参考:Ubuntu18.04下用pycharm運行程序,Qt出现qt.qpa.plugin:Could not load the Qt platform plugin “xcb“问题_bandieyanzhi的博客-CSDN博客

你可能感兴趣的:(qt,开发语言)