How to do it…

Follow these steps to learn how to set up OpenGL in Qt:

  1. Create a new Qt Widgets Application by going to File | New File or Project.
  2. We will remove the mainwindow.ui file because we are not going to use it in this example. Right-click on the mainwindow.ui file and select Remove File from the dropdown menu. Then, a message box will appear and ask for your confirmation. Tick Delete file permanently and press the OK button.
  3. Repeat Step 2 for both mainwindow.h and mainwindow.cpp as well. We don't need these two files in this project either.
  4. Open up your project file (.pro) and add the OpenGL module to your project by adding an opengl keyword behind QT +=, like so:
QT += core gui opengl
  1. You also need to add another line in your project file so that it will load both the OpenGL and GLu (OpenGL Utilities) libraries during startup. Without these two libraries, your program will not be able to run:
LIBS += -lopengl32 -lglu32

  1. Open up main.cpp and replace mainwindow.h with the QtOpenGL header:
#include <QtOpenGL>
  1. Remove all of the code related to the MainWindow class from your main.cpp file and replace it with the code that's highlighted in the following snippet:
#include <QApplication>
#include <QtOpenGL>

int main(int argc, char *argv[]) {
QApplication app(argc, argv);

QOpenGLWindow window;
window.setTitle("Hello World!");
window.resize(640, 480);
window.show();

return app.exec();
}
  1. If you compile and run the project now, you will see an empty window with a black background. Don't worry about it—your program is now running on OpenGL:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset