Draw text in a QOpenGLWidget



  • Hi,

    I have a QOpenGLWidget with a context core profile (3.2). I want to draw a text but I didn't found how do that.
    Is it possible to blit a QImage of my text over the QOpenGLWidget or I should used OpenGL GUI ?

    Best regards,
    Robin


  • Lifetime Qt Champion

    Hi,

    AFAIK, you can use QPainter for that in your QOpenGLWidget subclass. It's mentioned here



  • Thank you for your replay. I made some test but QPainter seem not be usable with OpenGL Core Profile.
    This is a sample code test:

    #include <iostream>
    
    #include <GL/glew.h>
    
    #include <QApplication>
    #include <QOpenGLWidget>
    #include <QPainter>
    
    class OpenglWidget : public QOpenGLWidget {
    public:
    	void paintEvent(QPaintEvent *e) {
    		QPainter painter;
    		painter.begin(this);
    		paintGL();
    
    		painter.setPen(Qt::black);
    		painter.drawRect(100, 100, 300, 300);
    		printf("error: %d\n\n", glGetError()); //0x0502
    
    		painter.end();
    	}
    
    	void paintGL() {
    		glClearColor(0.3f, 0.0f, 0.0f, 1.0f);
    		glClear(GL_COLOR_BUFFER_BIT);
    	}
    
    	void initializeGL() {
    		glewExperimental = GL_TRUE;
    		if (glewInit() != GLEW_OK) {
    			exit(-1);
    		}
    		glGetError();
    
    		glViewport(0, 0, 600, 600);  
    	}
    };
    
    int main(int argc, char** argv) {
    	QApplication app(argc, argv);
    
    	QSurfaceFormat format;
    	format.setMajorVersion(3);
    	format.setMinorVersion(2);
    	format.setProfile(QSurfaceFormat::CoreProfile);
    
    	OpenglWidget opengl;
    	opengl.setFormat(format);
    	opengl.show();
    
    	return app.exec();
    }
    
    

    EDIT: painter.draw[...](...) generate an OpenGL error (0x0502)

    Regards ,
    Robin


  • Lifetime Qt Champion

    You shouldn't implement paintEvent, but use QPainter in paintGL



  • It's the same thing :(
    I try with and without the vao.

    #include <iostream>
    
    #include <GL/glew.h>
    
    #include <QApplication>
    #include <QOpenGLWidget>
    #include <QPainter>
    
    class OpenglWidget : public QOpenGLWidget {
    public:
    
    	void paintGL() {
    		glClearColor(0.3f, 0.0f, 0.0f, 1.0f);
    		glClear(GL_COLOR_BUFFER_BIT);
    
    		GLuint vao;
    		glGenVertexArrays(1, &vao);
    		glBindVertexArray(vao);
    
    		QPainter painter;
    		painter.begin(this);
    
    		painter.setPen(Qt::black);
    		painter.drawRect(100, 100, 300, 300);
    		printf("error: %d\n\n", glGetError()); //0x0502
    
    		painter.end();
    
    		glBindVertexArray(0);
    		glDeleteVertexArrays(1, &vao);
    	}
    
    	void initializeGL() {
    		glewExperimental = GL_TRUE;
    		if (glewInit() != GLEW_OK) {
    			exit(-1);
    		}
    		glGetError();
    
    		glViewport(0, 0, 600, 600);  
    	}
    };
    
    int main(int argc, char** argv) {
    	QApplication app(argc, argv);
    
    	QSurfaceFormat format;
    	format.setMajorVersion(3);
    	format.setMinorVersion(2);
    	format.setProfile(QSurfaceFormat::CoreProfile);
    
    	OpenglWidget opengl;
    	opengl.setFormat(format);
    	opengl.show();
    
    	return app.exec();
    }
    


  • Hi dig into the Qt code and I have found the problem. The shader used are in GLSL 120.

    The vertex shader:

        void setPosition(); 
        void main(void) 
        { 
            setPosition(); 
        }
    
        attribute highp   vec2      vertexCoordsArray; 
        attribute highp   vec3      pmvMatrix1; 
        attribute highp   vec3      pmvMatrix2; 
        attribute highp   vec3      pmvMatrix3; 
        void setPosition(void) 
        { 
            highp mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); 
            vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); 
            gl_Position = vec4(transformedPos.xy, 0.0, transformedPos.z); 
        }
    
    

    and the fragement shader:

    
        lowp vec4 srcPixel(); 
        void main() 
        { 
            gl_FragColor = srcPixel(); 
        }
    
        uniform   lowp    vec4      fragmentColor; 
        lowp vec4 srcPixel() 
        { 
            return fragmentColor; 
        }
    
    

    Is it possible to use shader in GLSL greather than 130 ?


  • Lifetime Qt Champion

    Yes, you have to ensure that you request the correct context (and that you get it)



  • I'm in the correct OpenGL context scope. The solution I choose is to convert a QImage into OpenGL texture and draw it in blend mode with a pass through shader.

    I think it really impossible to used a QPainter into a Core Profile context.

    Thank for you're time :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.