Setting up OpenGL in Qt
In this recipe, we will learn how to set up OpenGL in Qt 6.
How to do it…
Follow these steps to learn how to set up OpenGL in Qt:
- Create a new Qt Widgets Application by going to File | New Project. Uncheck the Generate form option to avoid generating the
mainwindow.ui
,mainwindow.h
, andmainwindow.cpp
files. - Open up your project file (
.pro
) and add the OpenGL module to your project by adding anopengl
keyword behindQT +=
; after that, runqmake
to reload the project modules:QT += core gui opengl
- You also need to add another line in your project file so that it will load both the OpenGL and OpenGL Utilities (GLU) libraries during startup. Without these two libraries, your program will not be able to run:
LIBS += -lopengl32 -lglu32
- Open up
main.cpp
and replacemainwindow.h
with theQtOpenGL
header:#include <QtOpenGL>
- Remove all of the code related to the
MainWindow
class from yourmain.cpp
file and replace it with the code...