Skip to content

Commit 24f43e7

Browse files
xhawk18alalek
andauthored
Merge pull request #20183 from xhawk18:3.4
* improve compatibility for qt 6. * cmake(highgui): rework Qt dependency support * cmake(highgui): workaround Qt5Config.cmake "components" bug Co-authored-by: Alexander Alekhin <[email protected]>
1 parent 48c985e commit 24f43e7

File tree

5 files changed

+146
-86
lines changed

5 files changed

+146
-86
lines changed

CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,12 +1232,17 @@ status("")
12321232
status(" GUI: ")
12331233

12341234
if(WITH_QT OR HAVE_QT)
1235-
if(HAVE_QT5)
1236-
status(" QT:" "YES (ver ${Qt5Core_VERSION_STRING})")
1237-
status(" QT OpenGL support:" HAVE_QT_OPENGL THEN "YES (${Qt5OpenGL_LIBRARIES} ${Qt5OpenGL_VERSION_STRING})" ELSE NO)
1238-
elseif(HAVE_QT)
1235+
if(HAVE_QT)
12391236
status(" QT:" "YES (ver ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH} ${QT_EDITION})")
1240-
status(" QT OpenGL support:" HAVE_QT_OPENGL THEN "YES (${QT_QTOPENGL_LIBRARY})" ELSE NO)
1237+
if(HAVE_QT_OPENGL)
1238+
if(Qt${QT_VERSION_MAJOR}OpenGL_LIBRARIES)
1239+
status(" QT OpenGL support:" HAVE_QT_OPENGL THEN "YES (${Qt${QT_VERSION_MAJOR}OpenGL_LIBRARIES} ${Qt${QT_VERSION_MAJOR}OpenGL_VERSION_STRING})" ELSE NO)
1240+
else()
1241+
status(" QT OpenGL support:" HAVE_QT_OPENGL THEN "YES (${QT_QTOPENGL_LIBRARY})" ELSE NO)
1242+
endif()
1243+
else()
1244+
status(" QT OpenGL support:" "NO")
1245+
endif()
12411246
else()
12421247
status(" QT:" "NO")
12431248
endif()

cmake/OpenCVDetectVTK.cmake

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,14 @@ if(VTK_VERSION VERSION_LESS "5.8.0")
2626
endif()
2727

2828
# Different Qt versions can't be linked together
29-
if(HAVE_QT5 AND VTK_VERSION VERSION_LESS "6.0.0")
30-
if(VTK_USE_QT)
31-
message(STATUS "VTK support is disabled. Incompatible combination: OpenCV + Qt5 and VTK ver.${VTK_VERSION} + Qt4")
32-
endif()
33-
endif()
34-
35-
# Different Qt versions can't be linked together. VTK 6.0.0 doesn't provide a way to get Qt version it was linked with
36-
if(HAVE_QT5 AND VTK_VERSION VERSION_EQUAL "6.0.0" AND NOT DEFINED FORCE_VTK)
37-
message(STATUS "VTK support is disabled. Possible incompatible combination: OpenCV+Qt5, and VTK ver.${VTK_VERSION} with Qt4")
38-
message(STATUS "If it is known that VTK was compiled without Qt4, please define '-DFORCE_VTK=TRUE' flag in CMake")
29+
if((HAVE_QT AND VTK_USE_QT)
30+
AND NOT DEFINED FORCE_VTK # deprecated
31+
AND NOT DEFINED OPENCV_FORCE_VTK
32+
)
33+
message(STATUS "VTK support is disabled. Possible incompatible combination: OpenCV+Qt, and VTK ver.${VTK_VERSION} with Qt")
34+
message(STATUS "If it is known that VTK was compiled without Qt4, please define '-DOPENCV_FORCE_VTK=TRUE' flag in CMake")
3935
return()
4036
endif()
4137

