QCustomPlot使用Tips(自用)

QCustomPlot使用Tips(自用)



前言

此前Qt工程使用的一直都是QCharts,说实话,实在是太难用了。
QChart痛点1:大数据情况刷新缓慢
QChart痛点2:动态添加数据不友好
QChart痛点3:想要做一些拖动,放大缩小需要自己写
相比之下,QCustomPlot就很友好了,建议大家都用这个绘制曲线、直方图之类的统计相关图。


一、QCustomPlot是什么?怎么下载?

QCustomPlot是一个用于绘图和数据可视化的Qt C++构件。 它没有进一步的依赖关系,并有很好的文档记录。 该绘图库专注于制作美观,出版品质的2D图表,图表和图表,以及为实时可视化应用程序提供高性能。 QCustomPlot可以导出为各种格式,如矢量化的PDF文件和光栅化图像,如PNG,JPG和BMP。 QCustomPlot是在应用程序内部显示实时数据以及为其他媒体生成高质量图的解决方案。

博友们已经详细说明了,在这里就不过多赘述了
传送门1
传送门2

二、TIPS

1th Tip:QCustomPlot绘制成QPixmap用于自定义pdf绘制等

1.简单的直接将QCustomPlot,根据设定的长和宽,绘制到QPixmap中

代码如下:

QCustomPlot* show_plot = ui.mycustomplot;
float show_w = 100;
float show_h = 50;
QPixmap pixmap = show_plot->toPixmap(show_w, show_h);

源代码如下(qcustomplot.cpp):

/*!
  Renders the plot to a pixmap and returns it.
  
  The plot is sized to \a width and \a height in pixels and scaled with \a scale. (width 100 and
  scale 2.0 lead to a full resolution pixmap with width 200.)
  
  \see toPainter, saveRastered, saveBmp, savePng, saveJpg, savePdf
*/
QPixmap QCustomPlot::toPixmap(int width, int height, double scale)
{
  // this method is somewhat similar to toPainter. Change something here, and a change in toPainter might be necessary, too.
  int newWidth, newHeight;
  if (width == 0 || height == 0)
  {
    newWidth = this->width();
    newHeight = this->height();
  } else
  {
    newWidth = width;
    newHeight = height;
  }
  int scaledWidth = qRound(scale*newWidth);
  int scaledHeight = qRound(scale*newHeight);

  QPixmap result(scaledWidth, scaledHeight);
  result.fill(mBackgroundBrush.style() == Qt::SolidPattern ? mBackgroundBrush.color() : Qt::transparent); // if using non-solid pattern, make transparent now and draw brush pattern later
  QCPPainter painter;
  painter.begin(&result);
  if (painter.isActive())
  {
    QRect oldViewport = viewport();
    setViewport(QRect(0, 0, newWidth, newHeight));
    painter.setMode(QCPPainter::pmNoCaching);
    if (!qFuzzyCompare(scale, 1.0))
    {
      if (scale > 1.0) // for scale < 1 we always want cosmetic pens where possible, because else lines might disappear for very small scales
        painter.setMode(QCPPainter::pmNonCosmetic);
      painter.scale(scale, scale);
    }
    if (mBackgroundBrush.style() != Qt::SolidPattern && mBackgroundBrush.style() != Qt::NoBrush) // solid fills were done a few lines above with QPixmap::fill
      painter.fillRect(mViewport, mBackgroundBrush);
    draw(&painter);
    setViewport(oldViewport);
    painter.end();
  } else // might happen if pixmap has width or height zero
  {
    qDebug() << Q_FUNC_INFO << "Couldn't activate painter on pixmap";
    return QPixmap();
  }
  return result;
}

通过这个方法可以很好的将界面上正在显示的QCustomPlot图像,绘制到任何你想放的位置,包括但不限于报表(PDF)里面作为统计图。

2th Tip:大数据变得更快,减少频闪之类的问题

代码如下(setAdaptiveSampling):

 //添加图形
 QCPGraph* graph_tmp = ui.mycustomplot->addGraph();
 //自适应精简显示
 graph_tmp->setAdaptiveSampling(true);

源代码如下(qcustomplot.cpp):

void QCPGraph::setAdaptiveSampling(bool enabled)
{
  mAdaptiveSampling = enabled;
}

3th Tip:让图像可以拖拽,放大缩小之类的

代码如下(setAdaptiveSampling):

//设置基本坐标轴(左侧Y轴和下方X轴)可拖动、可缩放、曲线可选、legend可选、设置伸缩比例,使所有图例可见
ui.mycustomplot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
            QCP::iSelectLegend | QCP::iSelectPlottables);

源代码如下(qcustomplot.cpp):

void QCustomPlot::setInteractions(const QCP::Interactions &interactions)
{
  mInteractions = interactions;
}

总结

以上是我使用QCustomPlot的一些收获,如果以后有空了或者遇到新的需要加的了,再更新

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷失的walker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值