2

I'm trying to write a C++ application that uses QOpenGLWidget, but Qt Creator gives a "'QOpenGLWidget' file not found" error on this line:

#include <QOpenGLWidget>

The documentation says QOpenGLWidget was introduced in Qt 5.4, and I believe I am running Qt 5.12.1; qmake --version gives:

QMake version 3.1
Using Qt version 5.12.1 in /home/oreilly/Qt5.12.1/5.12.1/gcc_64/lib

Moreover, in the Qt Creator menu Tools -> Options... -> Kits also indicates Qt 5.12.1. And Qt Creator allows me to use #include <QOpenGLWindow> (also introduced in Qt 5.4 along with QOpenGLWidget), compile and link the OpenGL application without error.

What am I doing wrong? Where is QOpenGLWidget?

Yun
  • 3,056
  • 6
  • 9
  • 28
Tomasso
  • 693
  • 2
  • 9
  • 17

2 Answers2

5

QOpenGLWidget is not part of the main QtCore/QtWidgets libraries; rather it is part of a separate (QtOpenGL) library whose headers are in a directory that is not part of the Qt include-path by default.

Fortunately, it's easy to add QtOpenGL's headers to the include-path; just insert the following line into your .pro file:

QT += opengl 

... and then run qmake to update your Makefile/Project file, and you should be able to compile using the OpenGL classes.

Yun
  • 3,056
  • 6
  • 9
  • 28
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • My .pro file had this entry: "QT += quick". I assume this explains why qtcreator could find at least QOpenGLWindow. I added "QT += opengl" and now qtcreator finds QOpenGLWidget as well. – Tomasso Jun 20 '19 at 16:47
1

At least for Qt 6 for the QOpenGLWidget Class one wants:

qmake: QT += openglwidgets

Source: https://doc.qt.io/qt-6/qopenglwidget.html

For the QOpenGLWindow Class one wants:

qmake: QT += opengl

Source: https://doc.qt.io/qt-6/qopenglwindow.html