Hello,

I'm currently reading the "C++ GUI Programming with Qt 4"-book.

In chapter 5, you create a custom "icon editor", which enables you to edit each pixel of an icon. The icon is stored in a QImage (Format_ARGB32).

When there is a mousePressEvent, a pixel is painted.

Qt Code:
  1. void IconEditor::mousePressEvent(QMouseEvent *event)
  2. {
  3. if(event->buttons() & Qt::LeftButton) {
  4. setImagePixel(event->pos(), true);
  5. } else if(event->buttons() & Qt::RightButton) {
  6. setImagePixel(event->pos(), false);
  7. }
  8. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. void IconEditor::setImagePixel(const QPoint &pos, bool opaque)
  2. {
  3. int i = pos.x() / zoom;
  4. int j = pos.y() / zoom;
  5.  
  6.  
  7. if(image.rect().contains(i, j)) {
  8. QRgb pixelColor = image.pixel(i, j);
  9. if(opaque) {
  10. image.setPixel(i, j, penColor().rgba());
  11. } else {
  12. image.setPixel(i, j, qRgba(0, 0, 0, 0));
  13. }
  14.  
  15. update(pixelRect(i, j));
  16. }
  17. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. void IconEditor::paintEvent(QPaintEvent *event)
  2. {
  3. QPainter painter(this);
  4.  
  5. for(int i = 0; i < image.width(); ++i) {
  6. for(int j = 0; j < image.height(); ++j) {
  7. QRect rect = pixelRect(i, j);
  8. if(!event->region().intersect(rect).isEmpty()) {
  9. QColor color = QColor::fromRgba(image.pixel(i, j));
  10. if(color.alpha() < 255)
  11. painter.fillRect(rect, color);
  12. painter.fillRect(rect, color);
  13. }
  14. }
  15. }
  16. }
To copy to clipboard, switch view to plain text mode 

It works, but unfortunately very slow. Especially when the images are quite large. Then it takes some seconds to draw a pixel and it is inpossible to draw a continuous line. How is this solved in "real" image processing applications?


And sorry for my bad english.