What steps are necessary to enable antialiasing when using a QPainter on a QGLWidget?

I found the solution. When debugging a different issue, I found messages in my debug output to the effect that you can't set renderhints before the call to begin().

The following works:

QGLWidget *widget = ui->renderWidget;

QPainter painter;

widget->makeCurrent();
glEnable(GL_MULTISAMPLE);
glEnable(GL_LINE_SMOOTH);

painter.begin(widget);

painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);

You can try to enable the antialiasing on the complete Widget :

QGLWidget::setFormat(QGLFormat(QGL::SampleBuffers));


This question is quite old but I still found it on Google. You shouldn't use QGLWidget any more. Use the newer QOpenGLWidget. This renders the scene off-screen rather than creating a native OpenGL window which causes all sorts of issues with resizing layouts. This code works for me. Put it in your QGraphicsView constructor:

QOpenGLWidget* gl = new QOpenGLWidget;
QSurfaceFormat fmt;
fmt.setSamples(8);
gl->setFormat(fmt);
setViewport(gl);
setRenderHint(QPainter::Antialiasing);

Tags:

Opengl

Qt