• 【原创】qCustomPlot启用OpenGL


            网上有很多讲qCustomPlot启动OpenGL的文章,有些是很有用的,但也好像没有完全说清楚,现在把我觉得有用的收集起来,并加上一些自己的理解。

    1. 下载qCustomPlot

         https://www.qcustomplot.com/

    2. 下载freeglut库

         由于qCustomPlot需要调用glut库里的函数,但是glut库比较老旧,而且在实际测试中,也发现一旦new了多个图表控件,当鼠标在几个窗口之间点击的时候,会互相影响,错乱,甚至崩溃的现象。freeglut库是glut的最佳替代版本,更换之后,果然解决了这个问题。

        http://freeglut.sourceforge.net/index.php

     

    3. 将"qcustomplot.h和qcustomplot.cpp"添加到自己的工程中,在qcustomplot.cpp最前面加一条头文件引用

    1. #include "qcustomplot.h"
    2. //
    3. #include
    4. //>

    4. 在qcustomplot.cpp文件中搜索 "QCPPaintBufferGlFbo::draw",在“drawImage”之前一行加入如下代码以修复上下文错误

    1. /* inherits documentation from base class */
    2. void QCPPaintBufferGlFbo::draw(QCPPainter *painter) const
    3. {
    4. if (!painter || !painter->isActive())
    5. {
    6. qDebug() << Q_FUNC_INFO << "invalid or inactive painter passed";
    7. return;
    8. }
    9. if (!mGlFrameBuffer)
    10. {
    11. qDebug() << Q_FUNC_INFO << "OpenGL frame buffer object doesn't exist, reallocateBuffer was not called?";
    12. return;
    13. }
    14. //
    15. if (QOpenGLContext::currentContext() != mGlContext.data()) {
    16. mGlContext.data()->makeCurrent(mGlContext.data()->surface());
    17. }//>
    18. painter->drawImage(0, 0, mGlFrameBuffer->toImage());
    19. }

    5. 在工程.pro中添加以下内容:

            在工程右键"添加库",找到freeglut.lib库文件和头文件,把freeglut.dll文件拷贝到exe所在目录下,注意32位和64位库的目录有区别,由于项目中需要32位库,但该模块将来还想用于64位项目,所以我都给它加入进去,反正也不费电。

    1. Qt +=opengl
    2. DEFINES += QCUSTOMPLOT_USE_OPENGL
    3. //不需要传说中的 Lib += -lopengl32
    4. unix|win32: LIBS += -L$$PWD/../../../bin/Lib/freeglut-MSVC-3.0.0/freeglut/lib/ -lfreeglut
    5. unix|win32: LIBS += -L$$PWD/../../../bin/Lib/freeglut-MSVC-3.0.0/freeglut/lib/x64/ -lfreeglut
    6. INCLUDEPATH += $$PWD/../../../bin/Lib/freeglut-MSVC-3.0.0/freeglut/include
    7. DEPENDPATH += $$PWD/../../../bin/Lib/freeglut-MSVC-3.0.0/freeglut/include

    6. 在你代码中设置qCustomPlot开启OpenGl功能,在运行时,看到“应用程序输出”窗口有opengl = true字样,说明启动成功。

    1. ui->customPlot->setOpenGl(true);
    2. qDebug()<<"opengle="<customPlot->openGl();

     

  • 相关阅读:
    区块链溯源相比传统追溯有什么优点?
    深入理解JNI
    js图像对比可用的一些资源
    go-cqhttp系列教程-go-cqhttp安装
    Pytest+Unittest+Git+Jenkins企业级CICD自动化测试平台建设方案
    Synchronized和volatile 面试简单汇总
    java家政服务平台计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    CSS面试题:说一说对rem的理解?
    HTTP协议中Gzip格式的流量分析与识别
    苹果笔记本充不了电的解决办法
  • 原文地址:https://blog.csdn.net/jam12315/article/details/126582996