42-
# Different Qt versions can't be linked together
43-
if(HAVE_QT AND VTK_VERSION VERSION_GREATER "6.0.0" AND NOT ${VTK_QT_VERSION} STREQUAL "")
44-
if(HAVE_QT5 AND ${VTK_QT_VERSION} EQUAL "4")
45-
message(STATUS "VTK support is disabled. Incompatible combination: OpenCV + Qt5 and VTK ver.${VTK_VERSION} + Qt4")
46-
return()
47-
endif()
48-
49-
if(NOT HAVE_QT5 AND ${VTK_QT_VERSION} EQUAL "5")
50-
message(STATUS "VTK support is disabled. Incompatible combination: OpenCV + Qt4 and VTK ver.${VTK_VERSION} + Qt5")
51-
return()
52-
endif()
53-
endif()
54-
5538
set(HAVE_VTK ON)
5639
message(STATUS "Found VTK ${VTK_VERSION} (${VTK_USE_FILE})")

cmake/OpenCVFindLibsGUI.cmake

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,50 @@ if(WITH_WIN32UI)
1111
CMAKE_FLAGS "-DLINK_LIBRARIES:STRING=user32;gdi32")
1212
endif()
1313

14+
macro(ocv_find_package_Qt4)
15+
find_package(Qt4 COMPONENTS QtCore QtGui QtTest ${ARGN})
16+
if(QT4_FOUND)
17+
set(QT_FOUND 1)
18+
ocv_assert(QT_VERSION_MAJOR EQUAL 4)
19+
endif()
20+
endmacro()
21+
22+
macro(ocv_find_package_Qt OCV_QT_VER)
23+
find_package(Qt${OCV_QT_VER} COMPONENTS Core Gui Widgets Test Concurrent ${ARGN} NO_MODULE)
24+
if(Qt${OCV_QT_VER}_FOUND)
25+
set(QT_FOUND 1)
26+
set(QT_VERSION "${Qt${OCV_QT_VER}_VERSION}")
27+
set(QT_VERSION_MAJOR "${Qt${OCV_QT_VER}_VERSION_MAJOR}")
28+
set(QT_VERSION_MINOR "${Qt${OCV_QT_VER}_VERSION_MINOR}")
29+
set(QT_VERSION_PATCH "${Qt${OCV_QT_VER}_VERSION_PATCH}")
30+
set(QT_VERSION_TWEAK "${Qt${OCV_QT_VER}_VERSION_TWEAK}")
31+
set(QT_VERSION_COUNT "${Qt${OCV_QT_VER}_VERSION_COUNT}")
32+
endif()
33+
endmacro()
34+
1435
# --- QT4 ---
15-
ocv_clear_vars(HAVE_QT HAVE_QT5)
1636
if(WITH_QT)
17-
if(NOT WITH_QT EQUAL 4)
18-
find_package(Qt5 COMPONENTS Core Gui Widgets Test Concurrent REQUIRED NO_MODULE)
19-
if(Qt5_FOUND)
20-
set(HAVE_QT5 ON)
21-
set(HAVE_QT ON)
22-
find_package(Qt5 COMPONENTS OpenGL QUIET)
23-
if(Qt5OpenGL_FOUND)
24-
set(QT_QTOPENGL_FOUND ON)
25-
endif()
37+
if(NOT WITH_QT GREATER 0)
38+
# BUG: Qt5Config.cmake script can't handle components properly: find_package(QT NAMES Qt6 Qt5 REQUIRED NO_MODULE COMPONENTS Core Gui Widgets Test Concurrent)
39+
ocv_find_package_Qt(6 QUIET)
40+
if(NOT QT_FOUND)
41+
ocv_find_package_Qt(5 QUIET)
42+
endif()
43+
if(NOT QT_FOUND)
44+
ocv_find_package_Qt4(QUIET)
2645
endif()
46+
elseif(WITH_QT EQUAL 4)
47+
ocv_find_package_Qt4(REQUIRED)
48+
else() # WITH_QT=<major version>
49+
ocv_find_package_Qt("${WITH_QT}" REQUIRED)
2750
endif()
28-
29-
if(NOT HAVE_QT)
30-
find_package(Qt4 REQUIRED QtCore QtGui QtTest)
31-
if(QT4_FOUND)
32-
set(HAVE_QT TRUE)
51+
if(QT_FOUND)
52+
set(HAVE_QT ON)
53+
if(QT_VERSION_MAJOR GREATER 4)
54+
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS OpenGL QUIET)
55+
if(Qt${QT_VERSION_MAJOR}OpenGL_FOUND)
56+
set(QT_QTOPENGL_FOUND ON) # HAVE_QT_OPENGL is defined below
57+
endif()
3358
endif()
3459
endif()
3560
endif()

