【Ubuntu】QT程序 could not find or load the Qt platform plugin “xcb“ in “报错解决

问题描述:

见下图:
在这里插入图片描述

import matplotlib.pyplot as plt
import cv2
img=cv2.imread('img_path')###读入单张图片

b,g,r=cv2.split(img) #原图的bgr三个通道分离
img_rgb=cv2.merge([r,g,b])#rgb顺序显示的合成

plt.figure()
plt.subplot(331),plt.imshow(img),plt.title("cat_bgr")

plt.subplot(337),plt.imshow(img_rgb),plt.title("cat_rgb")
 
plt.show()

运行如上代码,博主要调用matplotlib函数绘图,在网上搜索*“This application failed to start because it could not find or load the Qt platform plugin “xcb”*,得到以下解决方案:

方法一:

原因分析:
matplotlib版本不兼容

解决思路:
先卸载原有matplotlib再重新安装

参考:

pip uninstall matplotlib
pip install matplotlib

方法二:

原因分析:

我们是要调用matplotlib函数绘图,而报错提示it could not find or load the Qt platform plugin “xcb” in " "
所以考虑Qt和matplotlib版本不兼容导致

解决思路:
卸载了QT与matplotlib并重新安装

参考:

conda uninstall qt
conda uninstall matplotlib
conda install qt
conda install matplotlib

方法三:

原因分析:
platforms文件夹内缺少相应的文件,尝试自己进行手动切换,对Qt安装目录下的查找platforms文件,是plugins文件下的一个子文件,将其中的.dll文件放到自己的文件夹下,即可运行。

此种方案博主没有尝试,因为涉及到底层依赖文件的移动,很容易因为漏掉某些文件出现问题,而且因为当时conda环境比较乱,找不到Qt的安装目录,于是放弃这种方案,不推荐尝试采用

方法四(可能是最常见的一种情况):

前提条件:
命令行输入qtcreator产生报错:

This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.

Reinstalling the application may fix this problem.
Aborted (core dumped)

如果使用qtcreator可以正常启动,则不属于这种情况,正常启动如下图:
【Ubuntu】QT程序 could not find or load the Qt platform plugin “xcb“ in “报错解决_第1张图片
解决方法:
1.输入

sudo gedit ~/.bashrc

在最后一行添加:

export QT_DEBUG_PLUGINS=1

这一步的作用是可以在qt启动过程中看到报错信息
2.运行qtcreator,在命令行得到运行中过程,如下:

Got keys from plugin meta data ("vnc")
QFactoryLoader::QFactoryLoader() looking at "/opt/Qt5.10.0/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so"
Found metadata in lib /opt/Qt5.10.0/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so, metadata=
{
    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": {
        "Keys": [
            "xcb"
        ]
    },
    "className": "QXcbIntegrationPlugin",
    "debug": false,
    "version": 330240
}


Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "/opt/Qt5.10.0/Tools/QtCreator/bin/platforms" ...
Cannot load library /opt/Qt5.10.0/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so: (/usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5: version `Qt_5_PRIVATE_API' not found (required by /opt/Qt5.10.0/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so))
QLibraryPrivate::loadPlugin failed on "/opt/Qt5.10.0/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so" : "Cannot load library /opt/Qt5.10.0/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so: (/usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5: version `Qt_5_PRIVATE_API' not found (required by /opt/Qt5.10.0/Tools/QtCreator/lib/Qt/plugins/platforms/libqxcb.so))"
This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.

Reinstalling the application may fix this problem.
Aborted (core dumped)

3.原因在于libqxcb.so,cd到/opt/Qt5.10.0/5.10.0/gcc_64/plugins/platforms,运行命令ldd libqxcb.so,查看关联内容,如下:

./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5: version `Qt_5_PRIVATE_API' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5: version `Qt_5_PRIVATE_API' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.10' not found (required by ./libqxcb.so)
./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5' not found (required by ./libqxcb.so)

可以看到问题在于:‘Qt_5_PRIVATE_API’ not found
4.需要添加环境变量,如下:

export LD_LIBRARY_PATH=/opt/Qt5.10.0/5.10.0/gcc_64/lib:$LD_LIBRARY_PATH

5.命令行输入qtcreator,成功运行

方法五:

思路一:
设置环境变量 export QT_DEBUG_PLUGINS=1

运行看到QT程序加载的过程,看到了详细的报错信息。
最后一部分是这样的:

Found metadata in lib /usr/lib64/qt5/plugins/platforms/libqxcb.so, metadata=
{
    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": {
        "Keys": [
            "xcb"
        ]
    },
    "className": "QXcbIntegrationPlugin",
    "debug": false,
    "version": 329218
}
QLibraryPrivate::loadPlugin failed on "/usr/lib64/qt5/plugins/platforms/libqxcb.so" : "Cannot load library /usr/lib64/qt5/plugins/platforms/libqxcb.so: (/lib64/libQt5XcbQpa.so.5: symbol _ZNK11QFontEngine6handleEv, version Qt_5_PRIVATE_API not defined in file libQt5Gui.so.5 with link time reference)"
This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, xcb, xcb.

Reinstalling the application may fix this problem.

把libQt5XcbQpa.so,libQt5DBus.so.5拷贝到运行目录。

思路二:
将qt下plugins目录下的platforms目录拷贝到可执行目录,和执行程序同一级目录(一定同一级)

这种方法同方法三

方法六:

解决qt.qpa.plugin: Could not find the Qt platform plugin “xcb“ in ““

方法七(也是我最后的解决方法):

思路:
重新创建一个conda环境:

conda create -n env_name python=3.7

安装matplotlib包:

pip install matplotlib

成功解决!

参考链接:
1.[SOLVED] qt.qpa.plugin: Could not find the Qt platform plugin “xcb” in “”

你可能感兴趣的:(报错解决,qt,ubuntu,经验分享)