Fix broken build for Qt6 with options: WITH_QT=ON and WITH_OPENGL=ON
authorPatrick Whalen <whalenpt@gmail.com>
Thu, 2 Dec 2021 19:10:30 +0000 (11:10 -0800)
committerPatrick Whalen <whalenpt@gmail.com>
Thu, 9 Dec 2021 21:22:20 +0000 (13:22 -0800)
- 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
modules/highgui/CMakeLists.txt
modules/highgui/src/window_QT.cpp
modules/highgui/src/window_QT.h

index b33929e..7224bdd 100644 (file)
@@ -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()
index 9177c1b..27cfbb3 100644 (file)
@@ -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()
 
index e3c831a..f6ba44b 100644 (file)
@@ -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
index 398f386..b93b9ba 100644 (file)
 
 #if defined( HAVE_QT_OPENGL )
 #include <QtOpenGL>
-#include <QGLWidget>
+
+  // QGLWidget deprecated and no longer functions with Qt6, use QOpenGLWidget instead
+  #ifdef HAVE_QT6
+  #include <QOpenGLWidget>
+  #else
+  #include <QGLWidget>
+  #endif
+
 #endif
 
 #include <QAbstractEventDispatcher>
@@ -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);