How to setup Qt and openCV on Windows: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Added instructions for the current versions of Qt and OpenCV, procedure is not 100% correctly written down, has to be tested/corrected on a fresh windows machine)
 
(9 intermediate revisions by 8 users not shown)
Line 1: Line 1:
How to setup Qt and openCV on Windows
How to setup Qt and openCV on Windows


{{Cleanup | reason=Incomplete and not functional.}}
[[Category:HowTo]]
[[Category:HowTo]]


== Introduction ==
== Introduction ==
WARNING: The below procedures are incomplete, and not functional.
This article shows how to install Qt, build OpenCV, and run a basic OpenCV example. This article assumes Windows 10 has just been installed.
This article shows how to install both Qt and openCV, and build a basic OpenCV example. This article assumes Windows 7 has just been installed.
 
This procedure requires close to 10GB of disk space:
Qt: 5.06GB
opencv: 522MB
opencv-Build: 3.95GB
downloads:152MB 


This article uses information from the following pages:
This article uses information from the following pages:
Line 12: Line 16:
http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml
http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml


== Windows, source of openCV, old version ==
== Windows 10, Qt 5.9, OpenCV 3.2.0 ==
This section is a direct copy of [http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml this] guide. Only minor changes are done to make it work with a newer version of Qt.
This guide is actual for Qt 5.12.2 with MinGW 7.3.0 and OpenCV 4.0.1 too
 
