[Interest] QImage::Format_Grayscale8 work working

Jason H jhihn at gmx.com
Thu Mar 15 21:21:25 CET 2018

Given the following functions, I should be able to create a non-all black image (assuming input is good)? The only success I have is using Indexed8

QHash<QRgb, qint32 (*)(QRgb)> colorFuncs {
	{ Qt::red,   qRed},
	{ Qt::green, qGreen},
	{ Qt::blue,  qBlue},
	{ Qt::gray,  qGray},

QImage color8(const QImage &image, int channel) { // Channel is one of Qt::red Qt::green Qt::blue or Qt::gray
	QImage out(image.width(), image.height(), QImage::Format_Indexed8);  // Change to Format_Grayscale8, and get nothing
// for indexed8
	QVector<QRgb> values;
	if (channel==Qt::gray) { for (int c=0; c<256; c++) values.append(qRgb(c,c,c)); }
	if (channel==Qt::red)  { for (int c=0; c<256; c++) values.append(qRgb(c,0,0)); }
	if (channel==Qt::green){ for (int c=0; c<256; c++) values.append(qRgb(0,c,0)); }
	if (channel==Qt::blue) { for (int c=0; c<256; c++) values.append(qRgb(0,0,c)); }
// end for indexed8

	int (*colorFunc)(QRgb rgb) = colorFuncs[channel];
	for (int y=0; y < image.height(); y++) {
		for (int x=0; x < image.width(); x++) {
			out.setPixel(x,y, colorFunc(image.pixel(x,y)));

	return out;

I'm not familar with Qt and Grayscale8... Anyone know what is going wrong?

More information about the Interest mailing list