cerkiewny
cerkiewny

Reputation: 2851

QOpenGLWidget QT requesting context

I have been using the shader file with QOpenGLWindow and had the version 330 shader, everything was working fine. However wanted to switch for a Widget to be able to dock it within my main window of application. I have been trying to get the right context ever since.

I am able to create the right one using the QGLWidget (which I have seen is deprecated):

 QGLFormat format;
 format.setProfile(QGLFormat::CoreProfile);
 format.setVersion(3,3);                                                                                                                                                                               
 QGLFormat::setDefaultFormat(format);

When I am doing exactly the same thing with a QOpenGLWidget however: I get

QOpenGLShader::compile(Vertex): ERROR: 0:1: '' :  version '330' is not supported

Which is to some extand reasonable in a way that the management of the QGLFormat is not responsible for a new class.

However after switching to new classes and setting the core profile with:

  QSurfaceFormat format;
  format.setProfile(QSurfaceFormat::CoreProfile);
  format.setVersion(3,3);
  QSurfaceFormat::setDefaultFormat(format);

I am getting a crush:

0   libobjc.A.dylib                 0x00007fff89f9a0dd objc_msgSend + 29
1   libqcocoa.dylib                 0x000000011085e3a3 QCocoaIntegration::createPlatformOpenGLContext(QOpenGLContext*) const + 83
2   org.qt-project.QtGui            0x000000010d2c954a QOpenGLContext::create() + 74
3   org.qt-project.QtWidgets        0x000000010cc35a6d QOpenGLWidgetPrivate::initialize() + 141
4   org.qt-project.QtWidgets        0x000000010cc36e58 QOpenGLWidget::event(QEvent*) + 232
5   org.qt-project.QtWidgets        0x000000010cbd632b QApplicationPrivate::notify_helper(QObject*, QEvent*) + 251
6   org.qt-project.QtWidgets        0x000000010cbd9648 QApplication::notify(QObject*, QEvent*) + 8136
7   org.qt-project.QtCore           0x000000010da59d83 QCoreApplication::notifyInternal(QObject*, QEvent*) + 115
8   org.qt-project.QtWidgets        0x000000010cc18879 sendWindowChangeToTextureChildrenRecursively(QWidget*) + 73
9   org.qt-project.QtWidgets        0x000000010cc03339 QWidget::setParent(QWidget*, QFlags<Qt::WindowType>) + 2217
10  org.qt-project.QtWidgets        0x000000010cbf4569 QLayout::addChildWidget(QWidget*) + 201
11  org.qt-project.QtWidgets        0x000000010cd1e740 QMainWindowLayout::setCentralWidget(QWidget*) + 32
12  ultrahaptics.Optimus_gui        0x000000010cb27f74 MainWindow::MainWindow() + 580
13  ultrahaptics.Optimus_gui        0x000000010cb2396b main + 59

I am using Os X on the macbook pro. If anyone knows the workaround / reason comments would be welcome.

Upvotes: 2

Views: 962

Answers (2)

Nai
Nai

Reputation: 71

I've read somewhere that if you develop your OpenGL program on OS X (no matter what platform you do use: Qt Creator , XCode..) , you will be able to use only one version of OpenGL either 2.1 or 4.1, and the default version usually is 2.1 So, By using setDefaultFormat we force the system to initialize OpenGL 4.1 for us.

I'v uploaded an example of how to use OpenGL 4.X on Mac.

QOpenGLWidget_OpenGL4.1

enjoy

Nai

Upvotes: 1

Nai
Nai

Reputation: 71

You should move the code bellow into the main.cpp file:

 QGLFormat format;
 format.setProfile(QGLFormat::CoreProfile);
 format.setVersion(3,3);                                                                                                                                                                               
 QGLFormat::setDefaultFormat(format);

Your main.cpp should be looks:

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

        QSurfaceFormat format;
//        format.setDepthBufferSize(24);
    //    format.setStencilBufferSize(8);

        format.setMajorVersion( 3 );
        format.setMinorVersion( 3 );
        format.setProfile(QSurfaceFormat::CoreProfile);
        QSurfaceFormat::setDefaultFormat(format);

    Dialog w;
    w.show();

    return a.exec();
}

you might need to add QSurfaceFormat header file to your main.cpp!

I hope this helps.

cheers, Nai

Upvotes: 0

Related Questions