相关文章推荐
任性的开心果  ·  Getting error : ...·  1 年前    · 
腼腆的卡布奇诺  ·  IDEA 错误: ...·  1 年前    · 

LColorMap 类说明:

jet颜色条,一共由343个颜色块。开放的接口有:

void setMinMax(double min,double max);// 设置最大值和最小值

QColor getColor(double value);// 根据传入的值(介于最大值和最小值之间),获取颜色

注意事项:这个LColorMap 的高度不能小于343 + 20

LCOLORMAP_H

#ifndef LCOLORMAP_H
#define LCOLORMAP_H
#include <QWidget>
class LColorMap : public QWidget
    Q_OBJECT
public:
    explicit LColorMap(QWidget *parent = nullptr);
    QColor getColor(double value);
protected:
    void paintEvent(QPaintEvent *event) override;
signals:
public slots:
    void setMinMax(double min,double max);
private:
    QVector<QColor> colors;// 颜色数组
    double min = 0;
    double max = 100;
#endif // LCOLORMAP_H

LCOLORMAP_CPP

#include "lcolormap.h"
#include <QPainter>
#include <QDebug>
LColorMap::LColorMap(QWidget *parent) : QWidget(parent)
    // 计算colors
    float colorBarLength = 343.0;
    float tempLength=colorBarLength/4;
    QColor color;
    for(int i=0;i<tempLength/2;i++)// jet
        color.setRgbF(0,0,(tempLength/2+i)/tempLength);
        colors.push_back(color);
    for(int i=tempLength/2+1;i<tempLength/2+tempLength;i++)// jet
        color.setRgbF(0,(i-tempLength/2)/tempLength,1);
        colors.push_back(color);
    for(int i=tempLength/2+tempLength+1;i<tempLength/2+2*tempLength;i++)// jet
        color.setRgbF((i-tempLength-tempLength/2)/tempLength,1,(tempLength*2+tempLength/2-i)/tempLength);
        colors.push_back(color);
    for(int i=tempLength/2+2*tempLength+1;i<tempLength/2+3*tempLength;i++)// jet
        color.setRgbF(1,(tempLength*3+tempLength/2-i)/tempLength,0);
        colors.push_back(color);
    for(int i=tempLength/2+3*tempLength+1;i<colorBarLength;i++)// jet
        color.setRgbF((colorBarLength-i+tempLength/2)/(tempLength),0,0);
        if (color == QColor(0,0,0))
            qDebug()<<"black color";
        colors.push_back(color);
    qDebug()<<"colors's num: "<<colors.size();