=== Qt ===
=== Qt ===
==== Installation ====
==== Installation ====
Download the Qt installer from [https://www.qt.io/download-open-source/ www.qt.io], then choose "Download now". This will then download qt-unified-windows-x86-2.0.5-online.exe. Execute the program, then choose the following settings:
Download the Qt installer from [https://www.qt.io/download-open-source/ www.qt.io], then choose "Download now". This will then download qt-unified-windows-x86-2.0.5-online.exe. Execute the program, then choose the following settings:
Welcome to the Qt online installer: next
  Welcome to the Qt online installer: next
Qt Account - your unified login to everything Qt: skip
  Qt Account - your unified login to everything Qt: skip
Setup-Qt: next
  Setup-Qt: next
installation folder: D:\Qt
  installation folder: D:\Qt
select components: Qt-Qt5.8-MingGW 5.3.0 32 bit
  select components: Qt-Qt5.9-MingGW 5.3.0 32 bit
select components: Qt-Tools-MinGW 5.3.0
  select components: Qt-Tools-MinGW 5.3.0
License Agreement: agree and next
  License Agreement: agree and next
start menu shortcuts: next
  start menu shortcuts: next
ready to install: install
  ready to install: install
==== Testing ====
==== Testing ====
Run D:\Qt\Tools\QtCreator\bin\qtcreator.exe
Run D:\Qt\Tools\QtCreator\bin\qtcreator.exe
Line 35: Line 40:
Now a new project is made. Start debugging by choosing
Now a new project is made. Start debugging by choosing
   Debug-Start Debugging-Start debugging (F5)
   Debug-Start Debugging-Start debugging (F5)
Now the Qt tab in the windows task bar should turn into a progress bar. After some time a new empty window should pop up. Stop debugging either by pressing the red cross in the top right of this new window, or choose
Now the Qt tab in the Windows taskbar should turn into a progress bar. After some time a new empty window should pop up. Stop debugging either by pressing the red cross in the top right of this new window, or choose
   Debug-Stop debugging   
   Debug-Stop debugging   
==== Adjust Qt ====
==== Adjust Qt ====
When you need to add, remove or update a component of Qt, this can be done by running D:\Qt\MaintenanceTool.exe:
When you need to add, remove or update a component of Qt, this can be done by running D:\Qt\MaintenanceTool.exe:
maintain Qt: Qt Account: Skip
  maintain Qt: Qt Account: Skip
Setup Qt: Add or remove components: Next
  Setup Qt: Add or remove components: Next
Select components:  
  Select components:  
next :update  
  next :update  
 
=== Tortoisegit ===


=== Cmake ===
=== Cmake ===
Line 53: Line 56:
   Destination folder: C:\Program Files\CMake (default), next
   Destination folder: C:\Program Files\CMake (default), next
   Ready to install CMake, Install
   Ready to install CMake, Install
 
=== OpenCV ===
=== OpenCV ===
==== Getting OpenCV ====
==== Getting OpenCV ====
Download openCV from [https://sourceforge.net/projects/opencvlibrary/ sourceforge]. In this guide, version [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3.1/ 2.3.1] is used. Start OpenCV-2.3.1-win-superpack.exe and let it extract to d:\ Now the folder d:\opencv is created.
Download openCV from [https://sourceforge.net/projects/opencvlibrary/ sourceforge]. In this guide, version [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/ 3.2.0] is used. Start opencv-3.2.0-vc14.exe and let it extract to d:\ . Now the folder d:\opencv is created.
==== Compiling OpenCV ====
Start C:\Program Files\CMake\bin\cmake-gui.exe then choose the following settings:
  Where is the source code: D:/openv
  where to build the binaries: D:\opencv-build
Then click Configure and choose the following settings:
  Specify the generator for this project: MinGW Makefiles
  Specify native compilers, next
  Compilers C: D:/Qt/Tools/mingw530_32/bin/gcc.exe
  Compilers C++: D:/Qt/Tools/mingw530_32/bin/g++.exe
  Finish
Now the error "Error in configuration process, project files may be invalid" comes up
  Click the checkbox [Advanced], then click the red line
  CMAKE_MAKE_PROGRAM and browse to D:/Qt/Tools/mingw530_32/bin/mingw32-make.exe
Then click Configure. Now the error "the system can't start because libwinpthread-1.dll is missing from your computer. Try reinstalling the program to fix this problem."


==== Add minGW to the windows PATH variable====
Open the control panel,
System and Security,
System,
Advanced system settings,
Environment Variables,
System Variables,
Variable Name: Path
Variable value: ;D:\Qt\Tools\mingw530_32\bin


The C compiler "D:/Qt/Tools/mingw530_32/bin/gcc.exe" is not able to compile a simple test program. It fails with the following output:
CMakeFiles\cmTC_9c5ad.dir\build.make:64: recipe for target
  'CMakeFiles/cmTC_9c5ad.dir/testCCompiler.c.obj' failed
 
Add to the system path solves above problems.
  Change CMAKE_BUILD_TYPE from Debug to Release
  Check the box [X]WITH_QT
  Check the box [X]WITH_QT_OPENGL
Then click configure again
Then click generate 
Next open cmd, and type the following commands:
  cd d:\
  cd opencv-build
  ming32-make -j 8
  ming32-make install
==== Installing OpenCV ====
==== Configuring OpenCV ====
=== Building and running an example ===
add D:\opencv-build\install\bin to path
modify the .pro file like this:
#-------------------------------------------------
#
# Project created by QtCreator 2017-03-05T12:30:06
#
#-------------------------------------------------
QT      += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
INCLUDEPATH += D:\opencv-build\install\include
LIBS += -LD:\opencv-build\install\lib \
    -lopencv_core231.dll \
    -lopencv_highgui231.dll \
    -lopencv_imgproc231.dll \
    -lopencv_features2d231.dll \
    -lopencv_calib3d231.dll
== Windows, Binary Qt, openCV from source ==
Todo: Qt instructions the same as above,
Todo: Cmake instructions the same as above
=== OpenCV ===
==== Getting OpenCV ====
Download openCV from [https://sourceforge.net/projects/opencvlibrary/ sourceforge]. In this guide, version [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/ 3.2.0] is used. Start opencv-3.2.0-vc14.exe and let it extract to d:\ Now the folder d:\opencv is created.
==== Compiling OpenCV ====
==== Compiling OpenCV ====
Start C:\Program Files\CMake\bin\cmake-gui.exe then choose the following settings:
Start C:\Program Files\CMake\bin\cmake-gui.exe then choose the following settings:
   Where is the source code: D:/opencv/sources
   Where is the source code: D:\opencv\sources
   where to build the binaries: D:\opencv-build
   where to build the binaries: D:\opencv-build
Then click Configure, let cmake create the build directory, and choose the following settings:
Then click Configure, let cmake create the build directory, and choose the following settings:
   Specify the generator for this project: MinGW Makefiles
   Specify the generator for this project: MinGW Makefiles
   Specify native compilers, next
   Specify native compilers, next
   Compilers C: D:/Qt/Tools/mingw530_32/bin/gcc.exe
   Compilers C: D:\Qt\Tools\mingw530_32\bin\gcc.exe
   Compilers C++: D:/Qt/Tools/mingw530_32/bin/g++.exe
   Compilers C++: D:\Qt\Tools\mingw530_32\bin\g++.exe
   Finish
   Finish
 
<syntaxhighlight>
  Check the box [X]WITH_QT
Check the box [X]WITH_QT
Then click configure again
Check the box [X]WITH_OPENGL
set Qt5_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5
</syntaxhighlight>
Uncheck the box []ENABLE_PRECOMPILED_HEADERS
Then click configure again.
  Set QT_MAKE_EXECUTABLE to D:\Qt\5.9\mingw53_32\bin\qmake.exe
  Set Qt5Concurrent_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Concurrent
  Set Qt5Core_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Core
  Set Qt5Gui_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Gui
  Set Qt5Test_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Test
  Set Qt5Widgets_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Widgets
  Set Qt5OpenGL_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5OpenGL
   Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo
   Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo
   Set Qt5Core_DIR to D:\Qt\5.8\mingw53_32\lib\cmake\Qt5Core
   Set OPENCV_VS_VERSIONINFO_SKIP=1
  Set Qt5Concurrent_DIR D:\Qt\5.8\mingw53_32\lib\cmake\Qt5Concurrent
  Set Qt5Core_DIR D:\Qt\5.8\mingw53_32\lib\cmake\Qt5Core
  Set Qt5Gui_DIR D:\Qt\5.8\mingw53_32\lib\cmake\Qt5Gui
  Set Qt5Test_DIR D:\Qt\5.8\mingw53_32\lib\cmake\Qt5Test
  Set Qt5Widgets_DIR D:\Qt\5.8\mingw53_32\lib\cmake\Qt5Widgets


Then click configure again
Then click configure again
Then click generate   
Then click generate   


Next open cmd, and type the following commands. To speed up the compile, the -j flag can be used to run multiple compile jobs simultaneously. On an 8 core CPU, you can set it to 8 or higher, so all cores are used.
Next open cmd, and type the following commands. To speed up the compile, the -j flag can be used to run multiple compile jobs simultaneously. On an 8 core CPU, you can set it to 8 or higher, so all cores are used. On a core i7-3770@3.4GHz with 8GB ram, the compile takes about 6 minutes.
   d:
   d:
   cd d:\
   cd d:\
   cd opencv-build   
   cd opencv-build   
   mingw32-make -j 8
   mingw32-make -j 8
   mingw32-make install
   mingw32-make install
 
 
 
 
 
 
 
 
    
    
    
    
 
If, in the file  opencv/sources/modules/videoio/src/cap_dshow.cpp, you have the following error : 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...
== Windows, precompiled binaries, new versions ==


try this :put the following line: #define NO_DSHOW_STRSAFE, before the line : #include "DShow.h"


=== Install OpenCV ===
If you have the error: ‘nullptr’ was not declared in this scope..  
Download the newest version from [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/ sourceforge ]. At the time of this writing, [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/ opencv-3.2.0-vc14.exe] (118MB) is the latest version. Execute the program, then enter a location with at least 460MB free. This how to assumes OpenCV to be extracted to "d:\", so the folder d:\opencv exists.


=== Install Qt ===
try this: in cmake check the box ENABLE_CXX11
Download the Qt installer from [https://www.qt.io/download-open-source/ www.qt.io], then choose "Download now" This will then download qt-unified-windows-x86-2.0.5-online.exe Execute the program, then choose the following settings:
welcome next
qt account skip
setup-Qt next
installation folder: D:\Qt
select components: next (accept defaults)
License Agreement: agree and next
start menu shortcuts: next
ready to install: install


=== Adjust Qt ===
If, in the file modules\videoio\src\cap_msmf.cpp you have the error: using invalid field '{anonymous}::ComPtr<T>::p'..
Run D:\Qt\MaintenanceTool.exe then use the following options:
maintain Qt: Qt Account: Skip
Setup Qt: Add or remove components: Next
Select components: Qt-Qt5.8-MinGW 5.3.0 32 bit
Select components: tools-MinGW 5.3.0 32 bit: next :update


=== Configure Qt and OpenCV ===
try this: in cmake unchecking WITH_MSMF


==== Add OpenCV compiled libraries to the windows PATH variable====
Open the control panel,
System and Security,
System,
Advanced system settings,
Environment Variables,
System Variables,
Variable Name: Path
Variable value: ;D:\opencv-build\install\x86\mingw\bin
 
=== Compile and run the example ===
=== Compile and run the example ===
Run D:\Qt\Tools\QtCreator\bin\qtcreator.exe
  File-New file or project-Qt Widgets Application-choose
  enter a name and a location: next
  select all kits: next
  Class information: MainWindow (defaults): Next
  Project management: Finish
Now a new project is made.
modify the .pro file like this:
modify the .pro file like this:
<syntaxhighlight lang="cpp">
#-------------------------------------------------
#-------------------------------------------------
#
#
Line 225: Line 152:
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets


TARGET = untitled
TARGET = opencvtest
TEMPLATE = app
TEMPLATE = app


Line 247: Line 174:
FORMS    += mainwindow.ui
FORMS    += mainwindow.ui


INCLUDEPATH += D:\opencv-build\install\include
INCLUDEPATH += D:\opencv\build\include
LIBS += -LD:\opencv-build\install\x86\mingw\lib \
 
    -lopencv_core320.dll \
LIBS += D:\opencv-build\bin\libopencv_core320.dll
    -lopencv_highgui320.dll \
LIBS += D:\opencv-build\bin\libopencv_highgui320.dll
    -lopencv_imgcodecs320.dll \
LIBS += D:\opencv-build\bin\libopencv_imgcodecs320.dll
    -lopencv_imgproc320.dll \
LIBS += D:\opencv-build\bin\libopencv_imgproc320.dll
    -lopencv_features2d320.dll \
LIBS += D:\opencv-build\bin\libopencv_features2d320.dll
    -lopencv_calib3d320.dll
LIBS += D:\opencv-build\bin\libopencv_calib3d320.dll


# more correct variant, how set includepath and libs for mingw
# add system variable: OPENCV_SDK_DIR=D:/opencv/opencv-build/install
# read http://doc.qt.io/qt-5/qmake-variable-reference.html#libs
#INCLUDEPATH += $$(OPENCV_SDK_DIR)/include
#LIBS += -L$$(OPENCV_SDK_DIR)/x86/mingw/lib \
#        -lopencv_core320        \
#        -lopencv_highgui320    \
#        -lopencv_imgcodecs320  \
#        -lopencv_imgproc320    \
#        -lopencv_features2d320  \
#        -lopencv_calib3d320
</syntaxhighlight>
and modify mainwindow.cpp like this:
and modify mainwindow.cpp like this:
<syntaxhighlight lang="cpp">
#include "mainwindow.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_mainwindow.h"
Line 262: Line 204:
#include <opencv2/core/core.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui.hpp>
/*
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>*/


MainWindow::MainWindow(QWidget *parent) :
MainWindow::MainWindow(QWidget *parent) :
Line 273: Line 212:


     // read an image
     // read an image
     cv::Mat image = cv::imread("f://1.jpg" , 1 );
     cv::Mat image = cv::imread("f://1.jpg", 1);
     // create image window named "My Image"
     // create image window named "My Image"
     cv::namedWindow("My Image");
     cv::namedWindow("My Image");
Line 284: Line 223:
     delete ui;
     delete ui;
}
}
</syntaxhighlight>
Place an image with the name "1.img" in the root of F:\, then run the example. Now 2 windows should pop up. One with the image, and one with an empty window.
NOTE: after changing the PATH variable, restart QtCreator to load the new value
 
When this example is run, 2 windows pop up. One with the image, and one with an empty window.

Latest revision as of 20:20, 24 January 2020

How to setup Qt and openCV on Windows

Introduction

This article shows how to install Qt, build OpenCV, and run a basic OpenCV example. This article assumes Windows 10 has just been installed.

This procedure requires close to 10GB of disk space:

Qt: 5.06GB
opencv: 522MB
opencv-Build: 3.95GB
downloads:152MB  

This article uses information from the following pages: http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html?highlight=installation http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml

Windows 10, Qt 5.9, OpenCV 3.2.0

This guide is actual for Qt 5.12.2 with MinGW 7.3.0 and OpenCV 4.0.1 too

Qt

Installation

Download the Qt installer from www.qt.io, then choose "Download now". This will then download qt-unified-windows-x86-2.0.5-online.exe. Execute the program, then choose the following settings:

 Welcome to the Qt online installer: next
 Qt Account - your unified login to everything Qt: skip
 Setup-Qt: next
 installation folder: D:\Qt
 select components: Qt-Qt5.9-MingGW 5.3.0 32 bit
 select components: Qt-Tools-MinGW 5.3.0
 License Agreement: agree and next
 start menu shortcuts: next
 ready to install: install

Testing

Run D:\Qt\Tools\QtCreator\bin\qtcreator.exe

 File-New file or project-Qt Widgets Application-choose
 enter a name and a location: next
 select all kits: next
 Class information: MainWindow (defaults): Next
 Project management: Finish

Now a new project is made. Start debugging by choosing

 Debug-Start Debugging-Start debugging (F5)

Now the Qt tab in the Windows taskbar should turn into a progress bar. After some time a new empty window should pop up. Stop debugging either by pressing the red cross in the top right of this new window, or choose

 Debug-Stop debugging  

Adjust Qt

When you need to add, remove or update a component of Qt, this can be done by running D:\Qt\MaintenanceTool.exe:

 maintain Qt: Qt Account: Skip
 Setup Qt: Add or remove components: Next
 Select components: 
 next :update 

Cmake

Download cmake from cmake.org. In this guide, 3.7.2 is used. Start cmake-3.7.2-win64-x64.msi, then choose the following settinge:

 Welcome to the CMake etup Wizzard: next
 End-User License Agreement: [X] Accept and next
 Install options: [X] Add CMake to the system PATH for all users, next
 Destination folder: C:\Program Files\CMake (default), next
 Ready to install CMake, Install

OpenCV

Getting OpenCV

Download openCV from sourceforge. In this guide, version 3.2.0 is used. Start opencv-3.2.0-vc14.exe and let it extract to d:\ . Now the folder d:\opencv is created.

Add minGW to the windows PATH variable

Open the control panel, 
System and Security, 
System, 
Advanced system settings,
Environment Variables,
System Variables,
Variable Name: Path
Variable value: ;D:\Qt\Tools\mingw530_32\bin

Compiling OpenCV

Start C:\Program Files\CMake\bin\cmake-gui.exe then choose the following settings:

 Where is the source code: D:\opencv\sources
 where to build the binaries: D:\opencv-build

Then click Configure, let cmake create the build directory, and choose the following settings:

 Specify the generator for this project: MinGW Makefiles
 Specify native compilers, next
 Compilers C: D:\Qt\Tools\mingw530_32\bin\gcc.exe
 Compilers C++: D:\Qt\Tools\mingw530_32\bin\g++.exe
 Finish
 Check the box [X]WITH_QT
 Check the box [X]WITH_OPENGL
 set Qt5_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5
Uncheck the box []ENABLE_PRECOMPILED_HEADERS

Then click configure again.

 Set QT_MAKE_EXECUTABLE to D:\Qt\5.9\mingw53_32\bin\qmake.exe
 Set Qt5Concurrent_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Concurrent
 Set Qt5Core_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Core
 Set Qt5Gui_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Gui
 Set Qt5Test_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Test
 Set Qt5Widgets_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5Widgets
 Set Qt5OpenGL_DIR to D:\Qt\5.9\mingw53_32\lib\cmake\Qt5OpenGL
 Set CMAKE_BUILD_TYPE to Release or RelWithDebInfo
 Set OPENCV_VS_VERSIONINFO_SKIP=1

Then click configure again Then click generate

Next open cmd, and type the following commands. To speed up the compile, the -j flag can be used to run multiple compile jobs simultaneously. On an 8 core CPU, you can set it to 8 or higher, so all cores are used. On a core i7-3770@3.4GHz with 8GB ram, the compile takes about 6 minutes.

 d:
 cd d:\
 cd opencv-build  
 mingw32-make -j 8
 mingw32-make install
 
 

If, in the file opencv/sources/modules/videoio/src/cap_dshow.cpp, you have the following error : 'sprintf_instead_use_StringCbPrintfA_or_StringCchPrintfA' was not declared in this scope ...

try this :put the following line: #define NO_DSHOW_STRSAFE, before the line : #include "DShow.h"

If you have the error: ‘nullptr’ was not declared in this scope..

try this: in cmake check the box ENABLE_CXX11

If, in the file modules\videoio\src\cap_msmf.cpp you have the error: using invalid field '{anonymous}::ComPtr<T>::p'..

try this: in cmake unchecking WITH_MSMF

Add OpenCV compiled libraries to the windows PATH variable

Open the control panel, 
System and Security, 
System, 
Advanced system settings,
Environment Variables,
System Variables,
Variable Name: Path
Variable value: ;D:\opencv-build\install\x86\mingw\bin
 

Compile and run the example

Run D:\Qt\Tools\QtCreator\bin\qtcreator.exe

 File-New file or project-Qt Widgets Application-choose
 enter a name and a location: next
 select all kits: next
 Class information: MainWindow (defaults): Next
 Project management: Finish

Now a new project is made.

modify the .pro file like this:

#-------------------------------------------------
#
# Project created by QtCreator 2017-03-05T12:30:06
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = opencvtest
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += D:\opencv\build\include

LIBS += D:\opencv-build\bin\libopencv_core320.dll
LIBS += D:\opencv-build\bin\libopencv_highgui320.dll
LIBS += D:\opencv-build\bin\libopencv_imgcodecs320.dll
LIBS += D:\opencv-build\bin\libopencv_imgproc320.dll
LIBS += D:\opencv-build\bin\libopencv_features2d320.dll
LIBS += D:\opencv-build\bin\libopencv_calib3d320.dll

# more correct variant, how set includepath and libs for mingw
# add system variable: OPENCV_SDK_DIR=D:/opencv/opencv-build/install
# read http://doc.qt.io/qt-5/qmake-variable-reference.html#libs

#INCLUDEPATH += $$(OPENCV_SDK_DIR)/include

#LIBS += -L$$(OPENCV_SDK_DIR)/x86/mingw/lib \
#        -lopencv_core320        \
#        -lopencv_highgui320     \
#        -lopencv_imgcodecs320   \
#        -lopencv_imgproc320     \
#        -lopencv_features2d320  \
#        -lopencv_calib3d320

and modify mainwindow.cpp like this:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // read an image
    cv::Mat image = cv::imread("f://1.jpg", 1);
    // create image window named "My Image"
    cv::namedWindow("My Image");
    // show the image on window
    cv::imshow("My Image", image);
}

MainWindow::~MainWindow()
{
    delete ui;
}

Place an image with the name "1.img" in the root of F:\, then run the example. Now 2 windows should pop up. One with the image, and one with an empty window.