qcustomplot 大量数据拖拽卡顿,开启opengl

发布于:2025-08-02 ⋅ 阅读:(17) ⋅ 点赞:(0)

记录一下qcustomplot在加载大量数据情况下卡顿的解决办法。

1. 导入库文件

QT       += core gui  opengl
DEFINES += QCUSTOMPLOT_USE_OPENGL

win32:LIBS += -lOpengl32 \
                -lglu32 \
                -lglut

2. 开启opengl

customplot->setOpenGl(true);

3. 解决opengl异常缩放问题

修改qcustomplot.cpp源码中mGlPaintDevice.data()->setDevicePixelRatio(mDevicePixelRatio);改为 mGlPaintDevice.data()->setDevicePixelRatio(1.0);

void QCPPaintBufferGlFbo::reallocateBuffer()
{
    // release and delete possibly existing framebuffer:
    if (mGlFrameBuffer)
    {
        if (mGlFrameBuffer->isBound())
            mGlFrameBuffer->release();
        delete mGlFrameBuffer;
        mGlFrameBuffer = 0;
    }

    if (mGlContext.isNull())
    {
        qDebug() << Q_FUNC_INFO << "OpenGL context doesn't exist";
        return;
    }
    if (mGlPaintDevice.isNull())
    {
        qDebug() << Q_FUNC_INFO << "OpenGL paint device doesn't exist";
        return;
    }

    // create new fbo with appropriate size:
    mGlContext.data()->makeCurrent(mGlContext.data()->surface());
    QOpenGLFramebufferObjectFormat frameBufferFormat;
    frameBufferFormat.setSamples(mGlContext.data()->format().samples());
    frameBufferFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
    mGlFrameBuffer = new QOpenGLFramebufferObject(mSize*mDevicePixelRatio, frameBufferFormat);
    if (mGlPaintDevice.data()->size() != mSize*mDevicePixelRatio)
        mGlPaintDevice.data()->setSize(mSize*mDevicePixelRatio);
#ifdef QCP_DEVICEPIXELRATIO_SUPPORTED
    // mGlPaintDevice.data()->setDevicePixelRatio(mDevicePixelRatio);
    mGlPaintDevice.data()->setDevicePixelRatio(1.0);
#endif

}

网站公告

今日签到

点亮在社区的每一天
去签到