opencv GUI does not work
cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE) results in an error:
QObject::moveToThread: Current thread (0xca9110) is not the object's thread (0x1256140).
Cannot move to target thread (0xca9110)
here is the version:
cv2.__version__
Out[3]: '3.3.0'
QtCore.QT_VERSION_STR
Out[7]: '5.6.2'
I didn't have this issue at first, then cv2.namedWindow just stopped working for me without me making any system changes. I noticed in dmesg the following: python[29883]: segfault at 0 ip 00007fa8c3c114be sp 00007ffcf5c34740 error 4 in libxcb.so.1.1.0[7fa8c3c07000+21000]
After a quick Google, I found this thread, which recommended removing the libqt5x11extras5 package. I just removed that (and the 121 dependent packages), and it's working properly again. So like @skvark mentioned, it's probably due to Qt conflicts.
you saved my day!
Anyway, without knowing if linking statically is even possible and solves the problem, legal debates are over nothing.
FYI, it is possible to link to Qt statically, and we would have to either
- provide
opencv-pythonand OpenCV source code (and require anyone else who conveys it to do the same) -- i.e. relicenseopencv-pythonas strong copyleft; or - provide object files for the closed-source parts that a user would be able to link (statically) with a modified (static) Qt, as well as instructions on how to do this, in some form.
For the 2nd option,
- I believe the temporary
cmake-builddirectory tree, without the resulting Python module, would qualify. In essence, we need the makefiles to pick up the object files without recompiling them and perform linking for whatever links to Qt and all the way up to the extension module as usual. Not sure if CMake-generated makefiles can do that, or they would complain and require OpenCV sources as well to check if the object files are up to date. - An archive of this tree for each release with static Qt would need to be available to
opencv-pythonusers somehow -- e.g. at Github releases page (for at least 3 years since release date). LICENSE-3RD-PARTY.txtwould need to refer to that location in its Qt section.- Anyone who conveys
opencv-pythonwould have the same obligation and would either have to rely on us, or (e.g. if they make modifications), provide such an archive themselves and updateLICENSE-3RD-PARTY.txtaccordingly.
I.e. this is not any easier at all than providing the source code in the first place.
So, I conclude that it would be much easier to update OpenCV to support namespaced Qt -- especially since supporting it is pretty straightforward -- and link against such a Qt.
- In Linux, it's as trivial as adding a configuration switch to dockerfiles since we compile Qt ourselves
- In OSX, will have to make a custom Qt formula and compile it from source just like for FFMpeg.
I don't have segfaults or crashes; the window just shows up with no content (black bar?) when trying to imshow with the following code:
import cv2
img = cv2.imread('/tmp/foo.png')
cv2.imshow('output', img)
cv2.waitKey(0)
with the aforementioned moveToThread messages:
QObject::moveToThread: Current thread (0x55bb9bd3a9f0) is not the object's thread (0x55bb9bf9a6e0).
Cannot move to target thread (0x55bb9bd3a9f0)

