Could not load the QT platform "xcb" even though it was found

System information (version)
  • OpenCV => 4.5.1.48
  • Operating System / Platform => Ubuntu 20.10
  • Compiler => Visual Studio 2020
Detailed description

I’m trying to run a face recognition algorithm in python3.8.5(in anaconda4.9 under a env base).
Once cv2.imshow() is encountered, I’m getting the following error.

qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/bas/anaconda3/lib/python3.8/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.

Aborted (core dumped)

Also I tried executing the code with QT_DEBUG_PLUGINS=1 python3 my_script.py still provides the same error along with debug lines.

QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xz"
"Failed to extract plugin meta data from '/home/bas/anaconda3/bin/xz'" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzcat"
"Failed to extract plugin meta data from '/home/bas/anaconda3/bin/xz'" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzcmp"
QElfParser: '/home/bas/anaconda3/bin/xzdiff' is not an ELF object
"'/home/bas/anaconda3/bin/xzdiff' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzdec"
"Failed to extract plugin meta data from '/home/bas/anaconda3/bin/xzdec'" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzdiff"
QElfParser: '/home/bas/anaconda3/bin/xzdiff' is not an ELF object
"'/home/bas/anaconda3/bin/xzdiff' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzegrep"
QElfParser: '/home/bas/anaconda3/bin/xzgrep' is not an ELF object
"'/home/bas/anaconda3/bin/xzgrep' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzfgrep"
QElfParser: '/home/bas/anaconda3/bin/xzgrep' is not an ELF object
"'/home/bas/anaconda3/bin/xzgrep' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzgrep"
QElfParser: '/home/bas/anaconda3/bin/xzgrep' is not an ELF object
"'/home/bas/anaconda3/bin/xzgrep' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzless"
QElfParser: '/home/bas/anaconda3/bin/xzless' is not an ELF object
"'/home/bas/anaconda3/bin/xzless' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/xzmore"
QElfParser: '/home/bas/anaconda3/bin/xzmore' is not an ELF object
"'/home/bas/anaconda3/bin/xzmore' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/zstd"
"Failed to extract plugin meta data from '/home/bas/anaconda3/bin/zstd'" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/zstdcat"
"Failed to extract plugin meta data from '/home/bas/anaconda3/bin/zstd'" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/zstdgrep"
QElfParser: '/home/bas/anaconda3/bin/zstdgrep' is not an ELF object
"'/home/bas/anaconda3/bin/zstdgrep' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/zstdless"
QElfParser: '/home/bas/anaconda3/bin/zstdless' is not an ELF object (file too small)
"'/home/bas/anaconda3/bin/zstdless' is not an ELF object (file too small)" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/bin/zstdmt"
"Failed to extract plugin meta data from '/home/bas/anaconda3/bin/zstd'" 
         not a plugin
QFactoryLoader::QFactoryLoader() checking directory path "/home/bas/anaconda3/lib/python3.8/site-packages/cv2/qt/plugins/platforms" ...
QFactoryLoader::QFactoryLoader() looking at "/home/bas/anaconda3/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"
Found metadata in lib /home/bas/anaconda3/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so, metadata=
{
    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": {
        "Keys": [
            "xcb"
        ]
    },
    "archreq": 0,
    "className": "QXcbIntegrationPlugin",
    "debug": false,
    "version": 331520
}


Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "/home/bas/anaconda3/bin/platforms" ...
loaded library "/home/bas/anaconda3/lib/python3.8/site-packages/cv2/qt/plugins/platforms/libqxcb.so"
qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/bas/anaconda3/lib/python3.8/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.

Aborted (core dumped)

welcome.

you should contact the maintainer of the conda package. these are errors related to Qt. the package maintainer is responsible for making that work.

I happened to find an answer. https://stackoverflow.com/questions/60042568/this-application-failed-to-start-because-no-qt-platform-plugin-could-be-initiali

uninstall opencv-python. Then install opencv-python-headless. Worked for me.

But that will remove some functions like imshow()