1. 程式人生 > >【Qt程式設計】設計ColorBar顏色欄

【Qt程式設計】設計ColorBar顏色欄

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