PDA

View Full Version : Problem with pixelIndex in QImage



danilodsp
16th September 2011, 16:03
I do not understand this problem.
I normally use a QImage, but now this happening a problem that I can not see.

In my .h:

QImage *mascara; // Máscara para filtro

In my .cpp:

mascara = new QImage(2,2,QImage::Format_Indexed8);

mascara->setPixel(0,0,5);
mascara->setPixel(0,1,5);
mascara->setPixel(1,0,5);
mascara->setPixel(1,1,5);
QString test;
test.setNum(mascara->pixelIndex(0,0));
ui->console->appendPlainText(test);

I do not see the pixel that I placed. In test no have the value 5.

stampede
16th September 2011, 16:48
I guess its because you forgot to set a color table. In the used image format, pixel value is an index in color table, which you have to set before specifying pixel values.
Something like this will work:


QImage img(2,2,QImage::Format_Indexed8);
img.setColor(0,5);
img.setColor(1,3);
img.setPixel(0,0,0);
img.setPixel(0,1,1);
qDebug() << img.pixel(0,0); // prints 5
qDebug() << img.pixel(0,1); // prints 3

Its explained in docs with more details : link (http://doc.qt.nokia.com/latest/qimage.html#pixel-manipulation)

danilodsp
17th September 2011, 06:14
Thank you very much!