Important: Please read the Qt Code of Conduct -

How to draw 2d text in a QGLWidget

  • Hi I am trying to make a QGLWidget for a 3d viewer,
    At the very basic level my class looks like this.

    @DemoEntityManager::DemoEntityManager(QWidget* const parent)
    :QGLWidget (parent)
    // Create the main Camera
    m_camera = new DemoCamera();

    // initialized the physics world for the new scene
    Cleanup ();

    // Start rendering after the system is set properly

    void DemoEntityManager::paintGL()
    glClearColor (0.5f, 0.5f, 0.5f, 0.0f );
    renderText (4, 4, "This is a test", m_font);

    However no mater what I do I can no get the ext to show on the screen, what I am doin wrong.

    I windows the way I do this is by creation and array display List wih on for diaply lest mtaching ever font, windo come with spacil funtion to do that,
    At typical implemantaion will look like this

    @GLuint GLContext::BuildFont()
    HFONT font = CreateFont(
    20, // nHeight
    0, // nWidth
    0, // nEscapement
    0, // nOrientation
    FW_BOLD, // nWeight
    FALSE, // bItalic
    FALSE, // bUnderline
    0, // cStrikeOut
    ANSI_CHARSET, // nCharSet
    OUT_DEFAULT_PRECIS, // nOutPrecision
    CLIP_DEFAULT_PRECIS, // nClipPrecision
    DEFAULT_QUALITY, // nQuality
    DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
    _T("Arial")); // lpszFacename

    // only 96 usable ascii for performance in some low end system
    GLuint displayLists = glGenLists(96);

    // remember the old font
    HFONT oldfont = (HFONT)SelectObject (m_hDC, font);
    wglUseFontBitmaps(m_hDC, 32, 96, displayLists);
    SelectObject(m_hDC, oldfont);

    return displayLists;

    Then to print any text, is just a matter to call the display list with the array on text as an array of display list starting from the base,
    I assuming Qt does something similar but I cannot figure it out,
    Can someone help me?

  • The advice from "QGLWidget": docs:

    [quote]"Overpaint": with QPainter::drawText() instead.[/quote]

  • I go it, thank you.

    I have anothe quetsion related to this and peroformance.
    do you know if the 2d text is drawn using Open Gl command or is by using the drawing funtionality.

    one of the big problems in many graphics apps is that bitmap fonts becomes the slower part of the application, you can see that for example when ising SDL ot GLUT.
    It is always better to make fonts by a using a true font and building a display List for each character.

  • Another way is using a QPainter to draw to a texture which is displayed with quads. This is the approach I use in MIFit. You can see the code here:

  • QPainter uses openGL calls to render the text. (It's a gl widget after all.)

  • Thanks I saw that, and I have what I want aleady.
    QPaint provides lots of 2d functionality, it is awesome.

    Thanks you for the help

Log in to reply