15

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?

Elliott
  • 1,336
  • 1
  • 13
  • 26
  • Does your system support multisample framebuffers? – cmannett85 Jun 10 '12 at 20:14
  • Yes, it's windows 7, and I've gotten it to work on another Qt project that used QGLWidget without a QPainter, by just enabling GL_MULTISAMPLE. – Elliott Jun 10 '12 at 20:19
  • The fact that it's Windows 7 is irrelevant, it's whether or not your GPU and drivers support them - but if another project _on the same machine_ worked correctly then it must be OK. Your example seems to be hinting that you're trying to draw on the widget outside of a `paintEvent(QPaintEvent* event)`, or is it just confusing pseudo-code!? – cmannett85 Jun 11 '12 at 06:41

4 Answers4

17

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);
Elliott
  • 1,336
  • 1
  • 13
  • 26
10

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

QGLWidget::setFormat(QGLFormat(QGL::SampleBuffers));
Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
6

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);
Timmmm
  • 88,195
  • 71
  • 364
  • 509
1

If you work on PyQt5, you will typically subclass QOpenGLWidget() to build your own GPU-powered widget. To turn on anti-aliasing, have a look at the code snippet below:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MyFigureClass(QOpenGLWidget):
    def __init__(self, parent):
        super().__init__(parent)
        fmt = QSurfaceFormat()    # -╷
        fmt.setSamples(8)         #  > anti-aliasing
        self.setFormat(fmt)       # -╵
        [...]

    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)
        qp.setRenderHint(QPainter.Antialiasing)
        [...]
        qp.end()

Note: Thank you @Timmmm for your answer. I found the PyQt5 solution when looking at your C++ code snippets.

K.Mulier
  • 8,069
  • 15
  • 79
  • 141