记一次坑
- QCustomplot绘制图标相对比较好用,但当绘制的数据较多时,就会导致界面刷新肉眼可见的卡顿,查看QCustomplot接口发现有setOpenGl()接口。查看接口说明:
\note OpenGL support is only enabled if QCustomPlot is compiled with the macro \c QCUSTOMPLOT_USE_OPENGL
defined. This define must be set before including the QCustomPlot header both during compilation
of the QCustomPlot library as well as when compiling your application. It is best to just include
the line <tt>DEFINES += QCUSTOMPLOT_USE_OPENGL</tt> in the respective qmake project files.
\note If you are using a Qt version before 5.0, you must also add the module "opengl" to your \c
QT variable in the qmake project files. For Qt versions 5.0 and higher, QCustomPlot switches to a
newer OpenGL interface which is already in the "gui" module.
可以看到,想使用OpenGL加速需要在qmake的pro文件中加入DEFINES += QCUSTOMPLOT_USE_OPENGL
,但运行仍然报错:void __cdecl QCustomPlot::setOpenGl(bool,int) QCustomPlot can't use OpenGL because QCUSTOMPLOT_USE_OPENGL was not defined during compilation (add 'DEFINES += QCUSTOMPLOT_USE_OPENGL' to your qmake .pro file)
。
2. 查阅网上帖子,后再pro中加入:
QT += opengl
DEFINES += QCUSTOMPLOT_USE_OPENGL
LIBS+=-lopengl32 -lglu32
依稀记得有个帖子说,qt5.x自带OpenGL库文件,qt5确实提供opengl相关的库,如QtOpenGl,QOpenGLWidget,但其实现方式由于没有去具体研究过,不知如何实现的,总之就是没解决问题。
3. 后来了解到glut和freeglut,分别安装后,加入项目文件,并在pro中加入也并没有用,该报的错误还是在继续。
freeglut链接:按情况下载
glut,比较老了官网都说二十年前就不支持了,推荐去下载freeglut:glut-3.7.6-bin.zip
INCLUDEPATH += $$PWD/freeglut/include
LIBS += -L$$PWD/freeglut/lib/x64 -lfreeglut
- 最终解决方法:
在上面的方法之外,修改qcustomplot.cpp
//在头文件出添加头
#include <GL/freeglut.h>
void QCPPaintBufferGlFbo::draw(QCPPainter* painter) const
{
……
if (QOpenGLContext::currentContext() != mGlContext.data()) {
mGlContext.data()->makeCurrent(mGlContext.data()->surface());
}
//在drawImage之前加入上面的判断语句,不然会报错:
//QOpenGLFramebufferObject::bind() called from incompatible context
painter->drawImage(0, 0, mGlFrameBuffer->toImage());
}
最后可以在继承qcustomplot的类使用
this->setOpenGl(true);
qDebug() << "opengl state:" << this->openGl();
参考:
- https://blog.csdn.net/qing666888/article/details/78946384
- https://betheme.net/yidongkaifa/28091.html?action=onClick
- https://blog.csdn.net/qq_39736982/article/details/132333950