- 
UID:81914 
 
- 
- 注册时间2009-07-09
 
- 最后登录2009-08-16
 
- 在线时间5小时
 
 
 
- 
- 发帖15
 
- 搜Ta的帖子
 
- 精华0
 
- 金钱150
 - 威望25
 - 贡献值0
 - 好评度15
 
 
 
- 
访问TA的空间加好友用道具
 
 
 
 
 
 
  
 
 | 
 
 
代码如下,这是我运行的结果,怎么觉得不对呀。    - //iconeditor.h 
 - #ifndef ICONEDITOR_H 
 - #define ICONEDITOR_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; 
 - 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; 
 - QImage image; 
 - int zoom; 
 - }; 
 - #endif 
 - //iconeditor.cpp 
 - #include <QtGui> 
 - #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 IconEditor::setPenColor(const QColor &newColor) 
 - { 
 - curColor = newColor; 
 - } 
 - void IconEditor::setZoomFactor(int newZoom) 
 - { 
 - if (newZoom < 1) 
 - newZoom = 1; 
 - if (newZoom != zoom) { 
 - zoom = newZoom; 
 - update(); 
 - updateGeometry(); 
 - } 
 - } 
 - void IconEditor::setIconImage(const QImage &newImage) 
 - { 
 - if (newImage != image) { 
 - 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 < 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); 
 - } 
 - } 
 - } 
 - } 
 - 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); 
 - } 
 - } 
 - //main.cpp 
 - #include <QApplication> 
 - #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(); 
 - } 
 
  
 
 
 |