Qt jet颜色条

 

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();
}

 

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Qt中的QSlider滑动控件和QColorDialog颜色选择对话框实现控制颜色值的功能。 首先,在Qt Designer中创建一个滑动和一个按钮,然后将它们放在窗口上。 接着,在Qt Creator中打开窗口类的头文件,添加如下代码: ```cpp #include <QSlider> #include <QColorDialog> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: QSlider *slider; QPushButton *button; QColor color; private slots: void setColor(); void onSliderValueChanged(int value); }; ``` 在MainWindow类的构造函数中,创建一个QSlider和一个QPushButton,并将它们添加到窗口中。设置滑动的最小值为0,最大值为255,初始值为0。将QPushButton的文本设置为“Choose Color”。 ```cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { slider = new QSlider(Qt::Horizontal, this); slider->setMinimum(0); slider->setMaximum(255); slider->setValue(0); connect(slider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int))); button = new QPushButton("Choose Color", this); connect(button, SIGNAL(clicked()), this, SLOT(setColor())); setCentralWidget(slider); setStatusBar(button); color = QColor(0, 0, 0); } ``` 在MainWindow类中添加两个私有槽函数setColor()和onSliderValueChanged(),用于设置颜色和响应滑动值的改变。 在setColor()函数中,使用QColorDialog对话框获取用户选择的颜色,并将其保存到color变量中。 ```cpp void MainWindow::setColor() { QColorDialog dialog(color, this); dialog.exec(); color = dialog.selectedColor(); } ``` 在onSliderValueChanged()函数中,将滑动的值转换为RGB颜色值,并将其设置为窗口的背景色。 ```cpp void MainWindow::onSliderValueChanged(int value) { int r = qRed(color.rgb()); int g = qGreen(color.rgb()); int b = qBlue(color.rgb()); if (slider == sender()) { r = value; } QColor newColor(r, g, b); setStyleSheet(QString("background-color: %1").arg(newColor.name())); } ``` 最后,在MainWindow类的析构函数中释放QSlider和QPushButton的内存。 ```cpp MainWindow::~MainWindow() { delete slider; delete button; } ``` 这样,就可以使用滑动控制颜色值了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值