Colored Squares is a cross-platform Qt 6 demonstration app that illustrates how to integrate modern OpenGL (4.6 Core Profile) into a Qt Widgets application.
The program opens a main window, requests an OpenGL 4.6 core context, and uses a custom QOpenGLWidget subclass (View) to render a simple 2D scene — a ground strip and a pyramid of brightly coloured squares stacked in layers.
- Uses OpenGL 4.6 Core Profile (no deprecated fixed-pipeline functions)
- Draws a pyramid of colored squares using modern buffer/shader APIs
- Includes an orthographic projection that adapts to window resizing
- Automatically sets up Qt application icons for window, taskbar, and executable
- Built with CMake, GLM, and Qt 6
Before building, ensure the following dependencies are installed:
| Dependency | Minimum Version | Purpose |
|---|---|---|
| Qt 6 | 6.5 or newer | Core / Gui / Widgets / OpenGL / OpenGLWidgets |
| CMake | 3.21 or newer | Build system |
| GLM | Latest | Math library for transformations |
| MinGW | Qt-provided toolchain | Compiler |
- A unit square is defined as two triangles stored in a Vertex Buffer Object (VBO).
- A Vertex Array Object (VAO) is used to describe how vertex data is interpreted.
- A minimal GLSL shader program transforms each vertex using a Model–View–Projection (MVP) matrix and colors it with a uniform RGBA value.
- Multiple squares are drawn at different positions and with different colors to form a pyramid:
- Base row: four colored squares (red, green, blue, orange)
- Second row: three squares (violet, yellow, turquoise)
- Top row: one pink square
- A gray “ground strip” is drawn beneath the pyramid for depth reference.
- The application requests a Core Profile OpenGL 4.6 context using
QSurfaceFormat. - The context is initialized in
View::initializeGL()where shaders and buffers are created. - Each frame is rendered inside
paintGL(), which:- Clears the framebuffer with a dark gray-blue color.
- Binds the shader program and VAO.
- Uploads new transformation matrices and color uniforms.
- Issues draw calls with
glDrawArrays(GL_TRIANGLES)for each square.
- The projection is orthographic and recalculated on every window resize in
resizeGL()to maintain proper aspect ratio.
-
Vertex Shader (
#version 450 core):layout(location = 0) in vec3 position; uniform mat4 mvp; void main() { gl_Position = mvp * vec4(position, 1.0); }
-
Fragment Shader (
#version 450 core):uniform vec4 color; out vec4 FragColor; void main() { FragColor = color; }
-
These shaders are compiled, linked, and cached during initialization.
- The app includes two icons inside
Icon/:Icon/Icon.png— used for window and taskbar icons.Icon/icon.ico— embedded in the executable for Windows Explorer.
- Qt Resource System (
resources.qrc) embeds both files into the binary. - Windows Resource Script (
appicon.rc) ensures the.exedisplays the correct icon in file explorer. - Both resources are added in
CMakeLists.txtunder theadd_executable()target.
- CMake enables Qt’s automatic tools:
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON)
- GLM is fetched automatically via:
FetchContent_Declare( glm GIT_REPOSITORY https://github.com/g-truc/glm.git GIT_TAG master) FetchContent_MakeAvailable(glm) - In
add_executable, there is "WIN32" that will disable the default console in both IDE and .exe. If removed, it will re-enable it.
To ensure full runtime compatibility, you can also use:
windeployqt --compiler-runtime --no-translations build/Colored_squares.exeThis command bundles all required Qt and MinGW runtime libraries for distribution.
- Change colors inside
View::paintGL()where each square’s RGBA values are set. - Adjust the number and spacing of squares for different arrangements.
- Replace the icons in
Icon/to rebrand the application. - Modify
CMakeLists.txtif you add new source files or Qt modules.
This project is provided for educational and demonstration purposes.
You are free to reuse, modify, or extend it in your own Qt or OpenGL-based projects.