本文介绍了Qt 和 opencv 应用程序无法在虚拟环境中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 pyqt5 和 opencv 创建了一个 GUI 应用程序.该应用程序在不激活虚拟环境的情况下运行良好,但是当我激活虚拟环境并运行该应用程序时,它显示此错误:

I created a GUI app using pyqt5 and opencv. The app works fine without activating the virtual env but when I activate the virtual env and run the app it shows this error:

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)在虚拟环境中都运行良好.

I tried running an example pyqt5 code (without importing opencv) and another code (only using opencv) both worked fine in the virtual env.

操作系统:Parrot OS 4.11

Operating System: Parrot OS 4.11

Python 版本:3.9.2

Python Version: 3.9.2

推荐答案

问题是opencv编译的Qt版本与PyQt5使用的版本不一样导致冲突.

The problem is that the version of Qt with which opencv was compiled is not similar to the one used by PyQt5 causing a conflict.

一个可能的解决方案是指明使用 PyQt5 使用的 Qt 插件.

A possible solution is to indicate to use the Qt plugins used by PyQt5.

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:

For 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 获取插件文件夹路径:

Update:

A better option is to use QLibraryInfo to get the plugins folder path:

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
)

这篇关于Qt 和 opencv 应用程序无法在虚拟环境中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:40
查看更多