用Qt绘制颜色渐变

最近一直在看关于Qt开发库的东西,研究了一些基本使用,写了一个关于绘制颜色渐变的小程序,现在共同和大家分享

声明一下,我是在linux下面用eclipse+CDT+qt开发的,感觉非常的好:-)


下面是PaintWidget.h头文件的声明

 1#ifndef PAINTWIDGET_H_
 2#define PAINTWIDGET_H_
 3#include <QWidget>
 4#include <QPainter>
 5class PaintWidget:public QWidget
 6{
 7public:
 8    PaintWidget(QWidget *parent=0);
 9protected:
10    void paintEvent(QPaintEvent *event);
11}
;
12
13#endif /* PAINTWIDGET_H_ */

PaintWidget.cpp文件

 

 1#include "PaintWidget.h"
 2
 3PaintWidget::PaintWidget(QWidget *parent):QWidget(parent)
 4{
 5
 6}

 7void PaintWidget::paintEvent(QPaintEvent */*event*/)
 8{
 9    QPainter pt(this);
10    QColor color(0x00,0x00,0x00);
11    for (int i=0;i<256;i++)
12    {
13        for (int j=0;j<256;j++)
14        {
15            color.setRgb(0xff,i,j,0xff);
16            pt.setPen(color);
17            pt.drawPoint(20+i,20+j);
18            color.setRgb(i,0xff,j,0xff);
19            pt.setPen(color);
20            pt.drawPoint(296+i,20+j);
21            color.setRgb(j,i,0xff,0xff);
22            pt.setPen(color);
23            pt.drawPoint(572+i,20+j);
24        }

25    }

26}

 

 1/*
 2 *
 3 *
 4 *  Created on: 2008-11-13
 5 *      Author: zhanweisun
 6 */

 7#include <QApplication>
 8#include "PaintWidget.h"
 9int main(int argc, char *argv[])
10{
11    QApplication a(argc, argv);
12    PaintWidget  widget;
13    widget.show();
14    return a.exec();
15}

16

 

posted on 2008-11-13 19:37  流浪猫  阅读(1783)  评论(0编辑  收藏  举报

导航