1.全部代码文件如下:
#ifndef ICONEDIT_H
#define ICONEDIT_H
#include <QColor>
#include <QImage>
#include <QWidget>
class IconEditor : public QWidget
{
Q_OBJECT
Q_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 &newColor);
QColor penColor() const { return curColor; }
void setZoomFactor(int newZoom);
int zoomFactor() const { return zoom; }
void setIconImage(const QImage newImage);
QImage iconImage() const { return image; }
QSize sizeHint() const;
QImage image;
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private:
void setImagePixel(const QPoint &pos, bool opaque);
QRect pixelRect(int i, int j) const;
QColor curColor;
int zoom;
};
#endif
#include <QtGui>
#include "iconedit.h"
IconEditor::IconEditor(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
curColor = Qt::black;
zoom =8;
image=QImage(80,80,QImage::Format_ARGB32);
image.fill (qRgba(0,0,0,0));
}
void IconEditor::setPenColor(const QColor &newColor)
{
curColor = newColor;
}
void IconEditor::setZoomFactor(int newZoom)
{
if (newZoom <</span> 1)
newZoom = 1;
if (newZoom != zoom) {
zoom = newZoom;
update();
updateGeometry();
}
}
void IconEditor::setIconImage(const QImage newImage)
{
image = newImage.convertToFormat(QImage::Format_ARGB32);
update();
updateGeometry();
}
QSize IconEditor::sizeHint() const
{
QSize size = zoom * image.size();
if (zoom >= 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) {
setImagePixel(event->pos(), false);
}
}
void IconEditor::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
setImagePixel(event->pos(), true);
} else if (event->buttons() & Qt::RightButton) {
setImagePixel(event->pos(), false);
}
}
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 <</span> image.width(); ++i) {
for (int j = 0; j <</span> 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() <</span> 255)
painter.fillRect(rect, Qt::white);
painter.fillRect(rect, color);
}
}
}
}
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));
}
}
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);
}
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include<QMainWindow>
#include<QAction>
#include<QToolBar>
#include"iconedit.h"
#include<QImage>
#include<QSize>
class MainWindow:public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent=0);
~MainWindow();
private:
IconEditor *iconEditor;
QToolBar *toolBar;
QAction *openAction;
QAction *saveAction;
void creatToolbar();
void creatAction();
QImage *newImage;
QSize sizeHint() const;
private slots:
bool save();
bool open();
};
#endif
#include"mainwindow.h"
#include
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent)
{
setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
setWindowIcon(QIcon(":images/mouse"));
setWindowTitle(tr("IconEditor"));
iconEditor=new IconEditor;
setCentralWidget(iconEditor);
resize(sizeHint());
creatAction();
creatToolbar();
}
QSize MainWindow::sizeHint ()const
{
QSize size=iconEditor->sizeHint ();
return (size+=QSize(0,40));
}
void MainWindow::creatAction()
{
openAction=new QAction(tr("&open"),this);
openAction->setIcon(QIcon(":images/open"));
openAction->setShortcut (QKeySequence::Open);
openAction->setStatusTip (tr("Open an exit Icon.png"));
connect(openAction,SIGNAL(triggered()),this,SLOT(open()));
saveAction=new QAction(tr("&save"),this);
saveAction->setIcon (QIcon(":images/save"));
saveAction->setShortcut (QKeySequence::Save);
saveAction->setStatusTip (tr("save this Icon"));
connect(saveAction,SIGNAL(triggered()),this,SLOT(save()));
}
void MainWindow::creatToolbar ()
{
toolBar=addToolBar(tr("toolBar"));
toolBar->addAction (openAction);
toolBar->addAction (saveAction);
}
bool MainWindow::open()
{
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open"), ".");
if (fileName.isEmpty())
return 0;
else{
iconEditor->image=QImage(fileName);
resize(sizeHint());
return true;
}
return 0;
}
bool MainWindow::save ()
{
QString fileName=QFileDialog::getSaveFileName (this,tr("save"),".",tr("PNG file(*.png)"));
if(fileName.isEmpty ())
return false;
else{
QFile file(fileName);
file.open (QIODevice::WriteOnly);
iconEditor->image.save (&file,"PNG");
}
}
MainWindow::~MainWindow ()
{
}
#include"mainwindow.h"
#include<QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show ();
return app.exec();
}
代码来自c++ GUI qt4 第二版,修改了一部份,添加了打开不同PNG文件和保存PNG文件的功能,十分简单。
2.代码分析:
从iconedit的构造函数开始
IconEditor::IconEditor(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);//设置widget大小被改变是其存在部分不重新绘制
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);//设置iconedit的水平和垂直最小值为sizeHint()的返回值
curColor = Qt::black;
zoom =8;
image=QImage(80,80,QImage::Format_ARGB32);
image.fill (qRgba(0,0,0,0));
}
重写了两个鼠标事件:
void IconEditor::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
setImagePixel(event->pos(), true);
} else if (event->button() == Qt::RightButton) {
setImagePixel(event->pos(), false);
}
}
void IconEditor::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
setImagePixel(event->pos(), true);
} else if (event->buttons() & Qt::RightButton) {
setImagePixel(event->pos(), false);
}
}