void LColorMap::paintEvent(QPaintEvent *event)
    QPainter painter(this);
    if (height() < 343)
        return;
    for (int i =0 ;i<343;i++) {
        QRect rect(0,343-i + 20,20,1);
        painter.fillRect(rect,colors.at(i));
    for (int i = 0;i < 6;i++) {
        QString strValue;
        strValue.sprintf("%.2f",min+i*(max-min)/5);
        QFont font;
        font.setFamily("Microsoft YaHei");
        font.setPointSize(8);
        QFontMetrics fm(font);
        QRect rec = fm.boundingRect(strValue);
        //字符串所占的像素宽度,高度
        int textWidth = rec.width();
        int textHeight = rec.height();
        QRect textRect(25,343 +textHeight  - (343/5)*i,textWidth+20,textHeight);
        painter.drawText(textRect,strValue);
void LColorMap::setMinMax(double min,double max)
    this->min = min;
    this->max = max;
    update();
QColor LColorMap::getColor(double value)
    if (value > max || value <min)
        return QColor(0,0,0);
    int index = ((value-min)/(max-min))*343;
    return colors.at(index);

mian.cpp

#include <QApplication>
#include <QWidget>
#include <QPainter>
#include "lcolormap.h"
#include <QDebug>
//class PainterWidget : public QWidget
//    protected:
//    void paintEvent(QPaintEvent*);
//void PainterWidget::paintEvent(QPaintEvent *event)
//    QPainter painter(this);
//    QColor color;
//    QRect section;
//    float colorBarLength=343.0;//设置颜色条的长度
//    //------设置为gray颜色条---------//
//    for(int i=0;i<=colorBarLength;i++)// gray
//    {
//       //color.setRgbF(i/colorBarLength,i/colorBarLength,i/colorBarLength);//也可以使用这种方法
//       color.setHsv(0,0,(colorBarLength-i)/colorBarLength*255);
//        section.setRect(150,50+i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    //------设置为jet颜色条---------//
//    float tempLength=colorBarLength/4;
//    for(int i=0;i<tempLength/2;i++)// jet
//    {
//        color.setRgbF(0,0,(tempLength/2+i)/tempLength);
//        section.setRect(200,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    for(int i=tempLength/2+1;i<tempLength/2+tempLength;i++)// jet
//    {
//        color.setRgbF(0,(i-tempLength/2)/tempLength,1);
//        section.setRect(200,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    for(int i=tempLength/2+tempLength+1;i<tempLength/2+2*tempLength;i++)// jet
//    {
//        color.setRgbF((i-tempLength-tempLength/2)/tempLength,1,(tempLength*2+tempLength/2-i)/tempLength);
//        section.setRect(200,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    for(int i=tempLength/2+2*tempLength+1;i<tempLength/2+3*tempLength;i++)// jet
//    {
//        color.setRgbF(1,(tempLength*3+tempLength/2-i)/tempLength,0);
//        section.setRect(200,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    for(int i=tempLength/2+3*tempLength+1;i<colorBarLength;i++)// jet
//    {
//        color.setRgbF((colorBarLength-i+tempLength/2)/(tempLength),0,0);
//        section.setRect(200,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    //------设置为hsv颜色条---------//
//    for(int i=0;i<=colorBarLength;i++)// hsv
//    {
//        color.setHsvF(i/colorBarLength,1,1);
//        section.setRect(250,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    //------设置为hot颜色条---------//
//    tempLength=colorBarLength/2.5;
//    for(int i=0;i<tempLength/2;i++)// hot
//    {
//        color.setRgbF((tempLength/2+i)/tempLength,0,0);
//        section.setRect(300,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    for(int i=tempLength/2+1;i<tempLength/2+tempLength;i++)// hot
//    {
//        color.setRgbF(1,(i-tempLength/2)/tempLength,0);
//        section.setRect(300,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    for(int i=tempLength/2+tempLength+1;i<colorBarLength;i++)// hot
//    {
//        color.setRgbF(1,1,(i-tempLength/2-tempLength)/(colorBarLength-tempLength/2-tempLength+20));
//        section.setRect(300,colorBarLength+50-i*1,20,1);
//        painter.fillRect(section,color);
//    }
//    //---------设置边框--------------//
//    //刻度值的绘制可以自己设计,使用drawText函数即可,刻度的绘制可以使用drawLine函数
//    painter.setPen(Qt::black);
//    painter.drawRect(150,50,20,colorBarLength);
//    painter.setFont(QFont(QString::fromLocal8Bit("宋体"),10,-1,false));
//    painter.drawText(150,40,QStringLiteral("Gray"));
//    painter.drawRect(200,50,20,colorBarLength);
//    painter.setFont(QFont(QString::fromLocal8Bit("宋体"),10,-1,false));
//    painter.drawText(200,40,QStringLiteral("Jet"));
//    painter.drawRect(250,50,20,colorBarLength);
//    painter.setFont(QFont(QString::fromLocal8Bit("宋体"),10,-1,false));
//    painter.drawText(250,40,QStringLiteral("Hsv"));
//    painter.drawRect(300,50,20,colorBarLength);
//    painter.setFont(QFont(QString::fromLocal8Bit("宋体"),10,-1,false));
//    painter.drawText(300,40,QStringLiteral("Hot"));
//   // painter.drawText(150,320,QStringLiteral(" 0"));
int main(int argc, char *argv[])
    QApplication app(argc, argv);
//    PainterWidget pWidget;
//    pWidget.setWindowTitle("ColorTest");
//    pWidget.resize(500, 500);
//    pWidget.show();
    LColorMap lcolorMap;
    lcolorMap.setWindowTitle("ColorMap");
    lcolorMap.resize(500,500);
    lcolorMap.show();
    lcolorMap.setMinMax(3,9);
    qDebug()<<lcolorMap.getColor(3.2);
    return app.exec();
				
工作中用到QCustomPlot画曲线图和色阶图,并且在色阶图上添加文字,圆圈或者几线段画一些图形,这里写个简单的例子把这几个功能记录一下,代码在这里: https://download.csdn.net/download/Sakuya__/89681279https://download.csdn.net/download/Sakuya__/89681279 本文代码效果如下: MainWindow.h MainWindow.cpp
画过图的都知道,我们常常用颜色的深浅来表示值的大小,在Matlab作图中,我们使用的是colorbar这个函数来给出颜色的直观参考。下面给出Matlab的示例:在Matlab命令窗口输入: figure surf(peaks) colorbar 可以得到的图像如下: 通过右击该颜色栏,可以选择不同的颜色,当选择jet后,可以得到如下的图像: 颜色的渐变都是通过拉渐变或者是直接在paintevent中画出来的,根据hsv和rgb两种颜色类型之间的转化来实现。 重载QSlider来实现颜色花了不少时间,经过别人指点后才恍然大悟,主要还是对这些类的用法和类的实现原理不熟悉造成的,下来得好好的熟悉下。 重载QSlider实现颜色滚动的代码如下: QPainter paint