c – 如何使用QOpenGLWidget渲染文本

前端之家收集整理的这篇文章主要介绍了c – 如何使用QOpenGLWidget渲染文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在旧版本的Qt中有 QGLWidget,有一个很好的函数叫做 renderText.现在我使用 QOpenGLWidget类,并且缺少渲染文本的功能.

有没有简单的方式来渲染文本使用QOpenGLWidget?我不想从头开始用OpenGL构建整个文本渲染

解决方法

我最终做了一个类似于@jaba写的解决方案.我也注意到一些图形损坏,除非我在方法的结尾调用了painter.end().
  1. void MapCanvas::renderText(double x,double y,double z,const QString &str,const QFont & font = QFont()) {
  2. // Identify x and y locations to render text within widget
  3. int height = this->height();
  4. GLdouble textPosX = 0,textPosY = 0,textPosZ = 0;
  5. project(x,y,0f,&textPosX,&textPosY,&textPosZ);
  6. textPosY = height - textPosY; // y is inverted
  7.  
  8. // Retrieve last OpenGL color to use as a font color
  9. GLdouble glColor[4];
  10. glGetDoublev(GL_CURRENT_COLOR,glColor);
  11. QColor fontColor = QColor(glColor[0],glColor[1],glColor[2],glColor[3]);
  12.  
  13. // Render text
  14. QPainter painter(this);
  15. painter.setPen(fontColor);
  16. painter.setFont(font);
  17. painter.drawText(textPosX,textPosY,text);
  18. painter.end();
  19. }

猜你在找的C&C++相关文章