• qcustemplot使用opengl加速


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

    1. 下载qCustomPlot

      https://www.qcustomplot.com/

    2. 下载freeglut库

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

      http://freeglut.sourceforge.net/index.php
      我使用的是qt的mingw编译器,所以参考下面的文档
      https://www.transmissionzero.co.uk/computing/using-glut-with-mingw/

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

    	#include "qcustomplot.h"
    	#include 
    
    • 1
    • 2
    1. 在qcustomplot.cpp文件中搜索 “QCPPaintBufferGlFbo::draw”,在“drawImage”之前一行加入如下代码以修复上下文错误
    /* inherits documentation from base class */
    void QCPPaintBufferGlFbo::draw(QCPPainter *painter) const
    {
      if (!painter || !painter->isActive())
      {
        qDebug() << Q_FUNC_INFO << "invalid or inactive painter passed";
        return;
      }
      if (!mGlFrameBuffer)
      {
        qDebug() << Q_FUNC_INFO << "OpenGL frame buffer object doesn't exist, reallocateBuffer was not called?";
        return;
      }
     //增加下面的代码
      if (QOpenGLContext::currentContext() != mGlContext.data()) {
            mGlContext.data()->makeCurrent(mGlContext.data()->surface());
      }
     
      painter->drawImage(0, 0, mGlFrameBuffer->toImage());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    如果不加该行代码,运行会显示下面的问题。

    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    QOpenGLFramebufferObject::bind() called from incompatible context
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 在工程.pro中添加以下内容:

       在工程右键"添加库",找到libfreeglut.a库文件和头文件
       **把freeglut.dll文件拷贝到exe所在目录下**,注意32位和64位库的目录有区别。
      
      • 1
      • 2

    在库引用目录中添加 -lopengl32 -lglu32 否则会报如下的错误。

    debug/qcustomplot.o: In function `ZN19QCPPaintBufferGlFbo5clearERK6QColor':
    F:\mrzhang\Works\RecordWave\build-QtRecordWave-Desktop_Qt_5_12_4_MinGW_32_bit-Debug/../QtRecordWave/qcustomplot.cpp:937: undefined reference to `_imp__glClearColor@16'
    F:\mrzhang\Works\RecordWave\build-QtRecordWave-Desktop_Qt_5_12_4_MinGW_32_bit-Debug/../QtRecordWave/qcustomplot.cpp:938: undefined reference to `_imp__glClear@4'
    collect2.exe: error: ld returned 1 exit status
    mingw32-make[1]: *** [Makefile.Debug:105: debug/QtRecordWave.exe] Error 1
    mingw32-make: *** [Makefile:38: debug] Error 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    贴下我的pro文件。
    注意下面的中文注释

    #-------------------------------------------------
    #
    # Project created by QtCreator 2022-10-15T09:59:54
    #
    #-------------------------------------------------
    
    QT       += core gui network opengl
    #添加 opengl
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
    
    TARGET = QtRecordWave
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS QCUSTOMPLOT_USE_OPENGL
    #添加QCUSTOMPLOT_USE_OPENGL
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    CONFIG += c++11
    
    SOURCES += \
            dialog.cpp \
            main.cpp \
            mainwindow.cpp \
            procthread.cpp \
            qcustomplot.cpp \
            tcpserver.cpp \
            tcpsocket.cpp \
            waveplot.cpp
    
    HEADERS += \
            dialog.h \
            mainwindow.h \
            procthread.h \
            qcustomplot.h \
            tcpserver.h \
            tcpsocket.h \
            waveplot.h
    
    FORMS += \
            dialog.ui \
            mainwindow.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    win32:LIBS += -L$$PWD/freeglut-MinGW-3.0.0-1.mp/freeglut/lib/ -lfreeglut -lopengl32 -lglu32
    else:unix: LIBS += -L$$PWD/freeglut-MinGW-3.0.0-1.mp/freeglut/lib/ -lfreeglut -lopengl32 -lglu32
    
    INCLUDEPATH += $$PWD/freeglut-MinGW-3.0.0-1.mp/freeglut/include
    DEPENDPATH += $$PWD/freeglut-MinGW-3.0.0-1.mp/freeglut/include
    
    INCLUDEPATH += $$PWD/freeglut-MinGW-3.0.0-1.mp/freeglut/include
    DEPENDPATH += $$PWD/freeglut-MinGW-3.0.0-1.mp/freeglut/include
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    1. 在你代码中设置qCustomPlot开启OpenGl功能,在运行时,看到“应用程序输出”窗口有opengl = true字样,说明启动成功。

    ui->customPlot->setOpenGl(true);
    qDebug()<<“opengle=”openGl();

    ————————————————

  • 相关阅读:
    基于springboot+vue共享充电宝管理系统
    mysql的缓存页对LRU的改进;预读机制;及对应的调优
    Linux学习-40-格式化分区mkfs、mke2fs命令用法
    iota()函数
    使用phpMyAdmin管理WordPress数据库入门指南
    基于JavaWeb的宿舍管理系统设计与实现
    Pycharm 对容器中的 Python 程序断点远程调试
    C#控件随窗体大小动态调整
    K8S部署高可用postgresql集群 —— 筑梦之路
    Sui学术研究奖公布,资助研究者探索人工智能、能源市场和区块链游戏
  • 原文地址:https://blog.csdn.net/zwjzwj108108/article/details/127458512