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

**URL:** https://discourse.cmake.org/t/problem-in-setting-up-qt-to-integrate-with-opencv-and-c-in-ubuntu-18-04-using-cmake/786
**Category:** Usage
**Tags:** os:linux, comp:gcc
**Created:** [March 10, 2020, 9:08pm UTC](https://discourse.cmake.org/t/problem-in-setting-up-qt-to-integrate-with-opencv-and-c-in-ubuntu-18-04-using-cmake/786 "2020-03-10T21:08:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![raisa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/raisa/32/422_2.png) [@raisa](https://discourse.cmake.org/u/raisa)
#### Post date: [March 10, 2020, 9:08pm UTC](https://discourse.cmake.org/t/problem-in-setting-up-qt-to-integrate-with-opencv-and-c-in-ubuntu-18-04-using-cmake/786/1 "2020-03-10T21:08:39Z")

</div>

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.

**WHAT I’VE TRIED**  
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 :

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

```
$ pkg-config --modversion opencv 
4.2.0

```

I also came across this question : [OpenCV integration With Qt](https://stackoverflow.com/questions/37289184/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 :

(Qt installation folowing installation docs from [Qt Wiki](https://wiki.qt.io/Install_Qt_5_on_Ubuntu))

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.

---

<div class="post-metadata">

### Author: ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)
#### Post date: [March 17, 2020, 9:43pm UTC](https://discourse.cmake.org/t/problem-in-setting-up-qt-to-integrate-with-opencv-and-c-in-ubuntu-18-04-using-cmake/786/2 "2020-03-17T21:43:38Z")

</div>

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):

```auto
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.

---

<div class="post-metadata">

### Author: ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)
#### Post date: [March 17, 2020, 9:51pm UTC](https://discourse.cmake.org/t/problem-in-setting-up-qt-to-integrate-with-opencv-and-c-in-ubuntu-18-04-using-cmake/786/3 "2020-03-17T21:51:42Z")

</div>

> [@raisa](#):
>
> 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 :
> 
> ```auto
> 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
> 
> ```

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_…

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

```

or…

```auto
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.

---

<div class="post-metadata">

### Author: ![raisa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/raisa/32/422_2.png) [@raisa](https://discourse.cmake.org/u/raisa)
#### Post date: [March 18, 2020, 12:26am UTC](https://discourse.cmake.org/t/problem-in-setting-up-qt-to-integrate-with-opencv-and-c-in-ubuntu-18-04-using-cmake/786/4 "2020-03-18T00:26:19Z")

</div>

Hi, thanks !  
I searched for “qt5-dev” related for Ubuntu and I came across this : [List of all Qt5 developement packages, available on Ubuntu](https://askubuntu.com/questions/508503/whats-the-development-package-for-qt5-in-14-04). 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](https://www.youtube.com/watch?v=qA46fvP3O5A), 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.

---

<div class="post-metadata">

### Author: ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)
#### Post date: [March 18, 2020, 1:29am UTC](https://discourse.cmake.org/t/problem-in-setting-up-qt-to-integrate-with-opencv-and-c-in-ubuntu-18-04-using-cmake/786/5 "2020-03-18T01:29:06Z")

</div>

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.)
