【Qt编程】设计ColorBar颜色栏
画过图的都知道,我们常常用颜色的深浅来表示值的大小,在Matlab作图中,我们使用的是colorbar这个函数来给出颜色的直观参考。下面给出Matlab的示例:在Matlab命令窗口输入:
可以得到的图像如下:
通过右击该颜色栏,可以选择不同的颜色,当选择jet后,可以得到如下的图像:
#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颜色栏的更多相关文章
- 零基础学QT编程
吴迪.2010.1 北京航空航天大学出版社 Qt资源 CSDN QT http://bbs.csdn.net/forums/Qt/ QT编程网 http://www.qtbcw.com/ 编程论坛 ...
- QT Desinger设计窗体应用程序框架
目录 目录 前言 系统软件 QT Designer Using QT Designer Open QTDesigner Tool Widget Box QT Designer的布局 属性栏 示例 i ...
- qt编程入门
面对qt编程,必须先知道qt中常用的类: QPushButton按钮类.QLabel标签类.QMessageBox对话框类.QCheckBox.QAction.QMenu.QStatusBar.QTo ...
- Visual Studio下Qt编程中对中文的处理
Visual Studio下Qt编程中对中文的处理 本文为原创文章,原文地址http://www.cnblogs.com/c4isr/p/qt_develop_in_vs.html Visual St ...
- QT笔记(1)--QT编程环境搭建
一.QT简介 Qt 是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊 ...
- Qt编程学习网站
http://blog.csdn.net/k122769836/article/details/8637677 QT - little_su - 博客频道 - CSDN.NET Qt - 1+1=2 ...
- 调色板类QPalette——包含了Qt窗口不见的颜色组(collor group),和Windows右键属性外观非常类似
QPalette类包含了Qt窗口不见的颜色组(collor group); 1.Active组,该组的颜色用户当前活动的(active)窗口,即具有键盘或鼠标焦点的窗口; 2.Inactive组,该组 ...
- 在windows下的QT编程中的_TCHAR与QString之间的转换
由于在windows下的QT编程中,如果涉及到使用微软的API,那么不可避免使用_TCHAR这些类型,因此在网上查了一下,其中一个老外的论坛有人给出了这个转换,因此在这里做一下笔记 : )#ifdef ...
- IT第十天 - String和StringBuffer的比较、编程设计技巧整理、本周总结 ★★★
IT第十天 上午 String 1.String在进行多次的+扩展时,会严重的降低处理效率,因为String长度是不可变的,在进行+运算改变字符串时,会自动创建很多临时字符串,并不是在原字符串上追加, ...
随机推荐
- Android动态修改ToolBar的Menu菜单
Android动态修改ToolBar的Menu菜单 效果图 实现 实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,把对应的Action隐藏了. 开始上货 Menu Me ...
- sorted函数返回一个新的列表就安全了吗?
arr=[[1,2,3],[4,2,3],[5,2,3]] x2=sorted(arr) print 'sorted',x2 print '-'*20 for ar in arr: ar.append ...
- Lucene 查询(Query)子类
QueryParser(单域查询) QueryParser子类对单个域查询时创建查询query,构造方法中需要传入Lucene版本号,检索域名和分词器. QueryParser parser = ne ...
- Docker与容器
Docker介绍 1. Docker 主要解决什么问题 Docker 对外宣称的是Build.Ship 和Run,Docker 要解决的核心问题就是快速地干这三件事情.它通过将运行环境和应用程序打包到 ...
- Android自定义ViewGroup(四、打造自己的布局容器)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51500304 本文出自:[openXu的博客] 目录: 简单实现水平排列效果 自定义Layo ...
- SceneKit:简单的3D游戏场景搭建
SceneKit是Apple用来开发休闲3D游戏的框架,不同于底层的OpenGL库,你仅仅需要很少的代码就可以快速看到实际的3D场景效果.下面简单的聊聊搭建一个3D游戏场景需要做的事情. 首先你必须用 ...
- 剑指offer-面试题7:俩个栈实现队列(java)
详细分析请参照C语言版,这里仅仅给出实现代码,注释很详细,不得不说java各种api用起来真是爽飞了 1 package com.xsf.SordForOffer; 2 3 import java.u ...
- Redis 4.0新功能介绍
Redis 的作者 antirez 在三天之前通过博客文章<The first release candidate of Redis 4.0 is out>发布了 Redis 4.0 的第 ...
- SQL Server 索引维护(1)——如何获取索引使用情况
前言: 在前面一文中,已经提到了三类常见的索引问题,那么问题来了,当系统出现这些问题时,该如何应对? 简单而言,需要分析现有系统的行为,然后针对性地对索引进行处理: 对于索引不足的情况:检查缺少索引的 ...
- Microsoft Dynamics CRM 2011 当您在 大型数据集上执行 RetrieveMultiple 查询很慢的解决方法
症状 当您在 Microsoft Dynamics CRM 2011 年大型数据集上执行 RetrieveMultiple 查询时,您会比较慢. 原因 发生此问题是因为大型数据集缓存 Retrieve ...