Thanks for your very fast reply,

I'm sorry, I got error message with that code from QImage that the pixel I try to set is always out of range (QImage::setPixel: index=15 out of range). Then I tried to print img.numColors(), and I found that it always returns 0, even though I've set the format to 8 bit indexed (QImage::Format_Indexed8). However if I set the numColors manually via img.setNumColors(256), that piece of code works, the QWidget and also img.save() produce correct images.

I still don't understand why 8 bit indexed can have 0 numColors() ? From the doc, I read QImage will not return 0 from image that uses color table. Or maybe I miss something?

Thanks btw