看qt书的第一个作品

  最近看了《c++ GUI qt4编程》,做了个简单的程序。。。

是第五章的“创建自定义窗口部件”,是一个图标编辑器。。。

书上只能一个一个的点,我另外加了点功能,就是可以拖动,并且颜色会随机改变。。。

代码如下:

//        iconeditor.h
#ifndef ICONEDITOR_H
#define ICONEDITOR_H 
 
 
 
 
#include <QColor>#include <QImage>#include <QWidget> 
 
 
 
 
class IconEditor:public QWidget{Q_OBJECTQ_PROPERTY(QColor penColor READ penColor WRITE setPenColor)Q_PROPERTY(QImage iconImage READ iconImage WRITE setIconImage)Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setZoomFactor)public:IconEditor(QWidget *parent=0); 
 
 
 
 
void setPenColor(QColor const& newColor);QColor penColor() const{return curColor;}void setZoomFactor(int newZoom);int zoomFactor() const{return zoom;}void setIconImage(QImage const& newImage);QImage iconImage() const{return image;}QSize sizeHint() const;protected:void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void paintEvent(QPaintEvent *event);private:void setImagePixel(QPoint const& pos,bool opaque);QRect pixelRect(int i,int j) const; 
 
 
 
 
QColor curColor;QImage image;int zoom;bool s; 
 
 
 
 
}; 
 
 
 
 
#endif // ICONEDITOR_H 
 
 
 
 
//         iconeditor.cpp
#include <QtGui>
#include "iconeditor.h"#include "QTime" 
 
 
 
 
IconEditor::IconEditor(QWidget *parent):QWidget(parent){setWindowIcon(QIcon(":/title.png"));setAttribute(Qt::WA_StaticContents);setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum); 
 
 
 
 
curColor=Qt::green;zoom=8; 
 
 
 
 
image=QImage(100,100,QImage::Format_ARGB32);image.fill(qRgba(0,0,0,0));}QSize IconEditor::sizeHint() const{QSize size=zoom*image.size();if(zoom>=3)size+=QSize(1,1);return size; 
 
 
 
 
}void IconEditor::setPenColor(QColor const& newColor){curColor=newColor;}void IconEditor::setIconImage(const QImage &newImage){if(newImage!=image){image=newImage.convertToFormat(QImage::Format_ARGB32);update();updateGeometry();}}void IconEditor::setZoomFactor(int newZoom){if(newZoom<1)newZoom=1;if(newZoom!=zoom){zoom=newZoom;update();updateGeometry();}}void IconEditor::paintEvent(QPaintEvent *event){QPainter painter(this);if(zoom>=3){painter.setPen(palette().foreground().color());for(int i=0;i<=image.width();++i)painter.drawLine(zoom*i,0,zoom*i,zoom*image.height());for(int j=0;j<=image.height();++j)painter.drawLine(0,zoom*j,zoom*image.width(),zoom*j);}for(int i=0;i<image.width();++i)for(int j=0;j<image.height();++j){QRect rect=pixelRect(i,j);if(!event->region().intersect(rect).isEmpty()){QColor color=QColor::fromRgba(image.pixel(i,j));if(color.alpha()<255)painter.fillRect(rect,Qt::white);painter.fillRect(rect,color);}}}QRect IconEditor::pixelRect(int i, int j) const{if(zoom>=3){return QRect(zoom*i+1,zoom*j+1,zoom-1,zoom-1);}else{return QRect(zoom*i,zoom*j,zoom,zoom);}}void IconEditor::mousePressEvent(QMouseEvent *event){s=true;QTime t;t= QTime::currentTime();qsrand(t.msec()+t.second()*1000);//int n = qrand(); 
 
 
 
 
if(event->button()==Qt::LeftButton){setImagePixel(event->pos(),true);curColor=QColor(qrand()%255,qrand()%255,qrand()%255,qrand()%255);}else if(event->button()==Qt::RightButton){setImagePixel(event->pos(),false);}}void IconEditor::mouseReleaseEvent(QMouseEvent *event){s=false;}void IconEditor::mouseMoveEvent(QMouseEvent *event){if(s==true)setImagePixel(event->pos(),true); 
 
 
 
 
if(event->button()&Qt::LeftButton){setImagePixel(event->pos(),true);}else if(event->button()&Qt::RightButton){setImagePixel(event->pos(),false);}}void IconEditor::setImagePixel(const QPoint &pos, bool opaque){int i=pos.x()/zoom;int j=pos.y()/zoom;if(image.rect().contains(i,j)){if(opaque){image.setPixel(i,j,penColor().rgba());}else{image.setPixel(i,j,qRgba(0,0,0,0));}update(pixelRect(i,j));}}