Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qt

opengl

I am trying to draw basic shapes on a QGLWidget. I am trying to enable antialiasing to smooth out the lines, but it is not working.

This is what I am trying at the moment:

QGLWidget *widget = ui->renderWidget;

QPainter painter;

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

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

painter.begin(widget);

However, anything drawn with this painter still has jagged edges. What else do I need to do?

like image 239
Elliott Avatar asked Jun 10 '12 18:06

Elliott


2 Answers

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);
like image 67
Elliott Avatar answered Oct 01 '22 14:10

Elliott


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

QGLWidget::setFormat(QGLFormat(QGL::SampleBuffers));
like image 26
Marc Plano-Lesay Avatar answered Oct 01 '22 15:10

Marc Plano-Lesay