我有一个QOpenGLWidget,其中我用QPainter绘制文本。我希望通过将小部件内容呈现为QOpenGLFrameBuffer并将框架缓冲区转换为QImage来实现快照特性。
不幸的是,如果我将字体的点大小设置得太高(> 46),则快照中的文本显示为黑条,而在小部件中则正确显示。请参见下面的示例快照,其中行上方的块应该是字体大小> 46的文本。

下面是呈现图像的简化代码(它应该可以工作,因为它在QOpenGLWidget中正确显示):
void renderSomething(const int x, const int y, const QString & str, const int fontSize) {
// 1. draw the line
// (calculate min_x, max_x, y and z values)
glLineWidth(3);
glColor3f(0., 0., 0.);
glBegin(GL_LINES);
glVertex3f(min_x, y, z);
glVertex3f(max_x, y, z);
glEnd();
// 2. draw the text
GLint gl_viewport[4];
glGetIntegerv(GL_VIEWPORT, gl_viewport);
backup_gl_state();
QOpenGLPaintDevice paintDevice(gl_viewport[2], gl_viewport[3]);
QPainter painter(&paintDevice);
painter.setFont(QFont(painter.font().family(), fontSize);
painter.setPen(Qt::black);
painter.drawText(x, y, str);
painter.end();
restore_gl_state();
}下面是存储快照的代码:
void Viewport::takeSnapshot(const QString & path, const int size) {
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, size, size); // since size varies I want the text to scale with it
QOpenGLFramebufferObject fbo(size, size, QOpenGLFramebufferObject::Depth);
fbo.bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderer.renderSomething(100, 100, "Test", 50); // Here is the render call!
QImage fboImage(fbo.toImage());
QImage image(fboImage.constBits(), fboImage.width(), fboImage.height(), QImage::Format_RGB32);
image.save(path);
glPopAttrib();
fbo.bindDefault();
fbo.release();
}编辑
通过使用QPainter直接在QImage上绘制文本而不是绘制到FBO中,我找到了一个解决办法。没有黑色的酒吧了,但它使我的代码变得复杂,我很乐意找到一个真正的解决方案.
发布于 2015-07-25 19:26:05
解决方案很简单:重读关于QOpenGLFrameBufferObject的文档,其中写道:
如果希望QOpenGLFrameBufferObject正确呈现,则使用CombinedDepthStencil附件创建QPainter实例。
我只附加了一个深度缓冲器。正确的初始化将是:
QOpenGLFramebufferObject fbo(size, size, QOpenGLFramebufferObject::CombinedDepthStencil);https://stackoverflow.com/questions/31456763
复制相似问题