作者:billy
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处
前言
在最近的项目中需要显示一些图形和图表,博主感觉Qt自带的 QtCharts 比较简陋,于是往第三方库上考虑。对比了QCustomplot、QWT、ECharts等一些比较热门的图形库,博主这里选择了QCustomplot,这章节记录一下使用过程,方便后期回顾。
QCustomPlot 是一个基于Qt C++的图形库,用于绘制和数据可视化,并为实时可视化应用程序提供高性能服务。它没有进一步的依赖关系,并有着良好的文档记录。只需要在项目中加入头文件 qcustomplot.h 和 qcustomplot.cpp 文件,然后使一个widget提升为 QCustomPlot 类,即可使用。
下载
- 官网下载:QCustomPlot 2
选择带有源代码、帮助文档和示例的资源包
- 网盘下载:QCustomPlot 2.0.1
提取码:35l6
导入帮助文档
- 把 QCustomPlot 2.0.1\documentation\qcustomplot.qch 拷贝到Qt的帮助文档目录 Qt\Qt5.12.8\Docs\Qt-5.12.8 下面
- 在Qt中导入文档
- 可以在Qt帮助文档中查看 QCustomPlot 类的属性和方法
配置
-
在pro中添加printsupport模块由于QCustomPlot中存在导出功能,使用了printsupport模块):
QT += printsupport
-
在项目中导入 qcustomplot.h 和 qcustomplot.cpp
以色谱图为例
QCustomPlot *customPlot = new QCustomPlot(this); // QCustomPlot 继承自QWidget,一个独立窗口
customPlot->setFixedSize(200, 200); // 设置窗口大小
QCPColorScale *colorScale = new QCPColorScale(customPlot); // 颜色滚动条
colorScale->axis()->setTicks(true); // 设置标签可见
colorScale->axis()->setRange(0, 1); // 设置范围
colorScale->setType(QCPAxis::atRight); // 设置显示位置
colorScale->setRangeDrag(false); // 设置不能拖动
colorScale->setRangeZoom(false); // 设置不能滚动
colorScale->setBarWidth(8); // 设置宽度为8
QCPColorMap *colorMap = new QCPColorMap(customPlot->xAxis, customPlot->yAxis);
colorMap->data()->setSize(200, 200); // 调整数据数组的大小,key和value都是200
colorMap->data()->setRange(QCPRange(0, 200), QCPRange(0, 200)); // 设置key和value的范围都是0-200
colorMap->setColorScale(colorScale); // 设置色谱图的颜色滚动条
colorMap->setGradient(QCPColorGradient::gpPolar); // 设置用于表示数据的颜色渐变
colorMap->rescaleDataRange(true); // 设置数据范围以跨越当前数据集中出现的最小值和最大值
customPlot->plotLayout()->addElement(0, 1, colorScale); // 将元素添加到具有行和列的单元格中
customPlot->rescaleAxes(); // 重新缩放轴,使绘图中的所有绘图表完全可见
以下是我的应用,显示openCV处理过的图像
QApplication::processEvents();
ui->view_table->clearContents();
for ( int i = 0; i < m_algorithmList.size(); ++i )
{
QCustomPlot *customPlot = new QCustomPlot(ui->view_table);
customPlot->setFixedSize(ui->view_table->width() - 20, ui->view_table->width() - 70);
QCPColorScale *colorScale = new QCPColorScale(customPlot);
colorScale->axis()->setTicks(true);
colorScale->axis()->setRange(0, 1);
colorScale->setType(QCPAxis::atRight);
colorScale->setRangeDrag(false);
colorScale->setRangeZoom(false);
colorScale->setBarWidth(8);
QCPColorMap *colorMap = new QCPColorMap(customPlot->xAxis, customPlot->yAxis);
if ( m_algorithmList.at(i)->errorType == ERROR_TYPE_NOT_ERROR ) {
cv::Mat mat = m_algorithmList.at(i)->fitttingSurface.clone();
double minV = 0, maxV = 0;
cv::minMaxIdx(mat, &minV, &maxV, nullptr, nullptr, mat == mat);
if (abs(maxV - minV) > 0.00000001) {
mat = (mat - minV) / (maxV - minV) * 255;
}
mat.convertTo(mat, CV_8UC1);
cv::applyColorMap(mat, mat, COLORMAP_JET);
cv::Mat tmpMask = m_algorithmList.at(i)->roiMask.clone();
mat.setTo(cv::Vec3b(255, 255, 255), ~tmpMask);
cv::imshow(QString("mat%1").arg(i).toStdString(), mat);
//------------------------------------------------------------------------------
int max = mat.cols >= mat.rows ? mat.cols : mat.rows;
colorMap->data()->setSize(max, max);
colorMap->data()->setRange(QCPRange(0, max), QCPRange(0, max));
for ( int x = 0; x < mat.cols; ++x )
{
for ( int y = 0; y < mat.rows; ++y )
{
double r = mat.at<Vec3b>(y, x)[0];
double g = mat.at<Vec3b>(y, x)[1];
double b = mat.at<Vec3b>(y, x)[2];
if ( r == 255 && g == 255 && b == 255 ) {
colorMap->data()->setAlpha(x, mat.rows - 1 - y, 0);
} else {
QColor color(r, g, b);
colorMap->data()->setCell(x, mat.rows - 1 - y, color.hueF());
}
}
}
if ( mat.cols >= mat.rows ) {
for ( int x = mat.rows; x < mat.cols; ++x )
{
for ( int y = 0; y < mat.cols; ++y )
{
colorMap->data()->setAlpha(y, x, 0);
}
}
} else {
for ( int x = mat.cols; x < mat.rows; ++x )
{
for ( int y = 0; y < mat.rows; ++y )
{
colorMap->data()->setAlpha(x, y, 0);
}
}
}
} else {
colorMap->data()->setSize(200, 200);
colorMap->data()->setRange(QCPRange(0, 200), QCPRange(0, 200));
}
QCPColorGradient cpg;
cpg.setColorStopAt(0.005, QColor(0, 0, 255)); // blue
cpg.setColorStopAt(0.025, QColor(0, 50, 255)); // light blue
cpg.setColorStopAt(0.350, QColor(0, 255, 255)); // green
cpg.setColorStopAt(0.750, QColor(255, 255, 0)); // yellow
cpg.setColorStopAt(0.975, QColor(255, 30, 0)); // light red
cpg.setColorStopAt(0.995, QColor(255, 0, 0)); // red
colorMap->setColorScale(colorScale);
colorMap->setGradient(cpg);
colorMap->rescaleDataRange(true);
customPlot->plotLayout()->addElement(0, 1, colorScale);
customPlot->rescaleAxes();
customPlot->replot();
ui->view_table->setCellWidget(i, 0, customPlot);
}
和openCV直接imshow作比较