/tmp/foo.png is a colour image of size 512x512.
I am on a Manjaro system with python 3.8.3, opencv-python 4.3.0.36, KDE and Qt5 (although this should not matter as opencv-python ships with own version of Qt). Looking through the LD_DEBUG output, I can see the system Qt libs being loaded as a result of an indirect dependency from the opencv-python shipped Qt libs!
1262759: file=/usr/lib/qt4/plugins/imageformats/libqgif.so [0]; dynamically loaded by /home/freakabcd/.virtualenvs/opencv/lib/python3.8/site-packages/cv2/../opencv_python.libs/libQtCore-6b7bca35.so.4.8.7 [0]
1262759: file=/usr/lib/qt4/plugins/imageformats/libqgif.so [0]; generating link map
1262759: dynamic: 0x00007f38b9c68bf8 base: 0x00007f38b9c5f000 size: 0x000000000000a030
1262759: entry: 0x00007f38b9c62020 phdr: 0x00007f38b9c5f040 phnum: 9
1262759:
1262759:
1262759: file=libQtGui.so.4 [0]; needed by /usr/lib/qt4/plugins/imageformats/libqgif.so [0]
1262759: find library=libQtGui.so.4 [0]; searching
1262759: search cache=/etc/ld.so.cache
1262759: trying file=/usr/lib/libQtGui.so.4
1262759:
1262759: file=libQtGui.so.4 [0]; generating link map
1262759: dynamic: 0x00007f38b9ba3540 base: 0x00007f38b909b000 size: 0x0000000000b202c0
1262759: entry: 0x00007f38b926c020 phdr: 0x00007f38b909b040 phnum: 9
1262759:
1262759:
1262759: file=libQtCore.so.4 [0]; needed by /usr/lib/qt4/plugins/imageformats/libqgif.so [0]
1262759: find library=libQtCore.so.4 [0]; searching
1262759: search cache=/etc/ld.so.cache
1262759: trying file=/usr/lib/libQtCore.so.4
1262759:
1262759: file=libQtCore.so.4 [0]; generating link map
1262759: dynamic: 0x00007f38b9094920 base: 0x00007f38b8d98000 size: 0x0000000000302410
1262759: entry: 0x00007f38b8e30f60 phdr: 0x00007f38b8d98040 phnum: 12
As you can see, the opencv-python provided libQtCore is dynamically loading this libqgif.so because it is present in some known path to it /usr/lib/qt4/plugins/imageformats. Given that @areche mentions /usr/lib/kde4/plugins/styles/breeze.so being loaded on their system, it could be that the 4.8.7 Qt shipped with opencv-python is not built correctly to ignore loading plugins from well known "system" paths. I am not even sure if such an option exists in the Qt build configuration as it has been many years since I have built Qt from source.
It would be great if some of the people on this thread with the same issue can check if they have the /usr/lib/qt4/plugins or /usr/lib/kde4/plugins paths showing up in their debug logs (LD_DEBUG=all python myfile.py). If other people can confirm one (or both) of these paths is appearing in their output, someone can check if there is a Qt build config to get rid of searching system paths for plugins to load dynamically.
@freakabcd See https://github.com/skvark/opencv-python/issues/362
The latest issue with Qt4 is related to the manylinux2014 upgrade. I had to use qt-devel directly from CentOS 7 repos because there were some compiler related issues when I tried to compile Qt4 manually. In older manylinux1 releases Qt4 was compiled manually. Please check the Dockerfiles in the docker folder in this repo for more info. I'll try to upgrade to Qt5 before next release which hopefully solves some of these issues. Plugins path can be set as an env var in Qt5 (like it is set currently in macOS builds in __init__.py), but not in Qt4 based on some fast searches I did earlier.
Starting from 3.4.10.37 and 4.3.0.38 the pre-built wheels will include Qt 5.15 which has been built in a namespace so the conflict issue should be fixed. The new releases should be up in the next 24 hours. However, please note that I cannot guarantee that the wheels will work on all GNU/Linux systems. The new releases will include also source distributions which should allow to build the wheels locally if you encounter issues with the OpenCV GUI functions.
I would just like to add that I get this same error on 4.3.0.38 and forward but not on 4.3.0.36. I'm running Elementary OS 5.1.7 Hera (Ubuntu based).
I tried updating because cv2.imshow wasn't showing all of my images properly (some were just showing a small box instead of the image) and after updating they show as they should but I'm getting the same QObject::moveToThread error.
@joelcma Yeah, that "small box" bug was this one which was also caused by a Qt conflict: https://github.com/skvark/opencv-python/issues/362
Can't help much with that QObject::moveToThread error. Just make sure that your Python environment is consistent and you have removed all old versions of opencv-python from there. I have done everything I can to fix the issue.
It seem that I soved QObject::moveToThread: Current thread is not the objects thread. Cannot move to target thread, byconda uninstall pyqt. But I don't make sure its feasibility. And I found the QT about libs in /usr/lib/x86_64-linux-gnu whill be removed after the commond and every thing is fine now.
My problem was caused by QT. before I install QT, my opencv was fine. I uninstalled QT but it did not help. I finally was able to make it work through "sudo apt-get autoremove libqt5x11extras5".
It didn't work for me, I have to submit a project but nothing seems to be working. I have Ubuntu 20.04 LTS, working in Spyder but unable to take videocapture. Please help.
Next
opencv-pythonrelease will include Qt5 instead of Qt4. That change will most likely solve this issue.
What do we do if we are still having this issue? (version 4.6)
For those having this issue in Anaconda installation, just pip uninstall your opencv-python , and install opencv using conda :
conda install -c conda-forge opencv
that should fix your problems.