欢迎您的访问
专注架构,Java,数据结构算法,Python技术分享

Qt 画瀑布图,热力图,雨图

效果图如下

概述

Qt画信号瀑布图有很多方式,这里使用第三方组件QCustomPlot

组件官网下载地址:https://www.qcustomplot.com/index.php/download

本文使用的是2.0.1版本

下载慢的话,我放到了CSDN上,这里下:https://download.csdn.net/download/woniu211111/16788925

开始使用

新建qcustomplot-example工程,添加WaterfallDialog类,基类为QDialog

把qcustomplot包里的qcustomplot.cpp和qcustomplot.h文件引入到工程

.pro文件添加printsupport

UI界面拖拽添加Widget控件,ObjectName为widget,样式背景颜色为黑色

鼠标放到widget控件鼠标右键提升为QCustomPlot

WaterfallDialog.h引入头文件,#include “qcustomplot.h”

添加如下内容:

    void PlotWaterfall(QCustomPlot* customPlot);

    QCustomPlot* customPlot;

    QCPColorMap* cpColorMap;

构造函数添加如下:

void PlotWaterfall(QCustomPlot* customPlot)函数实现如下:

 void WaterfallDialog::PlotWaterfall(QCustomPlot* customPlot)
{
    //背景为黑色
    QLinearGradient plotGradient;
    plotGradient.setColorAt(0, QColor(000));
    plotGradient.setColorAt(1, QColor(000));
    customPlot->setBackground(plotGradient);

    //坐标轴为白色
    customPlot->xAxis->setBasePen(QPen(Qt::white, 1));
    customPlot->yAxis->setBasePen(QPen(Qt::white, 1));

    //坐标轴的提示信息为白色
    customPlot->xAxis->setTickLabelColor(Qt::white);
    customPlot->yAxis->setTickLabelColor(Qt::white);

    //可拖拽+可滚轮缩放
    customPlot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom);

    //添加标题
    customPlot->plotLayout()->insertRow(0);
    QCPTextElement* title = new QCPTextElement(customPlot, "信号瀑布图", QFont("黑体"15, QFont::Bold));
    title->setTextColor(QColor(2552550));
    customPlot->plotLayout()->addElement(00, title);

    //设置坐标轴范围,提示标签样式
    customPlot->xAxis->setRange(01000);
    customPlot->yAxis->setRange(-6000,6000);
    customPlot->yAxis->ticker()->setTickCount(6);
    customPlot->yAxis->setTickLabelFont(QFont("黑体"10, QFont::Light));

    //设置色谱范围
    cpColorMap = new QCPColorMap(customPlot->xAxis,customPlot->yAxis);
    cpColorMap->data()->setSize(100012000); //设置X轴1000个点,Y轴12000个点
    cpColorMap->data()->setRange(QCPRange(0,1000),QCPRange(-6000,6000));

    //信号范围在-4000到+4000,设置数据
    for(int x=0; x<1000; x++)
    {
        for(int y=2000; y<2000+8000; y++)
        {
            cpColorMap->data()->setCell(x, y, qCos(x/10.0)+qSin(y/10.0));
        }
    }

    //构建色条
    QCPColorScale *colorScale = new QCPColorScale(customPlot);
    colorScale->setType(QCPAxis::atRight); // 刻度应为垂直条,刻度线/坐标轴标签右侧(实际上,右侧已经是默认值)
    colorScale->axis()->setLabelColor(Qt::white);
    colorScale->axis()->setTickLabelColor(Qt::white);
    colorScale->axis()->setTickLabels(false);
    colorScale->axis()->setTickLabelFont(QFont(("Arial"),10));
    colorScale->axis()->setLabel("信号强度变化");
    colorScale->axis()->setLabelFont(QFont(("Arial"), 10));
    cpColorMap->setColorScale(colorScale); // 将颜色图与色标关联

    //设置色条的颜色变化
    QCPColorGradient gradient;  // 色条使用的颜色渐变
    if(true//默认颜色变化
    {
        cpColorMap->setGradient(QCPColorGradient::gpJet);
        cpColorMap->rescaleDataRange(true);
    }
    else
    {
        gradient.setColorStopAt(0.0, QColor("#f6efa6"));   // 设置色条开始时的颜色
        gradient.setColorStopAt(1.0, QColor("#bf444c"));  // 设置色条结束时的颜色
        cpColorMap->setGradient(gradient);
    }
    cpColorMap->rescaleDataRange(true);
    customPlot->plotLayout()->addElement(11, colorScale); // 将其添加到主轴矩形的右侧

    //立即刷新
    customPlot->replot();
}

源码地址:https://github.com/woniu201/qt-examples/tree/master/qcustomplot-example

最终效果如下:

赞(14) 打赏
版权归原创作者所有,任何形式转载请联系作者;码农code之路 » Qt 画瀑布图,热力图,雨图

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