PDA

View Full Version : How to read a color of a pixel on a Widget?



wringer
3rd December 2009, 15:59
Is there any way to read a color of a pixel on a widget? i.e. read the color of a pixel right under my mouse pointer. I've been searching in various places and I'm slowly starting to think that QT doesn't support such functionality, but I might just be looking at the wrong place. Please give some advice to a newbie!!! :)

Lykurg
3rd December 2009, 16:23
you can use QPixmap::grabWidget(), convert it to QImage and then use QImage::pixel().

wringer
3rd December 2009, 16:29
I just solved it using a similar technique, but thanks for the reply!!! :D


QPixmap pixmap(this->size());
this->render(&pixmap);
QImage img(pixmap.toImage());
QRgb pix = img.pixel(10,10);
std::cout << "Red : " << qRed(pix) << std::endl;
std::cout << "Green : " << qGreen(pix) << std::endl;
std::cout << "Blue : " << qBlue(pix) << std::endl;

wysota
3rd December 2009, 16:55
It should be faster if you open the painter directly on the QImage object instead of the pixmap.

Tanuki-no Torigava
4th December 2009, 00:31
If you are using the mouse - do it through QMouseEvent with QEvent::MouseMove. So you will get theright coordinates and then call QImage through QPainter. That is faster.

Lykurg
4th December 2009, 06:31
So you will get theright coordinates and then call QImage through QPainter. That is faster.
Only if you cache the image, because rendering the image every time you move the mouse is damn slow!