在旧版本的Qt中有QGLWidget,有一个很好的函数renderText。现在我使用的是QOpenGLWidget类,而渲染文本的功能缺失了。
有没有一个简单的方法来渲染文本使用QOpenGLWidget?我不喜欢从头开始用OpenGL构建整个文本渲染.
在旧版本的Qt中有QGLWidget,有一个很好的函数renderText。现在我使用的是QOpenGLWidget类,而渲染文本的功能缺失了。
有没有一个简单的方法来渲染文本使用QOpenGLWidget?我不喜欢从头开始用OpenGL构建整个文本渲染.
6条答案
按热度按时间mbzjlibv1#
我最终做了一个类似于@jaba写的解决方案。我还注意到一些图形损坏,除非我在方法的末尾调用painter.end()。
dced5bon2#
您可以基于旧的Qt源代码自行实现此功能。
在从QOpenGLWidget继承的OpenGL widget类(在本例中是GLBox)中,必须实现以下方法:
renderText:
项目:
最后transformPoint:
如果您需要renderText(),因为您必须将Qt4应用程序移植到Qt5,请确保将此处提供的函数的签名更改为
你不用再担心这个了
ttisahbt3#
如果你想绘制2D文本,可以使用QPainter::drawText()。有关在QOpenGLWidget上使用QPainter的信息,请参阅此处。有关在QOpenGLWidgets上对文本渲染使用抗锯齿的信息,请参见here。
如果你想绘制2.5D文本(2D文本随着3D场景移动),滚动你自己的类并不“太难”。使用QFont和QFontMetricsF为您的字体字形构建纹理,为每个字符串构建一些四边形到VBO中,并为字符串中的字形绘制适当的四边形。
tjvv9vkg4#
QOpenGLWidget上的QPainter::drawText依赖于GL_UNPACK_ALIGNMENT设置为4,否则字符看起来会损坏/混乱。
如果您的应用程序中存在此问题,请确保调用
在画出文字之前。(更多信息请参见https://bugreports.qt.io/browse/QTBUG-65496)
eagi6jfj5#
对于未来的任何人来这里与类似的问题参考-我有一个类似的问题,如Urs's answer中所述:当使用
drawText
在OpenGL小部件上显示文本时,丢失了一些字母,有时是整个单词。然而,提到的glPixelStorei
调用并没有改变任何事情。当我阅读QOpenGLWidget绘图文档时,我注意到了与
QOpenGLFunction
相关的东西--在我们的例子中,确实受影响的小部件还没有使用QOpenGLFunction,但显然直接调用了OpenGL函数--这导致了奇怪的文本显示。从
QOpenGLFunction
派生并在initializeGL
中调用initializeOpenGLFunctions
似乎已经解决了这些问题。编辑:没有运气-我的问题似乎是一个间歇性的,只是偶尔出现(它不是重现每次我运行程序)。我想接下来我会研究一下Qt OpenGL上下文调试。
pexxcrt26#
我有这个问题时,我需要绘制轴和相应的刻度。在Qt 4中,我可以使用以下代码:
然而,当我尝试将代码迁移到Qt 6时,再也没有
renderText
了。在网上搜索并调查了renderText
的源代码后,我终于找到了解决方案,需要保存/加载gl状态:下面是qt_保存_gl_state,renderText,qt_restore_gl_state的def:
项目的src代码可以在这里找到QtSignalProcessing