Hello world!

In this recipe, we will learn about the pipeline of OpenGL and how to render a simple shape to the window. We will continue from the example project used in the previous recipe.

How to do it…

  1. First of all, go to mainwindow.h and add the following headers at the top of the source code:
    #include <QSurfaceFormat>
    #include <QOpenGLFunctions>
    #include <QtOpenGL>
    #include <GL/glu.h>
  2. Next, declare two private variables in mainwindow.h:
    private:
      QOpenGLContext* context;
      QOpenGLFunctions* openGLFunctions;
  3. After that, move over to mainwindow.cpp and set the surface format to compatibility profile. We also set the OpenGL version to 2.1 and create the OpenGL context using the format we just declared. Then, use the context we just created to access ...

Get Qt5 C++ GUI Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.