OpenCV integration with Qt6
-
Hello I've wasted several hours searching web to find solution for my problem and couldn't find anything useful
I've been following this tutorial: https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows and I don't understand what is not working
every single time I tried no matter if I've used Qt6/Qt5, CMake 3.7.2/CMake3.23.1, OpenCV3.4.15/Open4.5.5, Nothing seems to work at all. every single time I get Stuck on this part.
help -
Hi and welcome to devnet,
From what you show in your screenshot, cmake was not run.
cmake is required to setup the build instructions then make to execute these instructions.
-
D:\opencv-build
!=D:\opencv-4.x.build
-
@Christian-Ehrlicher Act like they are the same, I tried to install OpenCV like 20 times after the original post and this CMake Image is just more recent.
-
You Selected the wrong generator (Visual Studio) and not the Makefile generator and therefore a VS solution file was created. Remove the build dir, make sure no vsvars*.bat is not called so cmake does not pick up MSVC and call cmake with '-G MinGW Makefiles'
-
@Christian-Ehrlicher I'm new to Cmake what do you mean by
"make sure no vsvars*.bat is not called"I think what you mean is that I just have to change my generator to MinGW Makefile, but then:
In the console above ^
'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
Configuring incomplete, errors occurred! -
The https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows page is outdated and doesn't apply to Qt6 anymore.
That page needs to be rewritten to deal with CMake, Qt6 and having a OpenCV CMake example.
I think the easiest way of dealing with 3rd party dependencies would be to use a source package manager like conan, vcpkg or cpp-pm.
-
I just tested Qt 6.2.3, OpenCV 4.5.5 with MinGW 11.2.0, and everything worked as expected 🙂
I used CMake 3.21.3 (but any recent version should be fine) and Ninja 1.10.2.
Compiling OpenCV
I've extracted the OpenCV source package into
c:\Projects\opencv\opencv-4.5.5
.Then I've opened a command prompt window and set the environment:
set PATH=c:\mingw64\bin;c:\tools\ninja;c:\tools\cmake\bin;%PATH%
Your paths might vary so update accordingly.
Then configured OpenCV:
cd c:\Projects\opencv cmake -G Ninja -D CMAKE_BUILD_TYPE=Release -S opencv-4.5.5 -B build-opencv -D WITH_QT=ON -D CMAKE_PREFIX_PATH=C:/Qt/6.2.3/mingw_64
Then built OpenCV:
cmake --build build-opencv
And installed OpenCV
cmake --install build-opencv --prefix opencv-install
Using OpenCV
I've created
test-qt
CMake Qt Widgets example.In
CMakeLists.txt
I have added:find_package(OpenCV REQUIRED)
This command finds the OpenCV CMake package and sets some CMake variables which I used for the
test-qt
executable target:target_include_directories(test-qt PRIVATE ${OpenCV_INCLUDE_DIRS}) target_link_libraries(test-qt PRIVATE ${OpenCV_LIBS})
And in the CMake project settings I have changed the
CMAKE_PREFIX_PATH
in Initial Configuration to:%{Qt:QT_INSTALL_PREFIX};c:\Projects\opencv\opencv-install
Then in
mainwindow.cpp
I've added the OpenCV headers:#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp>
and in the constructor the OpenCV usage:
// read an image cv::Mat image = cv::imread("c:/Windows/Web/Wallpaper/Windows/img0.jpg", 1); // create image window named "Wallpaper" cv::namedWindow("Wallpaper"); // show the image on window cv::imshow("Wallpaper", image);
Then compiled and run my application in Qt Creator 7.0.1. 🎉
-
@cristian-adam
It really works and it wasn't that hard, your instructions were simple and clear.
I Highly appreciate your effort, and I hope you wonderful day/night :). -
@cristian-adam said in OpenCV integration with Qt6:
Using OpenCV
I've created test-qt CMake Qt Widgets example.
In CMakeLists.txt I have added:
find_package(OpenCV REQUIRED)This command finds the OpenCV CMake package and sets some CMake variables which I used for the test-qt executable target:
target_include_directories(test-qt PRIVATE ${OpenCV_INCLUDE_DIRS})
target_link_libraries(test-qt PRIVATE ${OpenCV_LIBS})And in the CMake project settings I have changed the CMAKE_PREFIX_PATH in Initial Configuration to:
%{Qt:QT_INSTALL_PREFIX};c:\Projects\opencv\opencv-installHey brother, I don't quite understand the specific operation here. Could you please guide me? Thank you
-
@Morgan7794 hi please see this example: https://github.com/FelgoSDK/Qt-Felgo-OpenCV maybe it helps you.
-
@cristian-adam After spending 6 hours trying to set up OpenCV with Qt6 while following several tutorials (only to find out that it all does not apply to Qt6), your solution worked perfectly fine.
I should only add that I struggled with Qt Creator GUI looking for cmake project settings to change CMAKE_PREFIX_PATH. but finally got it (CTRL+5) ,
Just bookmarked this thread for good.many thanks for this!