PDA

View Full Version : PyQt5 - Get the pixel color inside a QWidget



noob_user
15th September 2020, 05:07
I made a QWidget and inside I made some other items like QLabels which display images.
Consider what is inside that parent Widget I was trying to get the color where I would click.

Searching I found this thread but it is a bit old and I am not able to translate it to Python. as I cant read C++.

thread:
https://www.qtcentre.org/threads/49693-How-to-get-color-of-pixel-or-point

code:

QPixmap qPix = QPixmap::grabWidget(ui->myWidget);
QImage image(qPix.toImage());
QColor color(image.pixel(0, 1));

How would this translate to PyQt5 if it is the correct answer?

d_stranz
15th September 2020, 19:25
Don't double post, please.

QPixmap::grabWidget() has been deprecated and is no longer in Qt5, so this code wouldn't compile even in C++. However, there is an alternative. I am not too familiar with PyQt5, but the code should look something like this (where "widget" the is one you want the pixels from):



pixMap = QPixmap( widget.width(), widget.height() )
widget.render( pixMap )
image = pixMap.toImage()
color = image.pixelColor( 0, 0 )