Problem in Setting up QT to Integrate with OpenCV and C++ in Ubuntu 18.04 using CMake

Greetings ! It’s my first post in this forum.
I’ve been following an OpenCV tutorial from a book. Prior to it, I have OpenCV already installed on my machine. I’m working with OpenCV & C++ on terminal Ubuntu and compile my program with CMake. Here’s my Ubuntu OpenCV version :

Ubuntu 18.04.4 LTS
OpenCV version : 4.2.0

I have been getting trouble for days building OpenCV and integrating it with Qt using CMake. You might find my question elsewhere, because I’ve asked this question in OpenCV forum before, even in Stackoverflow, and CodeProject, all with no luck so far. (Can’t link them since I have limitation of how many links I can put here). CMake forum is my last hope, I’d really appreciate some input/advice.

From the book tutorial I followed, the result in the book is different than mine, apparently they’re using QT. And later on the next few chapters, they still use Qt. So, I went on to install Qt using these following codes :

sudo apt-get install qtcreator
sudo apt-get install qt5-default

When I checked my Qt version, this is what I got :

$ qmake --version                                                               
QMake version 3.1
Using Qt version 5.9.5 in /usr/lib/x86_64-linux-gnu

So, now I have Qt installed in my machine, but how do I integrate this with my OpenCV so I can use it on my programs other than putting highgui lib ? I encountered this error :

The library is compiled without QT support in function

I’ve been looking around online but still don’t know how to integrate it properly.

Apparently there was no other way beside completely uninstall OpenCV and then compile & reinstalling it with QT on. This is what I do :

  1. Uninstall OpenCV entirely, and reinstalled it again (did it 3x times already) by cloning OpenCV and OpenCV contrib from Github, and during building OpenCV with CMake in terminal, I’ve added WITH_QT = ON . here’s the full set up that I used :

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D WITH_V4L=ON \ 
    -D WITH_QT=ON \ 
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \

No issue and I got OpenCV installed correctly (tested with several programs). And here’s the version :

$ pkg-config --modversion opencv 

I also came across this question : OpenCV integration With Qt, but still no solution there. But why does I still get error on any project with QT on it (I have QT_RADIOBOX and other on createButton ) even after I entirely reinstall, build and make OpenCV with WITH_QT=ON ? The library is compiled without QT support, did CMake failed to locate QT ?

terminate called after throwing an instance of 'cv::Exception'
    what():  OpenCV(4.2.0-dev) /home/raisa/opencv_build/opencv/modules/highgui/src/window.cpp:597: 
error: (-213:The function/feature is not implemented) 
The library is compiled without QT support in function 'createButton'

    [2]    10467 abort (core dumped)  ./exerc13

Here’s createButton function :

//Create buttons
createButton("Blur",  blurCallback,  NULL, QT_CHECKBOX, 0);
createButton("Grey",  greyCallback,  NULL, QT_RADIOBOX, 0);
createButton("RGB",   bgrCallback,   NULL, QT_RADIOBOX, 1);
createButton("Sobel", sobelCallback, NULL, QT_PUSH_BUTTON, 0);
  1. I read about explicit path definition for Qt integration instead of just putting " WITH_QT=ON ". So I decided to go with cmake-gui and defined each path for Qt path configuration. Like this :

    WITH_QT=ON (checked in cmake-gui)
    Qt5Concurrent_DIR = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Concurrent
    Qt5Core_DIR       = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Core
    Qt5Gui_DIR        = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Gui
    Qt5OpenGL_DIR     = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5OpenGL
    Qt5Test_DIR       = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Test
    Qt5Widgets_DIR    = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5Widgets
    Qt5_DIR           = /home/raisa/Qt5.9.9/5.9.9/gcc_64/lib/cmake/Qt5

(Qt installation folowing installation docs from Qt Wiki)

Again, OpenCV was build & installed perfectly with CMake. Tested by running simple programs. But still encounter the same error for Qt. Any advice ? Thank you.

From a very quick scan of the info scattered around the web, it sounds like OpenCV will pick up Qt automatically when building. But for it to do that, you’l need to install the right set of development packages. (Which contain the headers, CMake configuration files, etc. required for building against those components.)

A good starting set would be (I’m guessing at these package names, as I’m a Fedora user):

sudo apt install qtbase5-dev qtmultimedia5-dev qt3d5-dev \
  qtsvg5-dev qttools5-dev qtwebview5-dev

And probably something to do with QtQuick and QtQML, too. Anyway, search for -dev packages that start with qt.

Or if there’s just a qt5-dev, that would probably be all of them.

Also, those seem wrong to me. Those aren’t the locations of the Qt components, they’re the locations of the CMake configuration files that configure the Qt components — but they’ll be thrown off with those paths (if they’re even run at all). The right variables would be something like

Qt5Concurrent_DIR = /home/raisa/Qt5.9.9/5.9.9/


Qt5Core_DIR       = /home/raisa/Qt5.9.9/5.9.9/gcc_64/

(Most likely the same path for all of the components, since they’re all ultimately installed in the same dir.)

But you shouldn’t need to do that, if CMake is in charge, and if you’re using a system Qt (installed by sudo apt). CMake will pick up the pieces for those automatically, they’re in default locations. (As long as you have -dev packages installed.)

If things aren’t working, it’s best to post as much of your actual CMakeLists.txt contents as could be relevant, from tiny snippets it’s hard to say what could be an issue. Posting actual output of the cmake generation run also helps.

Hi, thanks !
I searched for “qt5-dev” related for Ubuntu and I came across this : List of all Qt5 developement packages, available on Ubuntu. So I checked all my qt lib installation against it, at this point I’m sure I have everything installed.

For Cmake path I mentioned, I tried with pre-built path from CMake and it didn’t work. So I manually defined paths for QT following this tutorial, it seemed to work for some people. And actually it’s not just QT, installation with OpenGL also dosn’t work. Now I will try to install it again using pre-built path from Cmake. I’ll update it if it worked or not.

I mean, I honestly don’t know that your OpenCV setup isn’t right. I would expect that if you want to use Qt APIs from within your OpenCV code, you’d need to build with both Qt and OpenCV — I wouldn’t expect that OpenCV would somehow “incorporate” Qt.

(Quite the opposite — if anything I can see OpenCV building/installing some Qt plugins, perhaps, since Qt has an extremely extensible design. And in fact, some docs about the “marriage” of the two mention integrating OpenCV into QtCreator, which makes sense.)

Given that you have QtCreator installed, I’d suggest trying to get your code building in there, sice it basically guarantees that you’ll at least have the Qt dependencies available. (Working in OpenCV, that should be a lesser challenge.)

There also may be example projects ready-made that you could try to build, to verify the install. (Certainly, there are tons of example projects available in Creator, just to confirm that Qt itself is sane and functional.)