Qt:在QOpenGLWidget中的文本呈现 [英] Qt: text rendering in QOpenGLWidget

查看:638
本文介绍了Qt:在QOpenGLWidget中的文本呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在QOpenGLWidget上绘制文本标签。我试图使用QPainter这个任务,但没有成功 - 文本看起来丑陋和非抗锯齿。它也看起来丑陋的Qt OpenGL / 2dpainting样本。然而,文本渲染在使用OpenGL后端的QML控件中显着更好。这里 http://博客.qt.io / blog / 2011/07/15 / text-rendering-in-the-qml-scene-graph / 我发现了一种用于QML的技术。有没有办法使用这种技术来绘制文本在QOpenGLWidget?

I want to draw text labels on QOpenGLWidget. I tried to use QPainter for this task, but had no success - text looks ugly and non antialiased. It is also looks ugly in Qt OpenGL/2dpainting samples. However text rendering is significantly better in QML controls which also use OpenGL backend. Here http://blog.qt.io/blog/2011/07/15/text-rendering-in-the-qml-scene-graph/ I have found a technique that is used in QML. Is there a way to use that technique to draw text in QOpenGLWidget?

PS:可能是正确的方法是将所有我的场景嵌入QSGSceneGraph的QQuickWidget? / p>

PS: May be the right way to go is to embed all my scene inside QSGSceneGraph of QQuickWidget?

推荐答案

确保您创建的QGL / QopenGLWidget的表面格式支持多重采样的抗锯齿:

Make sure you create your QGL/QopenGLWidget with a surface format that supports multisampling for antialiasing:

QSurfaceFormat format = QSurfaceFormat::defaultFormat();
format.setSamples(4);
QOpenGLWidget * glWidget = new QOpenGLWidget;
glWidget->setFormat(format);

然后,当在该小部件上使用QPainter时,请确保打开抗锯齿:

Then, when using a QPainter on that widget, make sure you turn on antialiasing:

QPainter painter(paintDevice);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

然后绘制您的文字。

这篇关于Qt:在QOpenGLWidget中的文本呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