网上有很多讲qCustomPlot启动OpenGL的文章,有些是很有用的,但也好像没有完全说清楚,现在把我觉得有用的收集起来,并加上一些自己的理解。
下载qCustomPlot
https://www.qcustomplot.com/
下载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/
将"qcustomplot.h和qcustomplot.cpp"添加到自己的工程中,在qcustomplot.cpp最前面加一条头文件引用
#include "qcustomplot.h"
#include
/* 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());
}
如果不加该行代码,运行会显示下面的问题。
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
在工程.pro中添加以下内容:
在工程右键"添加库",找到libfreeglut.a库文件和头文件
**把freeglut.dll文件拷贝到exe所在目录下**,注意32位和64位库的目录有区别。
在库引用目录中添加 -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
贴下我的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
ui->customPlot->setOpenGl(true);
qDebug()<<“opengle=”
————————————————