c++ - 如何使用 QOpenGLWidget 渲染文本

标签 c++ qt

在旧版本的 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();
}

关于c++ - 如何使用 QOpenGLWidget 渲染文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216001/

相关文章:

模板对象的 C++ 继承(G++ 编译器)

c++ - CORBA : How to pass a QT object as a parameter

qt - 使用 QDataStream 读取文件 : Is it faster with a buffer? 怎么做?

c++ - GTest 类型测试 - 使用

c++ - 有人能解释一下这段代码是如何执行的吗?

c++ - 是否有一种简单的方法/算法来匹配 2 个 2D 点云?

c++ - 在 C++ 中应该以什么顺序释放内存?

c++ - Qt,链接器错误: can not find IGL

c++ - Qt creator 添加外部库(还是: Cannot open include file: 'GL/glew.h' )

python - PyQt - QLabel 继承