Results 1 to 2 of 2

Thread: PyQt5 - Get the pixel color inside a QWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2020
    Posts
    3
    Qt products
    Platforms
    Windows

    Default PyQt5 - Get the pixel color inside a QWidget

    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/496...pixel-or-point

    code:
    Qt Code:
    1. QPixmap qPix = QPixmap::grabWidget(ui->myWidget);
    2. QImage image(qPix.toImage());
    3. QColor color(image.pixel(0, 1));
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: PyQt5 - Get the pixel color inside a QWidget

    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):

    Qt Code:
    1. pixMap = QPixmap( widget.width(), widget.height() )
    2. widget.render( pixMap )
    3. image = pixMap.toImage()
    4. color = image.pixelColor( 0, 0 )
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. PyQt5 - Get the pixel color inside a QWidget
    By noob_user in forum Newbie
    Replies: 0
    Last Post: 15th September 2020, 05:59
  2. Run an active window inside an PyQT5 GUI application
    By Ircbarros in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2019, 17:25
  3. How to get color pixel in QML?
    By tanthinh1510 in forum Qt Quick
    Replies: 9
    Last Post: 29th October 2014, 07:31
  4. Get QWidget pixel color below mouse Cursor
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 24th May 2010, 18:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.