Search code examples
c++qtopenglqpainter

How to draw smoothly using QPainter, QOpenGLPaintDevice and QOpenGLWidget on retina display?


The drawing is very laggy when I try to draw using QWidget and QPainter. I decided to accelerate the drawing by using QOpenGLPaintDevice and QOpenGLWidget. But the rounded corners of shapes are very rude, comparing to drawing on regular widget.

QWidget:

enter image description here

QOpenGLWidget:

enter image description here

Here is a code how I setup the QOpenGLPaintDevice.

QOpenGLPaintDevice* device = nullptr;

void QOpenGLWorkspaceWidget::resizeGL(int w, int h) {
    initDeviceIfNeed();
    handleResize(this, w, h);
    int ratio = devicePixelRatio();
    device->setSize(QSize(w * ratio, h * ratio));
    device->setDevicePixelRatio(ratio);
}

void QOpenGLWorkspaceWidget::initDeviceIfNeed() {
    if (!device) {
        device = new QOpenGLPaintDevice();
        drawer->setPaintDevice(device);
    }
}

void QOpenGLWorkspaceWidget::paintGL() {
    initDeviceIfNeed();
    glDisable(GL_DEPTH_TEST);
    glClearColor(1, 1, 1, 1);
    workspaceDrawer->draw();
}

Solution

  • I found the answer here

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

    openGlWidget.setSamples(8) and painter.setRenderHint(QPainter::Antialiasing) did the job.