Important: Please read the Qt Code of Conduct -

QPainter drawRect does not work with GL_CULL_FACE

  • Hi guys, I am using QOpenGLWidget as my main widget and drawing objects using both raw OpenGL command and QPainter (I use QPainter to draw texts and some basic shapes like rectangle.). In my raw OpenGL, I have GL_CULL_FACE enabled but I realized GL_CULL_FACE does not work with QPainter::drawRect().

    I have glEnable(GL_CULL_FACE); in my initializeGL() and in paintGL() use the following to draw text and rectangle.

        // Render text
        m_test_painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
        m_test_painter->setFont(QFont("Arial", 30));
        m_test_painter->drawText(textPos, Qt::AlignCenter, text); // worked
        m_test_painter->setBrush(QColor(0, 0, 255, 127));
        m_test_painter->drawRect(0, 0, width(), height()/2);  // with GL_CULL_FACE enabled, this does not work. The rect does not show up. 
        // Comment out glEnable(GL_CULL_FACE), the rect shows up perfectly.

  • @dalishi
    You should not mix painter and gl calls like that. You should put gl calls between calls to beginNativePainting() and endNativePainting(), which are painter functions. It might also help to save the painter state before and restore it after depending on how it messes up your gl stuff.
    the docs here tell you what the painter does to the states.