I tried the QOpenGLWidget example described here: https://stackoverflow.com/a/31524956/4564882
but I get only a black widget. The code is exactly the same. this the code
The problem was because I use Qt5 binaries built with the default configuration. The default in Qt 5.5 is "dynamic" GL -- both ANGLE (ES2)
ANGLE ((Almost Native Graphics Layer Engine) is an open source project by Google. Its aim is to map OpenGL ES 2.0 API calls to DirectX 9 API.)
and Desktop backends (Desktop OpenGL)are built, the decision on which one to use is taken at runtime.
The problem is that ANGLE only supports OpenGL>3.x, so the first code that I test is deprecated and not supported by ANGLE. The second is supported, that's why it worked.
So, I rebuild Qt to target Desktop OpenGL only to support my deprecated code, using:
configure -debug-and-release -opensource -opengl desktop -platform win32-msvc2015
and then run nmake
, link my application to the new binaries, and my code works well!
In my case, my laptop uses NVIDIA external graphics card. So I went to NVIDIA Control Panel -> Manage 3D Settings -> Program Settings, and then selected "high-performance" for the .EXE file. This worked.
I had a black screen on desktop. I solved the problem by adding this line of code:
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
For example, put it here:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}