You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt6中QOpenGLWidget输入延迟问题的优化方案咨询

Qt6 QOpenGLWidget鼠标跟随渲染延迟问题解决咨询

我在Qt6中实现了自定义QOpenGLWidget,重写了*paintGL()和mouseMoveEvent()*方法,用来渲染跟随鼠标移动的三角形。但发现三角形总是滞后鼠标指针一帧甚至更多,快速移动鼠标时两者间距更大,导致应用有明显卡顿感,想咨询缓解该延迟的方法。

使用环境

  • 系统:Debian 11.7 x64
  • Qt版本:在线安装器安装的最新Qt6社区版
  • 硬件:Intel XEON E5-2697处理器、64GiB内存、nVidia Quadro P400显卡

具体实现代码

void SpaceViewWidget::mouseEvent(QMouseEvent *event){
    auto buts = event->buttons();
    for(int i = 0; i < 3; ++i){
        auto buttonName = buttonNames[i];
        const bool last = buttonsPressed[i];
        const bool next = buts & buttonName;
        if(last != next){
            buttonsPressed[i] = next;
            if(next){
                buttonsPressedLocation[i] = event->position() + pan;
            }
        }
    }
}


void SpaceViewWidget::mousePressEvent(QMouseEvent *event){
    mouseEvent(event);
}


void SpaceViewWidget::mouseReleaseEvent(QMouseEvent *event){
    mouseEvent(event);
}


void SpaceViewWidget::mouseMoveEvent(QMouseEvent *event){
    // Middle button pressed
    if(buttonsPressed[2]){
        auto deltaPos = buttonsPressedLocation[2] - event->position();
        pan = deltaPos;
        viewDirty = true;
        update();
        qDebug()<<deltaPos;
    }
}


void SpaceViewWidget::updateViewMatrix(){
    if (viewDirty) {
        viewDirty = false;
        auto sz = QSizeF(size());
        auto sz2 = sz / 2;
        modelMat.setToIdentity();
        float sc=static_cast<float>(1<<chunkDepth);
        modelMat.scale(sc, sc, 1.0);
        viewMat.setToIdentity();
        projectionMat.setToIdentity();
        auto frustum = QRectF(-sz2.width(), -sz2.height(), +sz.width(), +sz.height() );
        projectionMat.ortho(frustum);
        auto center= chunks.center();
        viewMat.translate(-center.x()*sc, -center.y()*sc, 0.0);
        viewMat.translate(-pan.x(), -pan.y(), 0.0);
    }
}

void SpaceViewWidget::paintGL(){
    clearBackground();
    updatePolygonMode();
    updateViewMatrix();
    program.bind();
    textureArray.bind();
    vao.bind();
    vbo.bind();
    program.setUniformValue("modelMat", modelMat);
    program.setUniformValue("viewMat", viewMat);
    program.setUniformValue("projectionMat", projectionMat);
    const auto num_quads = chunks.flags.size()*4;
    f->glDrawArrays(GL_TRIANGLES, 0, num_quads);
}

顶点着色器代码

void main(){
    /*...*/
    gl_Position = 
              projectionMat
            * viewMat
            * modelMat
            * vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

补充说明

以下是用Peek录制的截图,录制帧率为60fps,原生屏幕刷新率为29.97fps。截图中先将鼠标对准三角形角落,再拖动鼠标展示延迟情况:

截图展示鼠标与OpenGL渲染的延迟情况


内容的提问来源于stack exchange,提问作者Mr. Developerdude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:50:02