Using Non-Qt Libraries

It's impossible not to include any extra library when programming bigger applications. Here I show you how to use non-Qt libraries in Qt.

Here I'll use OpenCV as an example.

You can get an OpenCV package from here if you want: http://opencv.org/

*.pro file

Qt's *.pro file is where Qt records the path of the project related files. This includes headers, c++ files, extra library include paths, and more.

Below is an auto-generated *.pro file:

#-------------------------------------------------
#
# Project created by QtCreator 2015-03-14T14:40:52
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = OpenCVTest
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

To add the OpenCV library path info, there are two keywords to remember: INCLUDEPATH and LIBS. Qt will search the paths added after these two words.

You can also create our own variables just like in C and C++. But be sure that you didn't use any keyword as your variable.

OpenCV_Libd = D:/libraries/opencv300b_o/x86/vc12/lib\
OpenCV_Lib = D:/libraries/opencv300b_o/x86/vc12/lib\

INCLUDEPATH += D:/libraries/opencv300b_o/include
LIBS += $$OpenCV_Lib/opencv_world300.lib\
        $$OpenCV_Lib/opencv_ts300.lib\

The example code above adds the OpenCV to the project. Note that it is safer to use common slash instead of backslash in any path. Qt recognizes the backslash as the end of a path.

You can also use $$quote() to force Qt to read the whole line written in the brackets. Qt will then ignore the backslash if there is one in the middle of a path.

OpenCV_Libd = $$quote(D:\libraries/opencv300b_o/x86/vc12/lib)

But I'm not recommended to use backslash in paths because only Windows uses backslash in file paths.

If you want to use debug and release library in the same project, you can add a Release and Debug flag before the keyword LIBS.

OpenCV_Libd = D:/libraries/opencv300b_o/x86/vc12/lib
OpenCV_Lib = D:/libraries/opencv300b_o/x86/vc12/lib

INCLUDEPATH += D:/libraries/opencv300b_o/include

Release: LIBS += $$OpenCV_Lib/opencv_world300.lib\
                 $$OpenCV_Lib/opencv_ts300.lib\
Debug:   LIBS += $$OpenCV_Lib/opencv_world300d.lib\
                 $$OpenCV_Lib/opencv_ts300d.lib\

Run QMake

At last, make sure you perform the QMake action ( Build -> Run qmake ). Most people forget to run QMake and fail to use any library in the project.

The best solution is to run QMake each time you change the Qt profile setting. :)

results matching ""

    No results matching ""