QT学习笔记之 IconEditor

这次写了一个简单的Icon编辑器,功能很简单的说.....详见代码

 

=================================================================

 

iconeditor.h

 

  1. #ifndef ICONEDITOR_H  
  2. #define ICONEDITOR_H  
  3. #include <QtGui/QWidget>  
  4. #include <QImage>  
  5. #include <QWidget>  
  6. class IconEditor : public QWidget  
  7. {  
  8.     Q_OBJECT  
  9.     Q_PROPERTY(QColor penColor READ penClor WRITE setPenColor);  
  10.     Q_PROPERTY(QImage iconImage READ iconImage WRITE setIconImage);  
  11.     Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setZoomFactor);  
  12. public:  
  13.     IconEditor(QWidget *parent = 0);  
  14.     ~IconEditor();  
  15.     void setPenColor(const QColor &newColor);  
  16.     QColor penColor();  
  17.     void setZoomFactor(int newZoom);  
  18.     int zoomFactor() const;  
  19.     void setIconImage(const QImage &newImage);  
  20.     QImage iconImage() const;  
  21.     QSize sizeHint() const;  
  22. protected:  
  23.     void mousePressEvent(QMouseEvent *);  
  24.     void mouseMoveEvent(QMouseEvent *);  
  25.     void paintEvent(QPaintEvent *event);  
  26. private:  
  27.     void setImagePixel(const QPoint &pos,bool opaque);  
  28.     QRect pixelRect(int i,int j) const;  
  29.     QColor curColor;  
  30.     QImage image;  
  31.     int zoom;  
  32. };  
  33. #endif // ICONEDITOR_H  
  34. /* 
  35. Q_PROPERTY用法: 
  36. Q_PROPERTY(type name 
  37.                     READ getFunction 
  38.                     [WRITE setFunction] 
  39.                     [RESET resetFunction] 
  40.                     [DESIGNABLE bool] 
  41.                     [SCRIPTABLE bool] 
  42.                     [STORED bool] 
  43.                     [USER bool]) 
  44. */  
 

 

