加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何使用QOpenGLWidget渲染文本

发布时间:2020-12-16 05:31:12 所属栏目:百科 来源:网络整理
导读:在旧版本的Qt中有 QGLWidget,有一个很好的函数叫做 renderText.现在我使用 QOpenGLWidget类,并且缺少渲染文本的功能. 有没有简单的方式来渲染文本使用QOpenGLWidget?我不想从头开始用OpenGL构建整个文本渲染 解决方法 我最终做了一个类似于@jaba写的解决方
在旧版本的Qt中有 QGLWidget,有一个很好的函数叫做 renderText.现在我使用 QOpenGLWidget类,并且缺少渲染文本的功能.

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

解决方法

我最终做了一个类似于@jaba写的解决方案.我也注意到一些图形损坏,除非我在方法的结尾调用了painter.end().
void MapCanvas::renderText(double x,double y,double z,const QString &str,const QFont & font = QFont()) {
    // Identify x and y locations to render text within widget
    int height = this->height();
    GLdouble textPosX = 0,textPosY = 0,textPosZ = 0;
    project(x,y,0f,&textPosX,&textPosY,&textPosZ);
    textPosY = height - textPosY; // y is inverted

    // Retrieve last OpenGL color to use as a font color
    GLdouble glColor[4];
    glGetDoublev(GL_CURRENT_COLOR,glColor);
    QColor fontColor = QColor(glColor[0],glColor[1],glColor[2],glColor[3]);

    // Render text
    QPainter painter(this);
    painter.setPen(fontColor);
    painter.setFont(font);
    painter.drawText(textPosX,textPosY,text);
    painter.end();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读