Configure Qt and OpenCV On MacOSx

    Introduction

    This article shows how to use the QtCreator to build applications using the OpenCV library on MacOS.

    Installing Qt

    For installing Qt on mac, you first need to install xCode. Find it in the AppStore and install it. Then download the Qt installer from https://download.qt.io/archive/qt/. Execute it and voilĂ  it is installed.

    Installing OpenCV

    First you need to install brew, type in a terminal:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    brew update
    echo "# Homebrew" >> ~/.bash_profile
    echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile
    source ~/.bash_profile
    

    Then install OpenCV: brew install opencv

    Configuring Qt

    For Mac, the configuration is a little bit different, you need first to add some paths to the Qt creator builder

    Open the Qt creator
    Open your project
    In the side left panel, click on the "Projects" button
    In the second left side panel click on the "Build" button
    Go to the "Build Environment" and click on "Details".
    Open a terminal and type: 
    	`which pkg-config`
    If the command return nothing tape: 
    	`brew install pkg-config`
    else it will return a path.
    Copy this path minus the "pkg_config"
    Add :pathCopied in the value og the PATH variable
    In the terminal type
    	`find /usr/local -name opencv.pc`
    and copy the path minus the opencv.pc part
    In the Qt creator, add a new variable **PKG_CONFIG_PATH** and past in value the path previously copied
    

    Finally we create the .pro file:

                    #-------------------------------------------------
                    #
                    # Project created by QtCreator 2018-01-26T21:49:39
                    #
                    #-------------------------------------------------
                    QT       += core gui
                    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                    TARGET = MyApp
                    TEMPLATE = app
                    QT_CONFIG -= no-pkg-config
                    CONFIG  += link_pkgconfig
                    PKGCONFIG += opencv
                    # The following define makes your compiler emit warnings if you use
                    # any feature of Qt which has been marked as deprecated (the exact warnings
                    # depend on your compiler). Please consult the documentation of the
                    # deprecated API in order to know how to port your code away from it.
                    DEFINES += QT_DEPRECATED_WARNINGS
                    # You can also make your code fail to compile if you use deprecated APIs.
                    # In order to do so, uncomment the following line.
                    # You can also select to disable deprecated APIs only up to a certain version of Qt.
                    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                    SOURCES += \
                            main.cpp \
                            mainwindow.cpp \
                            someSourceCode.cpp
                    LIBS += -L/usr/local/lib/
                    HEADERS += \
                            mainwindow.h\
                            someSourceCode.h
                    FORMS += \
                            mainwindow.ui
                    RESOURCES += resources.qrc
    

    Your OpenCV/Qt application should compile and run without problems.