iconeditor.cpp

 

  1. #include<QtGui>  
  2. #include "iconeditor.h"  
  3. IconEditor::IconEditor(QWidget *parent)  
  4.     : QWidget(parent)  
  5. {  
  6.     setAttribute(Qt::WA_StaticContents);  
  7.     setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);  
  8.     curColor=Qt::black;  
  9.     zoom=8;  
  10.     image=QImage(16,16,QImage::Format_ARGB32);  
  11.     image.fill(qRgba(0,0,0,0));  
  12. }  
  13. /* 
  14. Qt::WA_StaticContents 用法: 
  15. Indicates that the widget contents are north-west aligned and static. 
  16. On resize, such a widget will receive paint events only for parts of itself 
  17. that are newly visible. This flag is set or cleared by the widget's author. 
  18. */  
  19. IconEditor::~IconEditor()  
  20. {  
  21. }  
  22. void IconEditor::setPenColor(const QColor &newColor)  
  23. {  
  24.     curColor=newColor;  
  25. }  
  26. QColor IconEditor::penColor()  
  27. {  
  28.     return curColor;  
  29. }  
  30. void IconEditor::setZoomFactor(int newZoom)  
  31. {  
  32.     if(newZoom<1)  
  33.         newZoom=1;  
  34.     if(newZoom!=zoom){  
  35.         zoom=newZoom;  
  36.         this->update();  
  37.         this->updateGeometry();    //调用update和updateGeometry进行重绘  
  38.     }  
  39. }  
  40. int IconEditor::zoomFactor() const  
  41. {  
  42.     return zoom;  
  43. }  
  44. void IconEditor::setIconImage(const QImage &newImage)  
  45. {  
  46.     if(newImage!=image){  
  47.         image=newImage.convertToFormat(QImage::Format_ARGB32);  
  48.         this->update();  
  49.         this->updateGeometry();  
  50.     }  
  51. }  
  52. QImage IconEditor::iconImage() const  
  53. {  
  54.     return image;  
  55. }  
  56. QSize IconEditor::sizeHint() const  
  57. {  
  58.     QSize size=zoom*image.size();  
  59.     if(zoom>=3)  
  60.         size+=QSize(1,1);    //当zoom>=3时,增加一个像素用存放网格线  
  61.     return size;  
  62. }  
  63. /*只要重绘就会调用这个函数*/  
  64. void IconEditor::paintEvent(QPaintEvent *event)  
  65. {  
  66.     QPainter painter(this);  
  67.     if(zoom>=3){  
  68.         painter.setPen(palette().foreground().color());  
  69.         //绘制垂直表格线  
  70.         for(int i=0;i<=image.height();++i)  
  71.             painter.drawLine(zoom*i,0,zoom*image.width(),zoom*i);   //(x1,y1) to (x2,y2)  
  72.         //绘制水平表格线  
  73.         for(int j=0;j<image.width();++j)  
  74.             painter.drawLine(0,zoom*j,zoom*image.width(),zoom*j);  
  75.     }  
  76.     for(int i=0;i<image.width();++i){  
  77.         for(int j=0;j<image.height();++j){  
  78.             QRect rect=pixelRect(i,j);  
  79.             if(!event->region().intersect(rect).isEmpty()){   //region区域  
  80.                 QColor color=QColor::fromRgba(image.pixel(i,j));   //pixel(x,y)返回(x,y)的一个像素  
  81.                 if(color.alpha()<255)  
  82.                     painter.fillRect(rect,Qt::white);  
  83.                 painter.fillRect(rect,color);  
  84.             }  
  85.         }  
  86.     }  
  87. }  
  88. QRect IconEditor::pixelRect(int i,int j) const  
  89. {  
  90.     if(zoom>=3){  
  91.         return QRect(zoom*i+1,zoom*j+1,zoom-1,zoom-1);  
  92.     }else{  
  93.         return QRect(zoom*i,zoom*j,zoom,zoom);  //QRect其中一种构造函数,QRect ( int x, int y, int width, int height )  
  94.     }  
  95. }  
  96. void IconEditor::mousePressEvent(QMouseEvent *event)  
  97. {  
  98.     if(event->button()==Qt::LeftButton){  
  99.         setImagePixel(event->pos(),true);  
  100.     }else if(event->button()==Qt::RightButton){  
  101.         setImagePixel(event->pos(),false);  
  102.     }  
  103. }  
  104. void IconEditor::mouseMoveEvent(QMouseEvent *event)  
  105. {  
  106.     if(event->buttons()& Qt::LeftButton){    //用Qt::LeftButton与buttons位或  
  107.         setImagePixel(event->pos(),true);  
  108.     }else if(event->buttons()& Qt::RightButton){  
  109.         setImagePixel(event->pos(),false);  
  110.     }  
  111. }  
  112. void IconEditor::setImagePixel(const QPoint &pos,bool opaque)  
  113. {  
  114.     int i=pos.x()/zoom;  
  115.     int j=pos.y()/zoom;  
  116.     if(image.rect().contains(i,j)){  
  117.         if(opaque){  
  118.             image.setPixel(i,j,penColor().rgba());  
  119.         }else{  
  120.             image.setPixel(i,j,qRgba(0,0,0,0));  
  121.         }  
  122.     }  
  123.         update(pixelRect(i,j));  
  124. }  
 

 

main.cpp

  1. #include <QtGui/QApplication>  
  2. #include "iconeditor.h"  
  3. int main(int argc, char *argv[])  
  4. {  
  5.     QApplication app(argc, argv);  
  6.     IconEditor *iconEditor=new IconEditor;  
  7.     iconEditor->setWindowTitle("Icon Editor");  
  8.     iconEditor->setIconImage(QImage(":/images/qt-logo.png"));  
  9.     iconEditor->show();  
  10.     return app.exec();  
  11. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值