프로그래밍/Qt 프레임워크

[Qt 6] Opencv 4 적용법(window_QT.cpp, obj section 오류 해결)

하루에 한번 방문하기 2021. 3. 8. 18:16

 

Qt6은 공식문서와 함께 Qt5처럼 공식 문서로 일단 진행합니다.

wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows

 

How to setup Qt and openCV on Windows - Qt Wiki

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

wiki.qt.io

Cmake에서 WITH_QT를 체크하면 Qt5 DIR을 요구할 텐데,

모든 CMakeList 파일 안의 Qt5를 Qt6으로, QT5를 QT6으로 바꿔주세요.

www.programmersought.com/article/17677111900/ 

 

QT6.0(C++)+OpenCV4 environment configuration and installation under Windows10 (unsuccessful) - Programmer Sought

QT6.0(C++)+OpenCV4 under Windows10 (1) Due to work reasons, we must begin to join the development army of QT. To be honest, I haven't done QT development before, and it's a zero basic entry. This article records the installation and configuration of the en

www.programmersought.com

(원문 (중국어) blog.csdn.net/u011826081/article/details/113081099 )

 

이제 Cmake에서 opencv source에 대해 Configue, Generate가 끝났다면

 mingw32를 환경변수 설정하고 cmd에서 "mingw32-make -j 8" 타이핑하세요.

 -j 8은 cpu의 8 코어를 전부 사용하겠다는 뜻입니다.

 저는 노트북 4코어..

100%로 완료되었다면 이어서 "mingw32-make install"를 타이핑하세요.

그 후 C:\opencv\newbuild\install\x64\mingw\bin 경로를 환경변수 설정하면 Qt6에서 사용할 수 있습니다.


Qt 6 OpenGL 관련 오류(QGLWidget.h)

만약 mingw 실행 도중에 QGLWidget.h 관련 오류가 나면

 QGLWidget.h를 QWidget으로 직접 바꾸어야 합니다. 

 (최신 버전 QT6은 QGLWidget이 버려지고 이름이 QWidget으로 바뀌었습니다.)

그리고 WITH_OPENGL 체크 해제해주세요.

(QGLWidget.h) OpenGL은 Qt 6 이상부터 사용되지 않습니다.

출처 : 

doc.qt.io/qt-6/opengl-changes-qt6.html

 

Changes to Qt OpenGL | Qt OpenGL

Changes to Qt OpenGL Qt 6 is a result of the conscious effort to make the framework more efficient and easy to use. We try to maintain binary and source compatibility for all the public APIs in each release. But some changes were inevitable in an effort to

doc.qt.io


mingw32-make: *** [Makefile:181: all] Error 2 오류

아무 이유 없이 중지

이유 없이 중지되면 OPENCV_ENABLE_ALLOCATOR_STATS를 해제하고 재시도해주세요.


window_QT.cpp 오류

급한 분들은 이거 다운 받아서 경로에 덮어씌워주세요.

window_QT.cpp
0.09MB

덮어쓰는 기본 경로는 C:\opencv\sources\modules\highgui\src 입니다.

 

설명 :

myGlobalLayout->setMargin() 이렇게 더 이상 쓰지 않는 함수 때문에 설치가 멈추면

Qt문서에서 대체할 함수 검색해서 고쳐줘야 돌아갑니다.

setMargin 함수는 setContentsMargins 로 대체해야 합니다.

 

orientation 함수는 angleDelta 함수로 대체해야 합니다.

if (category == mouse_wheel) {
            QWheelEvent *we = (QWheelEvent *) evnt;
            cv_event = ((we->orientation() == Qt::Vertical) ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL);
            flags |= (we->orientation() & 0xffff)<<16;
            return;
        }

https://forum.qt.io/topic/57922/qwheelevent-orientation-replaced-with-angledelta/3

번역 : angleDelta() QPoint를 반환합니다. 점의 x 부분은 수평 각도와 y 수직을 나타냅니다.
따라서 방향 확인을 원하면 이들 중 0이 아닌 것을 확인하십시오.

저는 이렇게 고쳤습니다.

if (cv_event == -1) {
        if (category == mouse_wheel) {
            QWheelEvent *we = (QWheelEvent *) evnt;
            cv_event = ((we->angleDelta().y != 0) ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL);
            flags |= (((we->angleDelta().y != 0)? 0x2 : 0x1) & 0xffff)<<16;
            return;
        }

이것 말고도 대체해야하는 함수가 꽤 있습니다. 혹시 직접 할 분은 Qt 문서 찾아보세요.


어셈블리 파일 크기 문제(too many sections, obj File too big)

이 문제는 컴파일 내용 크기가 2^16 entrie 보다 크기 때문에

 GNU 어셈블러에서 오브젝트 파일을 생성할 수 없어 발생합니다. (Mingw는 GNU어셈블러 파생이라고 합니다)

이미지 출처 : https://hwan-shell.tistory.com/185

 

 해결하기 위해 CMake에서 Flag를 설정합니다.

 -Wa,-mbig-obj

-Wa는 뒤의 옵션을 어셈블러에 전달하는 것을 의미합니다. 

-mbig-obj는 어셈블러에서 큰 소스의 컴파일을 가능하게 합니다.

 


마지막으로..

CMake나 Mingw는 Qt 설치할 때 같이 설치되는데 중복 설치 조심하세요.

 환경 변수 설정할 때 위에 있는 게 먼저 인식됩니다.


진심으로 너무 반가운 opencv 컴파일과 빌드.. 1년간 가장 기뻤네요