:::: MENU ::::

Using OpenCV 3 on Qt Creator 3.2 (Qt 5.3)

Posted in November 2, 2014

Qt is a “cross-plataform application and UI framework” that is being used for 800,000+ users. In this post we’re going to learn how to use OpenCV 3 on Qt 5.3 projects, so that you can create your applications with great UI features easier. If you remain with any doubt after reading this post, watch the video.

-- Ubuntu 14.04
-- OpenCV 3 installed: How to install OpenCV 3 on Ubuntu

This post has been translated to ‘farsi’ (persian) language by Ali (thanks!).

Getting ready

Before we start, I need to say I’m assuming you already have OpenCV 3 installed on your Ubuntu. If this isn’t your case, check the tutorial in the pre-requirements note. Even if you have, pay attention to this fact: OpenCV must have been compiled without WITH_QT=ON, because this flag enables OpenCV with Qt 4, not Qt 5.x, as we want here.

I know you’d like to see screenshots along this post, right? You won’t. Instead, I prefer to make videos so you can see the whole process. I hope you enjoy.

Downloading and installing Qt 5.3 and Qt Creator 3.2

Qt gives us a lot of options to download its framework and we’re going to use what’s recommended: Qt Online Installer for Linux.

  1. Visit Qt Website and go to Download;
  2. Choose Community, if you have “projects compliant with GPL/LGPL”;
  3. Click Download Now or choose what’s best for you;
    • Recommended download (in my case): Qt Online Installer for Linux;
  4. Run the installer;
  5. Done! We, now, have Qt 5.3 and Qt Creator 3.2 installed.

Creating a Project on Qt Creator 3.2

  1. File » ‘New File or Project’ (or Ctrl+N);
  2. Choose ‘Applications’ under Projects and select ‘Qt Widgets Application’ and press ‘Choose…’;
  3. Give a name to your project, browse to where you want it inside and click ‘Next’;
  4. Expand the options clicking on Desktop Details and uncheck Debug (personal choice). Click on ‘Next’;
  5. You can leave the rest as default, so go ‘Next’ » ‘Next’ » ‘Finish’ and we’re done!

Project Folder

Linking OpenCV to our projects -- Qt Creator 3.2 (Qt 5.3)

First of all, go to your “.pro” file and add the following lines before “SOURCES”:

INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui

Remember to add all the libraries you’re going to need in your project. I’ve added only these three (core, imgcodecs and highgui), because we’re going to build a very basic sample. After that, you’re ready to use OpenCV in your project.

Building and running a sample

The program we’re going to use is very simple: you’ll need to hard-code the filepath to an image so that cv::imread() can read it and the image will be displayed by cv::imshow(). This way we can proof that OpenCV is running on Qt Creator.

Add the following lines after “ui->setupUi(this)” in the file “Sources/mainwindow.cpp”:
Make sure your PATH_TO_IMAGE is valid. A lot of users were experiencing errors due to invalid paths. If your program runs and nothing happens, your image path is wrong.

cv::Mat inputImage = cv::imread(“{PATH_TO_IMAGE}”);
if(!inputImage.empty()) cv::imshow("Display Image", inputImage);

Don’t forget to add #include <opencv2/opencv.hpp> on the header.

Build the project (Build » Build Project or Ctrl+B) and run it (Build » Run or Ctrl+R).

Any doubts? Watch the video below:

The image used on the video was created by Razaras.

