1、Iconeditor.cpp#include #include “iconeditor.h“IconEditor:IconEditor(QWidget *parent): QWidget(parent)setAttribute(Qt:WA_StaticContents);setSizePolicy(QSizePolicy:Minimum, QSizePolicy:Minimum);curColor = Qt:black;zoom = 8;image = QImage(16, 16, QImage:Format_ARGB32);image.fill(qRgba(0, 0, 0, 0);void
2、IconEditor:setPenColor(const QColor void IconEditor:setZoomFactor(int newZoom)if (newZoom = 3)size += QSize(1, 1);return size;void IconEditor:mousePressEvent(QMouseEvent *event)if (event-button() = Qt:LeftButton) setImagePixel(event-pos(), true); else if (event-button() = Qt:RightButton) setImagePix
3、el(event-pos(), false);void IconEditor:mouseMoveEvent(QMouseEvent *event)if (event-buttons() else if (event-buttons() void IconEditor:paintEvent(QPaintEvent *event)QPainter painter(this);if (zoom = 3) painter.setPen(palette().foreground().color();for (int i = 0; i region().intersect(rect).isEmpty()
4、QColor color = QColor:fromRgba(image.pixel(i, j);if (color.alpha() = 3) return QRect(zoom * i + 1, zoom * j + 1, zoom - 1, zoom - 1); else return QRect(zoom * i, zoom * j, zoom, zoom);Iconeditor.h#ifndef ICONEDITOR_H#define ICONEDITOR_H#include #include #include class IconEditor : public QWidgetQ_OB
5、JECTQ_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(const QColor QColor penColor() const return curColor; voi
6、d setZoomFactor(int newZoom);int zoomFactor() const return zoom; void setIconImage(const QImage QImage iconImage() const return image; QSize sizeHint() const;protected:void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void paintEvent(QPaintEvent *event);private:void se
7、tImagePixel(const QPoint QRect pixelRect(int i, int j) const;QColor curColor;QImage image;int zoom;#endifMain.cpp#include #include “iconeditor.h“int main(int argc, char *argv)QApplication app(argc, argv);IconEditor iconEditor;iconEditor.setWindowTitle(QObject:tr(“Icon Editor“);iconEditor.setIconImage(QImage(“:/images/mouse.png“);iconEditor.show();return app.exec();