QT + OpenCV Example

Source

  1. #include <QCoreApplication>
  2.  
  3. #include <opencv2/opencv.hpp>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     //QCoreApplication a(argc, argv);
  8.  
  9.     using namespace cv;
  10.     Mat image = imread("/Users/water/Downloads/Share.jpg");
  11.     imshow("Output", image);
  12.  
  13.     waitKey(1000);
  14.  
  15.     return 0;
  16.  
  17.     //return a.exec();
  18. }

pro

  1. QT -= gui
  2.  
  3. CONFIG += c++11 console
  4. CONFIG -= app_bundle
  5.  
  6. # The following define makes your compiler emit warnings if you use
  7. # any Qt feature that has been marked deprecated (the exact warnings
  8. # depend on your compiler). Please consult the documentation of the
  9. # deprecated API in order to know how to port your code away from it.
  10. DEFINES += QT_DEPRECATED_WARNINGS
  11.  
  12. # You can also make your code fail to compile if it uses deprecated APIs.
  13. # In order to do so, uncomment the following line.
  14. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  15. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  16.  
  17. SOURCES += \
  18.         main.cpp
  19.  
  20. # Default rules for deployment.
  21. qnx: target.path = /tmp/$${TARGET}/bin
  22. else: unix:!android: target.path = /opt/$${TARGET}/bin
  23. !isEmpty(target.path): INSTALLS += target
  24.  
  25. INCLUDEPATH += /usr/local/include
  26. INCLUDEPATH += /usr/local/include/opencv
  27. INCLUDEPATH += /usr/local/include/opencv2
  28. LIBS += -L/usr/local/lib \
  29.  -lopencv_core \
  30.  -lopencv_highgui \
  31.  -lopencv_imgproc \
  32.   -lopencv_imgcodecs \

How to Build Qt From Source Codes (QT 5.12 + Visual Studio 2017)

msvc-desktop.conf

  1. C:\Qt\5.12.0\Src\qtbase\mkspecs\common\msvc-desktop.conf
  2.  
  3. QMAKE_CFLAGS_RELEASE    = $QMAKE_CFLAGS_OPTIMIZE -MT
  4. QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += $QMAKE_CFLAGS_OPTIMIZE -Zi -MT
  5. QMAKE_CFLAGS_DEBUG      = -Zi -MTd

qt5vars.cmd

  1. REM Set up \Microsoft Visual Studio 2017, where arch is \c amd64, \c x86, etc.
  2. CALL "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x86
  3. SET _ROOT=C:\Qt\5.12.0\Src
  4. SET PATH=%_ROOT%\qtbase\bin;%_ROOT%\gnuwin32\bin;%PATH%
  5. REM Uncomment the below line when using a git checkout of the source repository
  6. REM SET PATH=%_ROOT%\qtrepotools\bin;%PATH%
  7. SET _ROOT=

make & make install

  1. C:\Qt\5.12.0\Src>qt5vars.cmd
  2. C:\Qt\5.12.0\Src>configure -opensource -confirm-license
  3. C:\Qt\5.12.0\Src>jom -j 8
  4. C:\Qt\5.12.0\Src>jom install

QT5 International

Create Project & Generate .ts

  1. lupdate TestInternational.pro -ts zh_CN.ts

Translate with Linguist

Save & Release to Generate .qm

Add Code to Choose Language

  1. #include "widget.h"
  2. #include <QApplication>
  3.  
  4. #include <QTranslator>
  5.  
  6. #include <QDebug>
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     QApplication a(argc, argv);
  11.  
  12.     QTranslator trans;
  13.      if(trans.load(a.applicationDirPath() + "/../../../zh_CN.qm"))
  14.         a.installTranslator(&trans);
  15.  
  16.     Widget w;
  17.     w.show();
  18.  
  19.     return a.exec();
  20. }

The Result