modules/highgui/CMakeLists.txt

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,46 +41,55 @@ file(GLOB highgui_ext_hdrs
4141
# Removing WinRT API headers by default
4242
list(REMOVE_ITEM highgui_ext_hdrs "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/highgui_winrt.hpp")
4343

44-
if(HAVE_QT5)
45-
# "Automoc" doesn't work properly with opencv_world build, use QT5_WRAP_CPP() directly
46-
#set(CMAKE_AUTOMOC ON)
47-
48-
set(CMAKE_INCLUDE_CURRENT_DIR ON)
49-
50-
QT5_ADD_RESOURCES(_RCC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.qrc)
51-
QT5_WRAP_CPP(_MOC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h)
52-
list(APPEND highgui_srcs
53-
${CMAKE_CURRENT_LIST_DIR}/src/window_QT.cpp
54-
${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h
55-
${_MOC_OUTFILES}
56-
${_RCC_OUTFILES})
57-
58-
foreach(dt5_dep Core Gui Widgets Test Concurrent)
59-
add_definitions(${Qt5${dt5_dep}_DEFINITIONS})
60-
include_directories(${Qt5${dt5_dep}_INCLUDE_DIRS})
61-
list(APPEND HIGHGUI_LIBRARIES ${Qt5${dt5_dep}_LIBRARIES})
62-
endforeach()
44+
if(HAVE_QT)
45+
if(QT_VERSION_MAJOR GREATER 4)
46+
# "Automoc" doesn't work properly with opencv_world build, use QT<ver>_WRAP_CPP() directly
47+
#set(CMAKE_AUTOMOC ON)
48+
49+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
50+
51+
if(QT_VERSION_MAJOR EQUAL 6)
52+
QT6_ADD_RESOURCES(_RCC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.qrc)
53+
QT6_WRAP_CPP(_MOC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h)
54+
elseif(QT_VERSION_MAJOR EQUAL 5)
55+
QT5_ADD_RESOURCES(_RCC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.qrc)
56+
QT5_WRAP_CPP(_MOC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h)
57+
else()
58+
message(FATAL_ERROR "Unsuported QT version: ${QT_VERSION_MAJOR}")
59+
endif()
6360

64-
if(HAVE_QT_OPENGL)
65-
add_definitions(${Qt5OpenGL_DEFINITIONS})
66-
include_directories(${Qt5OpenGL_INCLUDE_DIRS})
67-
list(APPEND HIGHGUI_LIBRARIES ${Qt5OpenGL_LIBRARIES})
68-
endif()
61+
list(APPEND highgui_srcs
62+
${CMAKE_CURRENT_LIST_DIR}/src/window_QT.cpp
63+
${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h
64+
${_MOC_OUTFILES}
65+
${_RCC_OUTFILES})
66+
67+
set(qt_deps Core Gui Widgets Test Concurrent)
68+
if(HAVE_QT_OPENGL)
69+
list(APPEND qt_deps OpenGL)
70+
endif()
6971

70-
elseif(HAVE_QT)
71-
if (HAVE_QT_OPENGL)
72-
set(QT_USE_QTOPENGL TRUE)
73-
endif()
74-
include(${QT_USE_FILE})
72+
foreach(dt_dep ${qt_deps})
73+
add_definitions(${Qt${QT_VERSION_MAJOR}${dt_dep}_DEFINITIONS})
74+
include_directories(${Qt${QT_VERSION_MAJOR}${dt_dep}_INCLUDE_DIRS})
75+
list(APPEND HIGHGUI_LIBRARIES ${Qt${QT_VERSION_MAJOR}${dt_dep}_LIBRARIES})
76+
endforeach()
77+
else()
78+
ocv_assert(QT_VERSION_MAJOR EQUAL 4)
79+
if (HAVE_QT_OPENGL)
80+
set(QT_USE_QTOPENGL TRUE)
81+
endif()
82+
include(${QT_USE_FILE})
7583

76-
QT4_ADD_RESOURCES(_RCC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.qrc)
77-
QT4_WRAP_CPP(_MOC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h)
84+
QT4_ADD_RESOURCES(_RCC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.qrc)
85+
QT4_WRAP_CPP(_MOC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h)
7886

79-
list(APPEND HIGHGUI_LIBRARIES ${QT_LIBRARIES})
80-
list(APPEND highgui_srcs ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.cpp ${_MOC_OUTFILES} ${_RCC_OUTFILES})
81-
ocv_check_flag_support(CXX -Wno-missing-declarations _have_flag "")
82-
if(${_have_flag})
83-
set_source_files_properties(${_RCC_OUTFILES} PROPERTIES COMPILE_FLAGS -Wno-missing-declarations)
87+
list(APPEND HIGHGUI_LIBRARIES ${QT_LIBRARIES})
88+
list(APPEND highgui_srcs ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.cpp ${_MOC_OUTFILES} ${_RCC_OUTFILES})
89+
ocv_check_flag_support(CXX -Wno-missing-declarations _have_flag "")
90+
if(${_have_flag})
91+
set_source_files_properties(${_RCC_OUTFILES} PROPERTIES COMPILE_FLAGS -Wno-missing-declarations)
92+
endif()
8493
endif()
8594
elseif(WINRT)
8695
if(NOT WINRT_8_0)

modules/highgui/src/window_QT.cpp

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@
6464
#endif
6565

6666

67+
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
68+
#define Qt_MiddleButton Qt::MiddleButton
69+
inline Qt::Orientation wheelEventOrientation(QWheelEvent *we) {
70+
if (std::abs(we->angleDelta().x()) < std::abs(we->angleDelta().y()))
71+
return Qt::Vertical;
72+
else
73+
return Qt::Horizontal;
74+
}
75+
inline int wheelEventDelta(QWheelEvent *we) {
76+
if(wheelEventOrientation(we) == Qt::Vertical)
77+
return we->angleDelta().y();
78+
else
79+
return we->angleDelta().x();
80+
}
81+
inline QPoint wheelEventPos(QWheelEvent *we) {
82+
return we->position().toPoint();
83+
}
84+
#else
85+
#define Qt_MiddleButton Qt::MidButton
86+
inline Qt::Orientation wheelEventOrientation(QWheelEvent *we) {
87+
return we->orientation();
88+
}
89+
inline int wheelEventDelta(QWheelEvent *we) {
90+
return we->delta();
91+
}
92+
inline QPoint wheelEventPos(QWheelEvent *we) {
93+
return we->pos();
94+
}
95+
96+
#endif
97+
98+
6799
//Static and global first
68100
static GuiReceiver *guiMainThread = NULL;
69101
static int parameterSystemC = 1;
@@ -1579,7 +1611,9 @@ CvWinProperties::CvWinProperties(QString name_paraWindow, QObject* /*parent*/)
15791611
myLayout->setObjectName(QString::fromUtf8("boxLayout"));
15801612
myLayout->setContentsMargins(0, 0, 0, 0);
15811613
myLayout->setSpacing(0);
1614+
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
15821615
myLayout->setMargin(0);
1616+
#endif
15831617
myLayout->setSizeConstraint(QLayout::SetFixedSize);
15841618
setLayout(myLayout);
15851619

@@ -1957,7 +1991,9 @@ void CvWindow::createBarLayout()
19571991
myBarLayout->setObjectName(QString::fromUtf8("barLayout"));
19581992
myBarLayout->setContentsMargins(0, 0, 0, 0);
19591993
myBarLayout->setSpacing(0);
1994+
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
19601995
myBarLayout->setMargin(0);
1996+
#endif
19611997
}
19621998

19631999

@@ -1967,7 +2003,9 @@ void CvWindow::createGlobalLayout()
19672003
myGlobalLayout->setObjectName(QString::fromUtf8("boxLayout"));
19682004
myGlobalLayout->setContentsMargins(0, 0, 0, 0);
19692005
myGlobalLayout->setSpacing(0);
2006+
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
19702007
myGlobalLayout->setMargin(0);
2008+
#endif
19712009
setMinimumSize(1, 1);
19722010

19732011
if (param_flags == CV_WINDOW_AUTOSIZE)
@@ -2205,7 +2243,7 @@ void CvWindow::icvLoadControlPanel()
22052243
}
22062244
if (t->type == type_CvButtonbar)
22072245
{
2208-
int subsize = settings.beginReadArray(QString("buttonbar")+i);
2246+
int subsize = settings.beginReadArray(QString("buttonbar%1").arg(i));
22092247

22102248
if ( subsize == ((CvButtonbar*)t)->layout()->count() )
22112249
icvLoadButtonbar((CvButtonbar*)t,&settings);
@@ -2236,7 +2274,7 @@ void CvWindow::icvSaveControlPanel()
22362274
}
22372275
if (t->type == type_CvButtonbar)
22382276
{
2239-
settings.beginWriteArray(QString("buttonbar")+i);
2277+
settings.beginWriteArray(QString("buttonbar%1").arg(i));
22402278
icvSaveButtonbar((CvButtonbar*)t,&settings);
22412279
settings.endArray();
22422280
}
@@ -2396,14 +2434,14 @@ void OCVViewPort::icvmouseHandler(QMouseEvent* evnt, type_mouse_event category,
23962434
flags |= CV_EVENT_FLAG_LBUTTON;
23972435
if(buttons & Qt::RightButton)
23982436
flags |= CV_EVENT_FLAG_RBUTTON;
2399-
if(buttons & Qt::MidButton)
2437+
if(buttons & Qt_MiddleButton)
24002438
flags |= CV_EVENT_FLAG_MBUTTON;
24012439

24022440
if (cv_event == -1) {
24032441
if (category == mouse_wheel) {
24042442
QWheelEvent *we = (QWheelEvent *) evnt;
2405-
cv_event = ((we->orientation() == Qt::Vertical) ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL);
2406-
flags |= (we->delta() & 0xffff)<<16;
2443+
cv_event = ((wheelEventOrientation(we) == Qt::Vertical) ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL);
2444+
flags |= (wheelEventDelta(we) & 0xffff)<<16;
24072445
return;
24082446
}
24092447
switch(evnt->button())
@@ -2416,7 +2454,7 @@ void OCVViewPort::icvmouseHandler(QMouseEvent* evnt, type_mouse_event category,
24162454
cv_event = tableMouseButtons[category][1];
24172455
flags |= CV_EVENT_FLAG_RBUTTON;
24182456
break;
2419-
case Qt::MidButton:
2457+
case Qt_MiddleButton:
24202458
cv_event = tableMouseButtons[category][2];
24212459
flags |= CV_EVENT_FLAG_MBUTTON;
24222460
break;
@@ -2772,7 +2810,7 @@ void DefaultViewPort::wheelEvent(QWheelEvent* evnt)
27722810
{
27732811
icvmouseEvent((QMouseEvent *)evnt, mouse_wheel);
27742812

2775-
scaleView(evnt->delta() / 240.0, evnt->pos());
2813+
scaleView(wheelEventDelta(evnt) / 240.0, wheelEventPos(evnt));
27762814
viewport()->update();
27772815

27782816
QWidget::wheelEvent(evnt);

0 commit comments

Comments
 (0)