"QT Programming" design Colorbar color Bar

Source: Internet
Author: User
Tags drawtext

We all know that we often use the shades of color to denote the size of values, and in MATLAB we use the Colorbar function to give a visual reference to the color. Here's an example of Matlab: in the MATLAB command window, enter: FigureSurf (peaks)Colorbar

The images you can get are as follows:


By right-clicking the color bar, you can choose a different color, and when Jet is selected, you can get the following image:


In the example above, the advantage of using a color bar is that you can display four-dimensional information, such as (x, Y, z), which represents a coordinate of three dimensions, and the temperature of the sitting punctuation can be indicated by the temperature of the color bar. Of course, to illustrate that the temperature value here is the same size and height Z value is the same, this example did not lift, to draw a four-dimensional map can be Baidu itself.
It says how Matlab uses the color bar and its benefits, so let's look at how to draw a color bar in Qt. I thought that there were similar functions in QT, I could not find (if anyone knows, I can tell me), I had to write my own function. About the most basic use of QT Qpaint drawing I do not introduce, there are many tutorials on the web. In the program I just draw the four color bars (gray,jet,hsv,hot) that colorbar commonly used in MATLAB. The drawing process uses only simple fillrect functions to draw the filled quadrilateral. The following is mainly about color settings: We first right-click on the colorbar above matlab to select a color bar you want to draw (assuming Jet is selected), and then choose "Open Color Map Editor", get the following interface:

put the mouse on the color, you can get the corresponding RGB, HSV value. Then in QT you can use the program to describe the changes in this value, you can get the same color bar. Note: In the program, you can choose either RGB or HSV to describe, I in the program, both methods are used.
for convenience, I put the project in a. cpp file, so just create an empty QT project and then add the following. cpp file to it, the specific program is implemented as follows:
#include <QApplication> #include <QWidget> #include <qpainter>class painterwidget:public qwidget{PR Otected:void paintevent (qpaintevent*);};    void Painterwidget::p aintevent (qpaintevent *event) {qpainter painter (this);    Qcolor color;    Qrect section;    Float colorbarlength=343.0;//Set the length of the color bar//------set to Gray color bar---------//for (int i=0;i<=colorbarlength;i++)//Gray {//COLOR.SETRGBF (i/colorbarlength,i/colorbarlength,i/colorbarlength);//You can also use this method COLOR.SETHSV (0,0, (CO        lorbarlength-i)/colorbarlength*255);        Section.setrect (150,50+i*1,20,1);    Painter.fillrect (Section,color);    }//------set to Jet color bar---------//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, (TempLeng        th*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+temp        LENGTH/2)/(Templength), 0,0);        Section.setrect (200,colorbarlength+50-i*1,20,1);    Painter.fillrect (Section,color); }//------set to HSV color bar---------//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);    }//------set to Hot color bar---------//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)/tempLen        gth,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 (i-templength/2-temp        Length)/(colorbarlength-templength/2-templength+20));        Section.setrect (300,colorbarlength+50-i*1,20,1);    Painter.fillrect (Section,color); }//---------Set the Border--------------////Scale valueDrawing can be designed by itself, using the DrawText function, the scale can be drawn using the DrawLine function Painter.setpen (qt::black);    Painter.drawrect (150,50,20,colorbarlength);    Painter.setfont (Qfont (qstring::fromlocal8bit ("Arial"), 10,-1,false);    Painter.drawtext (150,40,qstringliteral ("Gray"));    Painter.drawrect (200,50,20,colorbarlength);    Painter.setfont (Qfont (qstring::fromlocal8bit ("Arial"), 10,-1,false);    Painter.drawtext (200,40,qstringliteral ("Jet"));    Painter.drawrect (250,50,20,colorbarlength);    Painter.setfont (Qfont (qstring::fromlocal8bit ("Arial"), 10,-1,false);    Painter.drawtext (250,40,qstringliteral ("HSV"));    Painter.drawrect (300,50,20,colorbarlength);    Painter.setfont (Qfont (qstring::fromlocal8bit ("Arial"), 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 ();}



Running results such as:


Original: http://blog.csdn.net/tengweitw/article/details/44957601

Nineheadedbird


"QT Programming" design Colorbar color Bar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.