Qt QCustomPlot 踩坑记录
Posted DreamLife.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt QCustomPlot 踩坑记录相关的知识,希望对你有一定的参考价值。
Qt QCustomPlot 踩坑记录
文章目录
关键字:
QCustomPlot
、
setData
、
addData
、
replot
、
Qt
摘要
QCustomPlot
是一个用于绘图和数据可视化的Qt ``C++
构件。 它没有进一步的依赖关系,并有很好的文档记录。 该绘图库专注于制作美观,出版品质的2D图表,图表和图表,以及为实时可视化应用程序提供高性能。 QCustomPlot
可以导出为各种格式,如矢量化的PDF
文件和光栅化图像,如PNG
,JPG
和BMP
。 QCustomPlot
是在应用程序内部显示实时数据以及为其他媒体生成高质量图的解决方案。
在实际生产中,遇到的问题还是不少的,最简单的使用可以看我以前写的文章
Qt QCustomPlot使用记录,这里简单记录了我第一次使用QCustomPlot
,在一个数据采集小工具中的使用。当初使用了 addData
来添加数据,并开启了OpenGL 3D
加速,在小工具下使用除了卡顿,没有太多问题,那时怀疑就是我没有开线程,造成的卡顿。
但是,这次当我把QCustomPlot
搬到真正的项目中,并部署到生产力机器上连入真实数据后发现,出现了好多莫名奇妙的错误,下面让我一一说明
1 两个数据显示的内容相同
问题描述
这个问题是我现在都不理解的一个问题,就是我在一个主界面上,存在两个QCustomPlot
模块,当我给第一个模块添加数据是,会在两个模块上同时显示数据,当我给第二个模式添加数据是,连个模块均不显示。
解决方法
为了解决这个问题,我开始怀疑我开线程操纵界面控件的问题,所以显示切换了信号槽通信,间接控制界面,还是无解,更换数据源等,最后,不得不重写一个莫夸出来,最后发现问题出在了这里gCustomplot->setOpenGl(true);
。哎,此刻一抬头羊驼。在小工具里面我也开启了OpenGL 家属,并没有问题,在实际项目中,就是会有问题。把它注释掉就没有问题了。这里我也吧作者的注释贴出来。注意第一句话,作者说了,这个是一个实验性的功能。
/*!
\\warning This is still an experimental feature and its performance depends on the system that it
runs on. Having multiple QCustomPlot widgets in one application with enabled OpenGL rendering
might cause context conflicts on some systems.
This method allows to enable OpenGL plot rendering, for increased plotting performance of
graphically demanding plots (thick lines, translucent fills, etc.).
If \\a enabled is set to true, QCustomPlot will try to initialize OpenGL and, if successful,
continue plotting with hardware acceleration. The parameter \\a multisampling controls how many
samples will be used per pixel, it essentially controls the antialiasing quality. If \\a
multisampling is set too high for the current graphics hardware, the maximum allowed value will
be used.
You can test whether switching to OpenGL rendering was successful by checking whether the
according getter \\a QCustomPlot::openGl() returns true. If the OpenGL initialization fails,
rendering continues with the regular software rasterizer, and an according qDebug output is
generated.
If switching to OpenGL was successful, this method disables label caching (\\ref setPlottingHint
"setPlottingHint(QCP::phCacheLabels, false)") and turns on QCustomPlot's antialiasing override
for all elements (\\ref setAntialiasedElements "setAntialiasedElements(QCP::aeAll)"), leading to a
higher quality output. The antialiasing override allows for pixel-grid aligned drawing in the
OpenGL paint device. As stated before, in OpenGL rendering the actual antialiasing of the plot is
controlled with \\a multisampling. If \\a enabled is set to false, the antialiasing/label caching
settings are restored to what they were before OpenGL was enabled, if they weren't altered in the
meantime.
\\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.
*/
void QCustomPlot::setOpenGl(bool enabled, int multisampling)
mOpenGlMultisamples = qMax(0, multisampling);
#ifdef QCUSTOMPLOT_USE_OPENGL
mOpenGl = enabled;
if (mOpenGl)
if (setupOpenGl())
// backup antialiasing override and labelcaching setting so we can restore upon disabling OpenGL
mOpenGlAntialiasedElementsBackup = mAntialiasedElements;
mOpenGlCacheLabelsBackup = mPlottingHints.testFlag(QCP::phCacheLabels);
// set antialiasing override to antialias all (aligns gl pixel grid properly), and disable label caching (would use software rasterizer for pixmap caches):
setAntialiasedElements(QCP::aeAll);
setPlottingHint(QCP::phCacheLabels, false);
else
qDebug() << Q_FUNC_INFO << "Failed to enable OpenGL, continuing plotting without hardware acceleration.";
mOpenGl = false;
else
// restore antialiasing override and labelcaching to what it was before enabling OpenGL, if nobody changed it in the meantime:
if (mAntialiasedElements == QCP::aeAll)
setAntialiasedElements(mOpenGlAntialiasedElementsBackup);
if (!mPlottingHints.testFlag(QCP::phCacheLabels))
setPlottingHint(QCP::phCacheLabels, mOpenGlCacheLabelsBackup);
freeOpenGl();
// recreate all paint buffers:
mPaintBuffers.clear();
setupPaintBuffers();
#else
Q_UNUSED(enabled)
qDebug() << Q_FUNC_INFO << "QCustomPlot can't use OpenGL because QCUSTOMPLOT_USE_OPENGL was not defined during compilation (add 'DEFINES += QCUSTOMPLOT_USE_OPENGL' to your qmake .pro file)";
#endif
2 使用addData ,会导致程序崩溃
这个问题也是没天理了,我在Demo中用的也是这个函数,一点问题都没有,在实际项目中,看内存蹭蹭涨,最后程序挂掉报qt terminate called after throwing an instance of 'std::bad_alloc'
错误。最终解决方案,自己造两个Double的容易,手动控制其大小,使用setData代替addData。
以上是关于Qt QCustomPlot 踩坑记录的主要内容,如果未能解决你的问题,请参考以下文章