I just had the same problem. Basically, QPainter is designed to work with OpenGL ES 2.0. The 3.3 Core desktop OpenGL profile is not compatible with ES 2.0. If you use the 3.3 core OpenGL profile then you cannot use QPainter with it (at least not on OS X).
The issue is however apparently being worked on (https://codereview.qt-project.org/#/c/166202/). So perhaps next release this will be possible.
Currently, the way to get around this is to use QPainter to draw to a QImage and then draw that image as a texture in OGL.
Here is a proof of concept I just put together. In production code don't do this. Setup a shader + VAO (with VBO attached) for a quad. Then use glDrawArrays with an appropriate transform to put the texture on screen.
glClearColor(1.f,1.f,1.f,1.f);
glClear(GL_COLOR_BUFFER_BIT);
// Use QPainter to draw to a QImage and then display the QImage
QFont f{"Helvetica",18};
QStaticText txt{msg};
txt.prepare({},f);
auto dims = txt.size().toSize();
QImage buffer(dims,QImage::Format_ARGB32);
{
QPainter p(&buffer);
p.fillRect(QRect{{0,0},dims},QColor::fromRgb(255,255,255));
p.setPen(QColor::fromRgb(0,0,0));
p.setFont(f);
p.drawStaticText(0,0,txt);
}
// Blit texture to screen
// If you at all care about performance, don't do this!
// Create the texture once, store it and draw a quad on screen.
QOpenGLTexture texture(buffer,QOpenGLTexture::DontGenerateMipMaps);
GLuint fbo;
glGenFramebuffers(1,&fbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER,fbo);
texture.bind();
glFramebufferTexture2D(
GL_READ_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
texture.textureId(),
0);
glBlitFramebuffer(
0,
dims.height(),
dims.width(),
0,
width() / 2 - dims.width() / 2,
height() / 2 - dims.height() / 2,
width() / 2 + dims.width() / 2,
height() / 2 + dims.height() / 2,
GL_COLOR_BUFFER_BIT,
GL_LINEAR);
glDeleteFramebuffers(1,&fbo);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…