I have this code:
Qt Code:
  1. QImage grayImage = image.convertToFormat(QImage::Format_Grayscale8);
  2. int size = grayImage.width() * grayImage.height();
  3. QRgb *data = new QRgb[size];
  4. memmove(data, grayImage.constBits(), size * sizeof(QRgb));
  5.  
  6. QRgb *ptr = data;
  7. QRgb *end = ptr + size;
  8. for (; ptr < end; ++ptr) {
  9. int gray = qGray(*ptr);
  10. }
  11.  
  12. delete[] data;
To copy to clipboard, switch view to plain text mode 
It is based on this: https://stackoverflow.com/a/40740985/8257882

How can I set the color of a pixel using that pointer?

In addition, using qGray() and loading a "bigger" image seem to crash this.

This works:
Qt Code:
  1. int width = image.width();
  2. int height = image.height();
  3. for (int y = 0; y < height; ++y) {
  4. for (int x = 0; x < width; ++x) {
  5. image.setPixel(x, y, qRgba(0, 0, 0, 255));
  6. }
  7. }
To copy to clipboard, switch view to plain text mode 
But it is slow when compared to explicitly manipulating the image data.