首页 > 解决方案 > 如何在视网膜显示器上使用 QPainter、QOpenGLPaintDevice 和 QOpenGLWidget 平滑绘制?

问题描述

当我尝试使用QWidget和绘图时,绘图非常滞后QPainter。我决定使用QOpenGLPaintDeviceand来加速绘图QOpenGLWidget。但是与在常规小部件上绘制相比,形状的圆角非常粗鲁。

QWidget:

在此处输入图像描述

QOpenGL小部件:

在此处输入图像描述

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

标签: c++qtopenglqpainter

解决方案


我在这里找到了答案

在 QGLWidget 上使用 QPainter 时启用抗锯齿需要哪些步骤?

openGlWidget.setSamples(8)painter.setRenderHint(QPainter::Antialiasing) 完成了这项工作。


推荐阅读