画过图的都知道,我们常常用颜色的深浅来表示值的大小,在Matlab作图中,我们使用的是colorbar这个函数来给出颜色的直观参考。下面给出Matlab的示例:在Matlab命令窗口输入:

figure
surf(peaks)
colorbar

可以得到的图像如下:

通过右击该颜色栏,可以选择不同的颜色,当选择jet后,可以得到如下的图像:

那上面的示例来说,使用颜色栏的好处是可以显示四维信息,例如(x,y,z)表示了一个三维空间的坐标,坐标点温度的大小则可以通过颜色栏的温度来表明。当然,要说明的是这里的温度值的大小和高度z的值是相同的,这个例子没举好,若要画四维图可以自行百度。

上面讲了Matlab如何运用颜色栏以及其好处,下面我们看看如何在Qt中绘制颜色条。本以为Qt中也有类似的函数,可以我没有找到(如果谁知道,可以告知我),只好自己写函数实现了。关于Qt中最基本的使用QPaint画图我就不介绍了,网上也有很多教程。程序中我只是将Matlab中Colorbar常用的四种颜色栏(Gray,Jet,Hsv,Hot)进行了绘制。绘制过程只使用到了简单的fillRect函数来画填充四边形。下面主要讲讲颜色的设置:
我们首先在上面Matlab的Colorbar上右击选择一个你希望绘制的颜色栏(假设选择了jet),然后选择“打开颜色图编辑器”,得到如下界面:

将鼠标放在颜色上,就可以得到对应的RGB、HSV的值。然后在Qt中就可以通过程序描绘这种值的变化,就可以得到与之相同的颜色栏了。注意:在程序中,你可以任意选择RGB或HSV来描述,我在程序中,两种方式都用到了。

为了方便,我将工程放着一个.cpp文件中,因此只需要建立一个空的Qt项目然后添加下面的.cpp文件就可以了,具体的程序实现如下:
#include <QApplication>
#include <QWidget>
#include <QPainter>

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();
    return app.exec();
}


运行结果如下图:




原文:http://blog.csdn.net/tengweitw/article/details/44957601

作者:nineheadedbird

【Qt编程】设计ColorBar颜色栏的更多相关文章

  1. 零基础学QT编程

    吴迪.2010.1 北京航空航天大学出版社   Qt资源 CSDN QT http://bbs.csdn.net/forums/Qt/ QT编程网 http://www.qtbcw.com/ 编程论坛 ...

  2. QT Desinger设计窗体应用程序框架

    目录 目录 前言 系统软件 QT Designer Using QT Designer Open QTDesigner Tool Widget Box QT Designer的布局 属性栏 示例 i ...

  3. qt编程入门

    面对qt编程,必须先知道qt中常用的类: QPushButton按钮类.QLabel标签类.QMessageBox对话框类.QCheckBox.QAction.QMenu.QStatusBar.QTo ...

  4. Visual Studio下Qt编程中对中文的处理

    Visual Studio下Qt编程中对中文的处理 本文为原创文章,原文地址http://www.cnblogs.com/c4isr/p/qt_develop_in_vs.html Visual St ...

  5. QT笔记(1)--QT编程环境搭建

    一.QT简介 Qt  是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊 ...

  6. Qt编程学习网站

    http://blog.csdn.net/k122769836/article/details/8637677 QT - little_su - 博客频道 - CSDN.NET Qt - 1+1=2 ...

  7. 调色板类QPalette——包含了Qt窗口不见的颜色组(collor group),和Windows右键属性外观非常类似

    QPalette类包含了Qt窗口不见的颜色组(collor group); 1.Active组,该组的颜色用户当前活动的(active)窗口,即具有键盘或鼠标焦点的窗口; 2.Inactive组,该组 ...

  8. 在windows下的QT编程中的_TCHAR与QString之间的转换

    由于在windows下的QT编程中,如果涉及到使用微软的API,那么不可避免使用_TCHAR这些类型,因此在网上查了一下,其中一个老外的论坛有人给出了这个转换,因此在这里做一下笔记 : )#ifdef ...

  9. IT第十天 - String和StringBuffer的比较、编程设计技巧整理、本周总结 ★★★

    IT第十天 上午 String 1.String在进行多次的+扩展时,会严重的降低处理效率,因为String长度是不可变的,在进行+运算改变字符串时,会自动创建很多临时字符串,并不是在原字符串上追加, ...

随机推荐

  1. 使用kprobes查看内核内部信息

    前言:使用printk打印变量等方法,是调试内核的有效方法之一,但是这种方法必须重新构建并用新内核启动,调试效率比较低.以内核模块的方式使用kprobes.jprobes,就可以在任意地址插入侦测器, ...

  2. 关于在arm裸板编程时使用printf问题的解决方法

    在ARM裸板驱动编程中,是不允许程序直接调用C库程序的.为什么呢?因为此时kernel还没有被加载,所以在封装在kernel层的C库的API是用不了的,那怎么办? 在开发过程中,printf的功能我不 ...

  3. Hexo 简明教程

    概述 对于个人独立博客的搭建,或者一些产品网站的介绍我个人比较推崇直接用静态网站生成器来完成这个事情,对于,静态网页部署方便,浏览速度快. 以下为部分静态网站生成器简要列表: Ruby Jekyll ...

  4. windows curl命令详解

    概述 Curl命令可以通过命令行的方式,执行Http请求.在Elasticsearch中有使用的场景,因此这里研究下如何在windows下执行curl命令. 软件下载 下载地址:https://cur ...

  5. [shiro学习笔记]第四节 使用源代码生成Shiro的CHM格式的API文档

    版本为1.2.3的shiro API chm个事故文档生成. 获取shiro源代码 编译生成API文档 转换成chm格式 API 获取shiro源代码 shiro官网: http://shiro.ap ...

  6. 将String转换为其表示的路径画到屏幕上

    关于这个问题,我已经在另一篇blog中有所提及: CoreText精彩文字轮廓绘制动画的一点改进 不过原有的转换代码使用Obj-C写的,在这里我们尝试将其转换为Swift语言,然后利用它实现一个测试小 ...

  7. Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)

    demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...

  8. linux crontab定时任务详解

    1.  为当前用户创建cron服务: crontab  -e 例如 文件内容如下(每隔1分钟执行sql脚本): */1 * * * * mysql -h127.0.0.1 -uroot -proot ...

  9. (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 前一篇博文介绍了物理对象中小球与砖块的碰撞处理,在这一篇中我们再 ...

  10. UNIX网络编程——套接字选项(setsockopt)

    setsockopt的一些用法: close socket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket: BOOL bReuseaddr=TRUE; setsockop ...