Question

I'm currently working with Qt5.1 and trying to draw some OpenGL stuff within a QGLWidget:

void Widget::paintGL() {
    startClipping(10, height()-110,100,100);

    qglColor(Qt::red);
    glBegin(GL_QUADS);
        glVertex2d(0,0);
        glVertex2d(500,0);
        glVertex2d(500,500);
        glVertex2d(0,500);
    glEnd();

    qglColor(Qt::green);
    this->renderText(50, 50, "SCISSOR TEST STRING");

    endClipping();
}

The quad gets clipped correctly but the text doesn't. I tried three ways of implementing the startClipping method: scissor test, setting the viewport to the clipping area and with a stencil buffer. None of them worked and the whole string was drawn instead of beeing cut off at the edges of the clipping area.

Now my question is: Is this behavior a bug of Qt or is there something, I missed or another possibility I could try??

Was it helpful?

Solution

After a week of trying around, I suddenly found a very simple way to achieve, what I was looking for. Using a QPainter and it's methods instead of the QGLWidget's renderText() simply makes text clipping work:

QPainter *painter = new QPainter();
painter->begin();

painter->setClipping(true);

painter->setClipPath(...);   // or
painter->setClipRect(...);   // or
painter->setClipRegion(...);

painter->drawText(...);

painter->end();

OTHER TIPS

As I understand it, this is by design. According to the documentation ( https://qt-project.org/doc/qt-4.8/qglwidget.html#renderText ):

Note: This function clears the stencil buffer.
Note: This function temporarily disables depth-testing when the text is drawn.

However for the 'xyz version' (overloaded function)

Note: If depth testing is enabled before this function is called, then the drawn text will be depth-tested against the models that have already been drawn in the scene. Use glDisable(GL_DEPTH_TEST) before calling this function to annotate the models without depth-testing the text.

So, if you use the second version (by including a z-value, eg 0) in your original code, I think you get what you want. I think you would want to do this if you do a scene that is 'real' 3D (eg, axis labels on a 3D plot).

The documentation does also mention using drawText.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top