From b2005ccaef83532582c1a57f19a60052047963a7 Mon Sep 17 00:00:00 2001 From: Patrick Whalen Date: Thu, 2 Dec 2021 11:10:30 -0800 Subject: [PATCH] Fix broken build for Qt6 with options: WITH_QT=ON and WITH_OPENGL=ON - QGLWidget changed to QOpenGLWidget in window_QT.h for Qt6 using typedef OpenCVQtWidgetBase for handling Qt version - Implement Qt6/OpenGL functionality in window_QT.cpp - Swap QGLWidget:: function calls for OpenCVQtWidgetBase:: function calls - QGLWidget::updateGL deprecated, swap to QOpenGLWidget::update for Qt6 - Add preprocessor definition to detect Qt6 -- HAVE_QT6 - Add OpenGLWidgets to qdeps list in highgui CMakeLists.txt - find_package CMake command added for locating Qt module OpenGLWidgets - Added check that Qt6::OpenGLWidgets component is found. Shut off Qt-openGL functionality if not found. --- cmake/OpenCVFindLibsGUI.cmake | 7 +++++++ modules/highgui/CMakeLists.txt | 5 +++++ modules/highgui/src/window_QT.cpp | 23 ++++++++++++++--------- modules/highgui/src/window_QT.h | 18 ++++++++++++++++-- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/cmake/OpenCVFindLibsGUI.cmake b/cmake/OpenCVFindLibsGUI.cmake index b33929e539..7224bddf90 100644 --- a/cmake/OpenCVFindLibsGUI.cmake +++ b/cmake/OpenCVFindLibsGUI.cmake @@ -47,6 +47,13 @@ if(WITH_QT) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS OpenGL QUIET) if(Qt${QT_VERSION_MAJOR}OpenGL_FOUND) set(QT_QTOPENGL_FOUND ON) # HAVE_QT_OPENGL is defined below + if(QT_VERSION_MAJOR GREATER 5) # QGL -> QOpenGL + find_package(Qt${QT_VERSION_MAJOR} COMPONENTS OpenGLWidgets QUIET) + if(NOT Qt${QT_VERSION_MAJOR}OpenGLWidgets_FOUND) + message(STATUS "Qt OpenGLWidgets component not found: turning off Qt OpenGL functionality") + set(QT_QTOPENGL_FOUND FALSE) + endif() + endif() endif() endif() endif() diff --git a/modules/highgui/CMakeLists.txt b/modules/highgui/CMakeLists.txt index 9177c1ba46..27cfbb3672 100644 --- a/modules/highgui/CMakeLists.txt +++ b/modules/highgui/CMakeLists.txt @@ -60,6 +60,7 @@ if(HAVE_QT) set(CMAKE_INCLUDE_CURRENT_DIR ON) if(QT_VERSION_MAJOR EQUAL 6) + add_definitions(-DHAVE_QT6) # QGLWidget deprecated for QT6, use this preprocessor to adjust window_QT.[h,cpp] QT6_ADD_RESOURCES(_RCC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.qrc) QT6_WRAP_CPP(_MOC_OUTFILES ${CMAKE_CURRENT_LIST_DIR}/src/window_QT.h) elseif(QT_VERSION_MAJOR EQUAL 5) @@ -78,6 +79,10 @@ if(HAVE_QT) set(qt_deps Core Gui Widgets Test Concurrent) if(HAVE_QT_OPENGL) add_definitions(-DHAVE_QT_OPENGL) + # QOpenGLWidget requires Qt6 package component OpenGLWidgets + if(QT_VERSION_MAJOR GREATER 5) + list(APPEND qt_deps OpenGLWidgets) + endif() list(APPEND qt_deps OpenGL) endif() diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index e3c831ab50..f6ba44b425 100644 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -3227,7 +3227,9 @@ void DefaultViewPort::setSize(QSize /*size_*/) #ifdef HAVE_QT_OPENGL -OpenGlViewPort::OpenGlViewPort(QWidget* _parent) : QGLWidget(_parent), OCVViewPort(), size(-1, -1) + +// QOpenGLWidget vs QGLWidget info: https://www.qt.io/blog/2014/09/10/qt-weekly-19-qopenglwidget +OpenGlViewPort::OpenGlViewPort(QWidget* _parent) : OpenCVQtWidgetBase(_parent), OCVViewPort(), size(-1, -1) { glDrawCallback = 0; glDrawData = 0; @@ -3281,7 +3283,11 @@ void OpenGlViewPort::makeCurrentOpenGlContext() void OpenGlViewPort::updateGl() { + #ifdef HAVE_QT6 + QOpenGLWidget::update(); + #else QGLWidget::updateGL(); + #endif } void OpenGlViewPort::initializeGL() @@ -3308,31 +3314,31 @@ void OpenGlViewPort::paintGL() void OpenGlViewPort::wheelEvent(QWheelEvent* evnt) { icvmouseEvent((QMouseEvent *)evnt, mouse_wheel); - QGLWidget::wheelEvent(evnt); + OpenCVQtWidgetBase::wheelEvent(evnt); } void OpenGlViewPort::mousePressEvent(QMouseEvent* evnt) { icvmouseEvent(evnt, mouse_down); - QGLWidget::mousePressEvent(evnt); + OpenCVQtWidgetBase::mousePressEvent(evnt); } void OpenGlViewPort::mouseReleaseEvent(QMouseEvent* evnt) { icvmouseEvent(evnt, mouse_up); - QGLWidget::mouseReleaseEvent(evnt); + OpenCVQtWidgetBase::mouseReleaseEvent(evnt); } void OpenGlViewPort::mouseDoubleClickEvent(QMouseEvent* evnt) { icvmouseEvent(evnt, mouse_dbclick); - QGLWidget::mouseDoubleClickEvent(evnt); + OpenCVQtWidgetBase::mouseDoubleClickEvent(evnt); } void OpenGlViewPort::mouseMoveEvent(QMouseEvent* evnt) { icvmouseEvent(evnt, mouse_move); - QGLWidget::mouseMoveEvent(evnt); + OpenCVQtWidgetBase::mouseMoveEvent(evnt); } @@ -3340,8 +3346,7 @@ QSize OpenGlViewPort::sizeHint() const { if (size.width() > 0 && size.height() > 0) return size; - - return QGLWidget::sizeHint(); + return OpenCVQtWidgetBase::sizeHint(); } void OpenGlViewPort::setSize(QSize size_) @@ -3350,6 +3355,6 @@ void OpenGlViewPort::setSize(QSize size_) updateGeometry(); } -#endif +#endif //HAVE_QT_OPENGL #endif // HAVE_QT diff --git a/modules/highgui/src/window_QT.h b/modules/highgui/src/window_QT.h index 398f3869f8..b93b9ba597 100644 --- a/modules/highgui/src/window_QT.h +++ b/modules/highgui/src/window_QT.h @@ -48,7 +48,14 @@ #if defined( HAVE_QT_OPENGL ) #include -#include + + // QGLWidget deprecated and no longer functions with Qt6, use QOpenGLWidget instead + #ifdef HAVE_QT6 + #include + #else + #include + #endif + #endif #include @@ -431,7 +438,14 @@ protected: #ifdef HAVE_QT_OPENGL -class OpenGlViewPort : public QGLWidget, public OCVViewPort +// Use QOpenGLWidget for Qt6 (QGLWidget is deprecated) +#ifdef HAVE_QT6 +typedef QOpenGLWidget OpenCVQtWidgetBase; +#else +typedef QGLWidget OpenCVQtWidgetBase; +#endif + +class OpenGlViewPort : public OpenCVQtWidgetBase, public OCVViewPort { public: explicit OpenGlViewPort(QWidget* parent);