If you still have doubts or you’re facing any problem, send me an email: me[at]rodrigoberriel.com. Don’t forget to leave your comment.

  • wolfenstein fox

    I get “the program has unexpectedly finished”

    • Are you sure you did everything?

      • wolfenstein fox

        yes, there are no errors at all in the code the only difference is that I’m using opencv2, there’s a segmentation fault I don’t know whats causing it. thanks for the reply

      • wolfenstein fox

        I’ll try to do it on an older version of qt and I’ll let you know how it goes

      • I don’t recommend you to use OpenCV 2 nowadays. Go for OpenCV 3 as well.

  • Alvar

    Hello @Rodrigo I followed the steps and it compiles, but when I try to run I got always this error 🙁

    (OpenCvDemo:11557): Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
    The program has unexpectedly finished.

    This was on Ubuntu 16.04

    • As the error says: you’re mixing two versions of GTK in the build, which is not allowed. This requires investigation of your setup, and I’d recommend you to take a look at http://stackoverflow.com/a/42534279.

      • Alvar

        Thanks for your response, it seems that indeed there is something weird with my configuration. I ended using a QtImage to avoid calling gtk components with “imshow” function. Nice tutorials by the way.

      • Thanks!

  • Thanks for the great Tutorial, it helped getting me started with my first project in Qt Creator

  • Kear K

    Hi I’m very new to Ubuntu and opencv, I would like to thanks you for your tutorial

    it’s very easy to understand and I keep following your tutorials

    d(^ ^)b

  • Manohar S L

    Hi am new to opencv not able to run sample code in Qt its crashing exactly in this line –> if(!inputImage.empty()) cv::imshow(“Display Image”, inputImage); can some one please help me as soon as possible.

    Starting /home/manu/Desktop/Projects/build-Image-Desktop_Qt_5_7_0_GCC_64bit-Release/Image…
    The program has unexpectedly finished.
    /home/manu/Desktop/Projects/build-Image-Desktop_Qt_5_7_0_GCC_64bit-Release/Image crashed.

    • Michal

      I have the same issue, please let me know if u solve it somehow

      • Joel Ravazzolo

        I get the same issue too…not sure what is causing the problem. I have tried adding waitKey(0) to the code. I have even commented out imshow() and left waitKey(0) and it still crashes.
        So I do not think is has anything to do with the path in imshow().

  • Kashif Sagheer

    I am new on QT. When I run your program I received the following error “QT creator needs a set up to build. Configure a compiler in the kit option”. I checked on internet but did not work. Any help will be appreciated.

  • Raden Muaz

    I cannot directly copypaste and compile your code snippet. double quotes are different characters.

  • oz

    Hello Rodrigo,

    I need Opencv and Qt but another program I have needs qt4.8. Your installation installs Qt5. Do you know how should I deal with it?

    Many Thanks!!

    • Hi oz, sorry, but I don’t. Are you sure it does not have compatibility with Qt5?

      • oz

        Hi Rodrigo,

        I just messed up the entire computer configuration when I wanted to fix this compatibility issue so I don’t know really. Anyway thank you very much for fast response!!

  • Saurabh Varshneya

    Hello Rodrigo,

    I am getting below error while building the above code on Qt, and through eclipse all is running fine. Could you please throw some light on possible solution to this problem.

    Applications/Qt/5.7/gcc_64/lib -lQt5Widgets -L/usr/lib64 -lQt5Gui -lQt5Core -lGL -lpthread
    /usr/bin/ld: cannot find -lGL
    Makefile:218: recipe for target ‘DisplayImage’ failed

  • tzxu

    I get an error:
    warning: libopencv_core.so.3.0, needed by /usr/local/lib/libopencv_imgcodecs.so, may conflict with libopencv_core.so.2.4.
    error: mainwindow.o: undefined reference to symbol ‘_ZN2cv6String10deallocateEv’
    what’s wrong. How to solve this?

    • If you have set LIBS properly and this error is still happening, I’d recommend to completely uninstall OpenCV 2.4, then proceed to OpenCV3+ installation again. This is a conflict of versions problem, as you can see.

  • Jerry

    I follow this tutorial, but the `INCLUDEPATH += /usr/local/include/opencv` is not suitable for me,
    It get following error: opencv2/opencv.hpp file not found,
    then, I changed the `INCLUDEPATH += /usr/local/include`, It is fine.

  • Toby

    Hey Rodrigo, i get following error:

    pci id for fd 17: 80ee:beef, driver (null)

    libGL error: core dri or dri2 extension not found

    libGL error: failed to load driver: vboxvideo

    feels like im missing something in my libGL. You have an idea?

    • Are you using VirtualBox? Watch this: https://www.youtube.com/watch?v=S9HJ-iAzf3k

      • Toby

        Hey Rodrigo! Thanks for the fast response!
        yeah im using VirtualBox… the fix from the video didn’t help. 🙁

      • Well, sure this is related to VirtualBox and video drivers. Unfortunately, this is beyond my knowledge.

      • Toby

        np, thanks for the help. Solved it by using vmware now!
        Awesome tutorial btw!

      • Great! Enjoy OpenCV. 🙂

  • Pedro

    Hey Rodrigo,

    I’m having a problem. When I “Run” my code it only appears a cmd prompt window (I’m using windows 10) waiting a key to be pressed to close de prompt. I think I’m missing sob library, could please help me?

    Here’s my pro:

    QT += core

    QT -= gui

    CONFIG += c++11

    TARGET = agoravai

    CONFIG += console

    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp

    INCLUDEPATH += C://opencv//release//install//include

    LIBS += C://opencv//release//bin//*.dll

    Thank you.

    • If there is no compiling error, the problem might be in your source-code. Are you sure the image was loaded? In case it didn’t, the provided program is expected to do nothing.

      • Pedro

        I checked it with an ‘if’, it seems that this part is ok. Maybe it has to do with the use of dll’s, I think I’m including a lot of dll’s at the same time and this is generating a conflict, do you know how I find wich dll’s I have to use? It gives me an error code: exited with code -1073741515

      • It depends on the requirements of your program. For the provided samples, you’d need: opencv_core, opencv_imgcodecs and opencv_highgui.

  • bothaynah

    This tutorial could work in windows system?

    • If you change to the correct paths (include and libraries[-L]), I’d expect it to work.

  • Raghunandan B

    Starting /home/raghunandan/workspace/qt-blog/build-dispImage-Desktop-Release/dispImage…
    The program has unexpectedly finished.
    /home/raghunandan/workspace/qt-blog/build-dispImage-Desktop-Release/dispImage crashed

    This is the error I am getting after running the program in qt….

    • That’s not enough info to help 🙁
      Did you try to debug your code?

  • Mayibongwe Handy

    hi Rodrigo , i followed the video but when i build then run the program i get the error shown below ,

    gtk_disable_setlocale() must be called before gtk_init()

    • That’s awkward. Without more information, I can’t help you. Are you sure image has been loaded properly?

    • Ratapong Onjun

      1. get picture img.jpg to this path

      2. I think you should change this path
      cv::Mat inputImage = cv::imread(“{PATH_TO_IMAGE}”);
      such as
      cv::Mat inputImage = cv::imread(“/home/boomeesut/Pictures/img.jpg”);

  • 謝硯澤

    hi Rodrigo, I have done all the steps above , when running the program I have error…

    *** Error in `/home/paul/Documents/build-try2_0204-Desktop_Qt_5_3_GCC_64bit-Release/try2_0204′: realloc(): invalid pointer: 0x00007ff13b013d40 ***
    The program has unexpectedly finished.

    • Probably you compiled OpenCV with the flag WITH_QT. In this case, you should re-compile without this flag.


    Rodrigo I wish you best in your life ! you helped me really 🙂

  • Fevzi Daş

    Hi, Rodrigo, thank you for your depiction. I have a error. I can’t use imshow command. When I use this command the program crash. Then I have creshed message like that. “realloc(): invalid pointer:”. Expect imshow I can use other opencv command. Thank you for your help. Good bye.

    • Are you sure the image was properly loaded? Usually, it’s just a wrong path.

  • kartik

    error type a.cpp:8:2: error: stray ‘200’ in program
    a.cpp:8:2: error: stray ‘214’ in program

    solution-(type the stuff)

    You are using a Zero Width Non Joiner character after the >> in your code. This is a unicode character that is encoded in UTF-8 as three characters with values 0x2e, 0x80, 0x8c (or in base 8, 342, 200, 214).
    This probably happened because you copy and pasted some code from a
    document (html web page?) that uses those special characters.

  • Master Yoda

    hello, I have read your guide and was wondering where do you find

    -lopencv_core because in the usr/local/lib there is libopencv_core not lopencv_core thank you!

  • Pingback: استفاده از کتابخانه OpenCV در Qt - NutShell()

  • Juan Carlos Alvez Balbastro

    Muito Obrigado Rodrigo, eu so tive que tirar o -D WITH_QT=ON do seu script para instalar a OpenCV no Ubuntu 14.

  • FrAnKeStEiN MC

    Hi , i have done all the steps above but when compiling i have this error : cannot find -lopencv_imgcodecs

    i’m using ubuntu 14.04 , Qt Creator 3.4.1 , Qt version 4.8.6 and opencv 3.0.0

    thanks 😉

    • Are you sure imgcodecs is there? Have you compiled other program that uses opencv or just this one is with problem?

      • FrAnKeStEiN MC

        I have added these 2 lines to the .pro file
        unix: CONFIG += link_pkgconfig

        unix: PKGCONFIG += opencv
        and chhose QT 4.8.6 kit
        and that worked fine 🙂

  • Diego Hernandez Reyes

    Great tutorial!! can you make a tutorial about how to use CUDA and OpenCV with Qt creator?

    • Sorry, I must have missed that. Again sorry, but I can’t. I don’t have experience on that.

  • Pingback: Using OpenCV 3 on Qt Creator 3.2 (Qt 5.3) – 向右走()

  • Gabriel Alzamora

    Otimo, it gave some troubles when I copy and paste instructions; but typewriting them everything worked properly. Obrigado

  • Romeo

    Could you show/tell us, how to add opencv to qt project for android. Thanks

    • Sorry, I have no previous experience with opencv+qt+android.

  • cara

    Beleza!, Otimo!

  • janaka

    i did same steps in the video but i got this error. please help me.

    The inferior stopped because it received a signal from the Operating System.

    Signal name : SIGSEGV

    Signal meaning : Segmentation fault

    • Are you sure your {PATH_TO_IMAGE} points to a valid image? Thx for the feedback!

      • janaka

        Thanks for help Rodrigo Berriel

  • Pingback: Can't get OpenCV and qt working, file not found (Mac OSX) | 我爱源码网()

  • Eberson Ramos

    did the same steps you performed in the video and gave the following error.

    p, li { white-space: pre-wrap; }
    Starting /home/eberson/Workspace Qt/build-displayimage-Desktop_Qt_5_4_0_GCC_64bit-Release/displayimage…
    *** Error in `/home/eberson/Workspace Qt/build-displayimage-Desktop_Qt_5_4_0_GCC_64bit-Release/displayimage’: realloc(): invalid pointer: 0x00007feecc08a0e0 ***
    The program has unexpectedly finished.
    /home/eberson/Workspace Qt/build-displayimage-Desktop_Qt_5_4_0_GCC_64bit-Release/displayimage crashed

    • Eberson, it seems like you have the same error as Yonas Teodros commented. His error was the image path, he sent me his code. Could you please confirm that your {PATH_TO_IMAGE} is correct? I’ll change the tutorial just now to include a “if statement”, to make it clearer. I wasn’t including verification steps to make it as simple as possible.

      • Eberson Ramos

        Rodrigo, the path is apparently correct. I entered the terminal and typed the command “pwd” to display the directory and returned me the same thing I’m going through the code.
        What is an invalid pointer error? Pesso apologize for the lack of knowledge, as I am now beginning to work with C ++ and opencv.

    • Also, make sure you have read the Getting Ready section.
      You must have compiled OpenCV without the flag WITH_QT=ON.

      • Eberson Ramos

        Rodrigo, went through step 3 again and changed the code to: cmake -D CMAKE_BUILD_TYPE RELEASE = -D CMAKE_INSTALL_PREFIX = / usr / local WITH_TBB D = ON = ON -D -D BUILD_NEW_PYTHON_SUPPORT WITH_V4L = ON = OFF D D WITH_FFMPEG WITH_OPENGL = ON ..

        I retraced every step starting the third step. And still gives the same error, invalid pointer error, which will be this error?

      • Before re-running the cmake, did you delete the previous build folder? This is a good practice to avoid cached configs, since we used an out-of-source build. Try this again (recreating the build folder) and running:


      • Eberson Ramos

        Rodrigo, thanks for listening and for the tips, this last tip your work here. Ran the windows as shown in the video. Using Ubuntu 14.04 64 bit. Now go into another battle, to configure the Android with opencv using Qt Creator 5.4.
        Many Thanks !! hugs !!

      • Great. Good luck!

  • Yonas Teodros

    it’s avery interesting one and Gracias!

    when i build all things gone fine and i did the same thing like you did . but when I excuit the programm i got this

    p, li { white-space: pre-wrap; }
    tarting /home/yonas/build-new-Desktop_Qt_5_4_0_GCC_64bit-Debug/new…
    *** Error in `/home/yonas/build-new-Desktop_Qt_5_4_0_GCC_64bit-Debug/new’: realloc(): invalid pointer: 0x00007fcd4acaa0e0 ***
    ======= Backtrace: =========

    and finnaly it will go like this what is wrong with me ?

    • Could you please try to use Release instead of Debug and see if the error persists?

      • Yonas Teodros

        The same thing is now happning in the Release mode too.

      • Are you trying to build the project used on this post or another project? Because it doesn’t looks like an OpenCV error at all. Could you please send me your code? Email me.

  • nico carosio

    Muy bueno el post!! gracias!