如何在视网膜显示器上使用 QPainter、QOpenGLPaintDevice 和 QOpenGLWidget 流畅地绘制?

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

当我尝试使用 QWidgetQPainter 绘图时,绘图非常缓慢。我决定使用 QOpenGLPaintDeviceQOpenGLWidget 来加速绘图。但是形状的圆角与在常规widget上绘制相比非常粗鲁。

QWidget:

QOpenGLWidget:

这是我如何设置 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();
}

我在这里找到了答案

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

openGlWidget.setSamples(8)painter.setRenderHint(QPainter::Antialiasing) 完成任务了。