0

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 associated to the QopenGLWidget:

OGLWidget::OGLWidget(QWidget *parent)
: QOpenGLWidget(parent)
 {

 }

 OGLWidget::~OGLWidget()
{

}

 void OGLWidget::initializeGL()
 {
   glClearColor(0,0,0,1);
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHT0);
   glEnable(GL_LIGHTING);
   glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
   glEnable(GL_COLOR_MATERIAL);
}

 void OGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(-0.5, -0.5, 0);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f( 0.5, -0.5, 0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f( 0.0,  0.5, 0);
glEnd();
 }

 void OGLWidget::resizeGL(int w, int h)
 {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)w/h, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
  }

I tried the example here: https://doc.qt.io/archives/qt-5.3/qtopengl-2dpainting-example.html. It works fine (trying the both base class: QGLWidget and QOpenGLWidget. this is the code associated to the Widget:

  GLWidget::GLWidget(Helper *helper, QWidget *parent)
  : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), helper(helper)
 {
 elapsed = 0;
setFixedSize(200, 200);
setAutoFillBackground(false);
}

void GLWidget::animate()
{
 elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;
 repaint();
 }

void GLWidget::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
helper->paint(&painter, event, elapsed);
painter.end();
}

I use Qt 5.5.1 binairies built on my machine. I let the Build Configuration by default, so it is based on Qt ANGLE not Desktop OpenGL. What is the problem of such a behaviour?

Community
  • 1
  • 1
ProEns08
  • 1,856
  • 2
  • 22
  • 38
  • Does the color change when you change the `glClearColor`? – BDL Dec 20 '15 at 13:34
  • @BDL: yes, if I specify a color ( for example red), it will show all the widget with the chosen color ( red ). But it didn't shows me the wanted triangle. – ProEns08 Dec 20 '15 at 13:55
  • Your triangle isn't drawing because you're using OpenGL 2.x code which is deprecated. Qt gives you access to later OpenGL functions which you should consider using. What dependencies are you using for the OpenGL functions? – Poriferous Dec 21 '15 at 15:59
  • Thank you! I am using as include: #include #include #include with Qt5 (the same code works if I links with Qt 4.8). and I am linking with the libraries: Qt5OpenGLd.lib, opengl32.lib, glu32.lib. How could I use this deprecated code because I am migrating the code from Qt 4.8 to Qt 5.5 but I should Keep the OpenGL widget as is, it is too long to be modified. – ProEns08 Dec 22 '15 at 08:13
  • I am using Qt5 with linking to QtANGLE header files. How can I switch to the Desktop OpenGL. – ProEns08 Dec 22 '15 at 10:38

3 Answers3

1

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Bin Bin
  • 11
  • 1
0

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!

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
ProEns08
  • 1,856
  • 2
  • 22
  • 38
0

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();
}
8Observer8
  • 868
  • 10
  • 17