QT applications error out due to conflict with cv2 (Could not load the Qt platform plugin "xcb")

We would like to address an existing bug that will likely occur using Python scripts that import the cv2 library and have a component that uses the linux apt installed QT platform to run a GUI. The error output would look like the following:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/hello-robot/.local/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.

The above error is due to a conflict that occurs where importing the cv2 library replaces the path to QT platform plugin binary file libqxcb.so that points to the version that exists inside the cv2 installation path instead of the linux system installed QT platform plugin path. This causes the above error while trying to run an any application that relies on this E.g. (Matplotlib, Rviz launch.py, etc). This issue could be fixed by forcing the QT platform plugin path back to the linux installed version by setting the environment variable QT_QPA_PLATFORM_PLUGIN_PATH to the one existing in the linux root install directory.

You can locate the libqxcb.so

$ locate libqxcb.so
/home/hello-robot/.local/lib/python3.10/site-packages/cv2/qt/plugins/platforms/libqxcb.so
/usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so

And in the python file after importing cv2 you can switch the QT to use the binary in the linux root instal in runtime.

import os
import cv2
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/usr/lib/x86_64-linux-gnu/qt5/plugins/platforms/libqxcb.so'

Also, note that after switching to the Linux-installed version of the libqzxcb,so binary, the cv2.imshow() method will no longer work because it requires the binary that comes with the cv2 install.

I hope this info is helpful.

3 Likes