Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QOpenGLFunctions glGetString(GL_EXTENSIONS) is null

QOpenGLFunctions glGetString(GL_EXTENSIONS) is null

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5openglqopenglfunctioncontextglextensions
22 Posts 4 Posters 13.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D deleted451

    @kshegunov Hello.

    Unfortunately, still does not want to work.

    QOpenGLFunctions created with non-current context
    Ошибка сегментирования (core dumped)
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <Qt>
    #include <QCoreApplication>
    #include <QGuiApplication>
    #include <QObject>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glext.h>
    #include <QtOpenGL/QtOpenGL>
    #include <QtOpenGL/QGL>
    #include <QtOpenGL/QGLFunctions>
    #include <QOpenGLFunctions>
    #include <QOpenGLContext>
    #include <QWidget>
    #include <QOpenGLWidget>
    int main(int argc, char ** argv)
    {
        /*QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL,true);
        void initializeGL();
        void initializeOpenGLFunctions();
        const GLubyte* gl_version1 = glGetString(GL_VERSION);
        const GLubyte* gl_extensions1 = glGetString(GL_EXTENSIONS);
        printf("GL_VERSION: %s\nGL_EXTENSIONS: %s\n",gl_version1,gl_extensions1);*/
    
        QGuiApplication app(argc, argv);
        QOpenGLWindow window;
        window.setSurfaceType(QSurface::OpenGLSurface);
        QSurfaceFormat format;
        if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL)
        {
            format.setVersion(1,2);
            format.setProfile(QSurfaceFormat::CoreProfile);
        }
        format.setDepthBufferSize(24);
        format.setSamples(4);
        format.setStencilBufferSize(8);
        window.setFormat(format);
        window.create();
        QOpenGLContext context;
        context.setFormat(format);
        context.setScreen(QGuiApplication::primaryScreen());
        context.create();
        QOpenGLFunctions* gl = context.functions();
        const GLubyte* gl_version2 = gl->glGetString(GL_VERSION);
        const GLubyte* gl_extensions2 = gl->glGetString(GL_EXTENSIONS);
        printf("GL_VERSION from Context: %s\nGL_EXTENSIONS from Context: %s\n",gl_version2,gl_extensions2);
    
        return 0;
    }
    
    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #12

    @Ilya-Indigo
    Hm, try calling QOpenGLContext::makeCurrent then. From your last code:

    // ...
    context.create();
    context.makeCurrent(&window);  //< Try inserting this line after creating the context and before retrieving the functions.
    QOpenGLFunctions* gl = context.functions();
    // ...
    

    Read and abide by the Qt Code of Conduct

    D 1 Reply Last reply
    1
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #13

      All you need is an application object, surface and a context. Here's a minimal example:

      #include <QApplication>
      #include <QOffscreenSurface>
      #include <QOpenGLContext>
      #include <QOpenGLFunctions>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QOffscreenSurface surf;
          surf.create();
      
          QOpenGLContext ctx;
          ctx.create();
          ctx.makeCurrent(&surf);
      
          qDebug () << (const char*)ctx.functions()->glGetString(GL_VERSION);
          qDebug () << (const char*)ctx.functions()->glGetString(GL_EXTENSIONS);
      }
      
      1 Reply Last reply
      3
      • kshegunovK kshegunov

        @Ilya-Indigo
        Hm, try calling QOpenGLContext::makeCurrent then. From your last code:

        // ...
        context.create();
        context.makeCurrent(&window);  //< Try inserting this line after creating the context and before retrieving the functions.
        QOpenGLFunctions* gl = context.functions();
        // ...
        
        D Offline
        D Offline
        deleted451
        wrote on last edited by
        #14

        @kshegunov
        Thank you very much for all your help! :-)
        After adding this line, the code worked.
        Although it is not the best, but it is working. :-)

        @Chris-Kawa
        Thank you for working and best example, this is what I needed. :-)

        test.pro

        QT += opengl
        
        CONFIG += c++11
        
        TARGET = test
        CONFIG += console
        CONFIG -= app_bundle
        
        TEMPLATE = app
        
        SOURCES += main.cpp
        

        main.cpp

        #include <QtOpenGL>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QOffscreenSurface surf;
            surf.create();
        
            QOpenGLContext ctx;
            ctx.create();
            ctx.makeCurrent(&surf);
        
            const GLubyte* extensions = ctx.functions()->glGetString(GL_EXTENSIONS);
            std::string ext = "\t";
            int l = strlen((char*)extensions);
        
            for (int i=0;i<l;i++)
            {
                if (extensions[i] != ' ')
                    ext += extensions[i];
                else
                    ext += "\n\t";
            }
        
            printf("OpenGL: %s %s %s\n\n%s\n", ctx.functions()->glGetString(GL_VENDOR), ctx.functions()->glGetString(GL_RENDERER), ctx.functions()->glGetString(GL_VERSION), ext.c_str());
        
            return 0;
        }
        

        I wonder whether it was possible somehow to replace the spaces (in my code ' ' -> "\n\t", but would be satisfied with a simple ' ' -> '\n') at the line breaks more optimally than I did?
        replace()/find() doing a little bit more, and with replace_if I did not understand.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #15

          Well, since you're using Qt anyway why not go all the way:

          QString exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(" ", "\n\t");
          

          or, if you prefer a list instead of one string:

          QStringList exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).split(' ');
          
          D 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            Well, since you're using Qt anyway why not go all the way:

            QString exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(" ", "\n\t");
            

            or, if you prefer a list instead of one string:

            QStringList exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).split(' ');
            
            D Offline
            D Offline
            deleted451
            wrote on last edited by deleted451
            #16

            @Chris-Kawa said:

            Well, since you're using Qt anyway why not go all the way:

            I'm still starting to learn Qt and still do not know about all of its features.

            QString exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(" ", "\n\t");
            

            Oh great, what you need.

            const char* ext = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(' ', "\n\t").toStdString().c_str();
            
            or, if you prefer a list instead of one string:
            ```cpp
            QStringList exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).split(' ');
            

            It reminded me explode(' ') / foreach() in PHP or split(' ').join('\n\t') in JS.
            In PHP foreach through the easiest process, but there is no native JS recursive replacement, this had to be split into an array.
            But in Qt would like to do is normal, especially if there is native support this. :-)
            Although it is not surprised that there is Qt and foreach(), which works the same way as in PHP. :-)

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #17

              @Ilya-Indigo said:

              const char* ext = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(' ', "\n\t").toStdString().c_str();

              Don't do that! The string generated by toStdString is a temporary destroyed at the end of this line. The resulting pointer ext points to garbage memory this way!

              Do it like this:

              QByteArray ext = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(' ', "\n\t").toUtf8();
              
              someFunctionthatExpectsConstCharStar(ext.constData());
              

              or, if you want std::string, like this:

              std::string ext = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(' ', "\n\t").toStdString();
              
              someFunctionthatExpectsConstCharStar(ext.c_str());
              

              you can store that pointer (almost always bad idea), as long as the original string lives:

              std::string ext = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(' ', "\n\t").toStdString();
              const char* ext_c_str = ext.c_str();
              someFunctionthatExpectsConstCharStar(ext_c_str);
              

              but don't ever store a pointer to a temporary like you did.

              1 Reply Last reply
              1
              • D Offline
                D Offline
                deleted451
                wrote on last edited by deleted451
                #18

                @Chris-Kawa
                Thank you for your comment and clarification. :-)
                If I correctly understood and grasped the essence, the "const char *" should be used only in the case of constant declarations.

                const char* var_name = "Constant string";
                

                And if it is not constant, and any expression, it is better to use std :: string?

                std::string var_name = a+b+c.some_string_method() ... etc ... ;
                
                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #19

                  It's not that general. It's better to understand what's the difference.

                  std:string, QString, CString and any other kind of library strings are containers. They store data and manage its lifetime. They are responsible for it. The data goes away when the object goes away. Because they do this bookkeeping they are a little heavier to initialize and destroy.
                  Pointer is just a finger pointing to a place in memory. It carries no notion of ownership or lifetime. The data it points to has no ties to it. It doesn't go away when data goes away. The data doesn't go away when the pointer does. Because of this pointers are ultra-light but also a lot more dangerous.

                  Pointers to characters are generally good for api-boundary situations. When you have a container type string it usually gives you a raw pointer to its internal data so you can "look" at it from some functions.

                  A rule of thumb is that you should avoid conversions between container types whenever possible, as they allocate memory, copy stuff around and are generally heavy. If you're using STL stick to std::string. If you're using Qt stick to QString.

                  Pointers are good and cheap when used for their purpose - pointing at things. They should not be used to manage lifetimes.
                  For example const char* foo = "bar bazz"; just points at some static data in memory. The data is not going away so its safe to keep this pointer around too. On the other hand const char* foo = someFuncThatGivesMeAPointer()is more vague. Who is responsible for that memory? should you call delete foo at some point or is the memory managed by someone? If it is then when is it released and can you keep the foo pointer and use without worrying that someone releases the memory from under it?

                  Qt uses a lot of pointers, but it has strict policies about the lifetimes of stuff it gives you pointers to. These are not, however, rules enforced by a compiler, only a general programmer-documentation contract.

                  D 1 Reply Last reply
                  1
                  • Chris KawaC Chris Kawa

                    It's not that general. It's better to understand what's the difference.

                    std:string, QString, CString and any other kind of library strings are containers. They store data and manage its lifetime. They are responsible for it. The data goes away when the object goes away. Because they do this bookkeeping they are a little heavier to initialize and destroy.
                    Pointer is just a finger pointing to a place in memory. It carries no notion of ownership or lifetime. The data it points to has no ties to it. It doesn't go away when data goes away. The data doesn't go away when the pointer does. Because of this pointers are ultra-light but also a lot more dangerous.

                    Pointers to characters are generally good for api-boundary situations. When you have a container type string it usually gives you a raw pointer to its internal data so you can "look" at it from some functions.

                    A rule of thumb is that you should avoid conversions between container types whenever possible, as they allocate memory, copy stuff around and are generally heavy. If you're using STL stick to std::string. If you're using Qt stick to QString.

                    Pointers are good and cheap when used for their purpose - pointing at things. They should not be used to manage lifetimes.
                    For example const char* foo = "bar bazz"; just points at some static data in memory. The data is not going away so its safe to keep this pointer around too. On the other hand const char* foo = someFuncThatGivesMeAPointer()is more vague. Who is responsible for that memory? should you call delete foo at some point or is the memory managed by someone? If it is then when is it released and can you keep the foo pointer and use without worrying that someone releases the memory from under it?

                    Qt uses a lot of pointers, but it has strict policies about the lifetimes of stuff it gives you pointers to. These are not, however, rules enforced by a compiler, only a general programmer-documentation contract.

                    D Offline
                    D Offline
                    deleted451
                    wrote on last edited by
                    #20

                    @Chris-Kawa
                    Thanks for your reply. I am now much clearer.

                    1 Reply Last reply
                    0
                    • 8Observer88 Offline
                      8Observer88 Offline
                      8Observer8
                      wrote on last edited by
                      #21

                      @Chris-Kawa said in QOpenGLFunctions glGetString(GL_EXTENSIONS) is null:

                      QString exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(" ", "\n\t");

                      I have a same question. I try to show OpenGL version but I get empty string:

                          QString version = QString::fromLatin1((const char*)glGetString(GL_VERSION));
                          qDebug() << version;
                      
                      8Observer88 1 Reply Last reply
                      0
                      • 8Observer88 8Observer8

                        @Chris-Kawa said in QOpenGLFunctions glGetString(GL_EXTENSIONS) is null:

                        QString exts = QString::fromLatin1((const char*)ctx.functions()->glGetString(GL_EXTENSIONS)).replace(" ", "\n\t");

                        I have a same question. I try to show OpenGL version but I get empty string:

                            QString version = QString::fromLatin1((const char*)glGetString(GL_VERSION));
                            qDebug() << version;
                        
                        8Observer88 Offline
                        8Observer88 Offline
                        8Observer8
                        wrote on last edited by 8Observer8
                        #22

                        @8Observer8 said in QOpenGLFunctions glGetString(GL_EXTENSIONS) is null:

                        I have a same question. I try to show OpenGL version but I get empty string:

                        I solved the problem. I activated a second video card on my laptop from code in main.cpp:

                        #ifdef _WIN32
                        #include <windows.h>
                        extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
                        extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
                        #endif
                        
                        #include "Widget.h"
                        
                        #include <QApplication>
                        
                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                            Widget w;
                            w.show();
                            return a.exec();
                        }
                        

                        It works too (without QString::fromLatin1):

                        qDebug() << (const char*)glGetString(GL_VERSION);
                        
                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved