Results 1 to 6 of 6

Thread: How to read a color of a pixel on a Widget?

  1. #1

    Default How to read a color of a pixel on a Widget?

    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!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to read a color of a pixel on a Widget?

    you can use QPixmap::grabWidget(), convert it to QImage and then use QImage::pixel().

  3. #3

    Default Re: How to read a color of a pixel on a Widget?

    I just solved it using a similar technique, but thanks for the reply!!!

    Qt Code:
    1. QPixmap pixmap(this->size());
    2. this->render(&pixmap);
    3. QImage img(pixmap.toImage());
    4. QRgb pix = img.pixel(10,10);
    5. std::cout << "Red : " << qRed(pix) << std::endl;
    6. std::cout << "Green : " << qGreen(pix) << std::endl;
    7. std::cout << "Blue : " << qBlue(pix) << std::endl;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to read a color of a pixel on a Widget?

    It should be faster if you open the painter directly on the QImage object instead of the pixmap.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to read a color of a pixel on a Widget?

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to read a color of a pixel on a Widget?

    Quote Originally Posted by Tanuki-no Torigava View Post
    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!

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. How to Open & Close a Widget ?!!
    By Fatla in forum Qt Programming
    Replies: 6
    Last Post: 13th June 2008, 20:39
  3. read and write content of lineedit widget in Other Form
    By validator in forum Qt Programming
    Replies: 1
    Last Post: 25th April 2008, 15:07
  4. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  5. QPixmap pixel by pixel ?
    By tommy in forum Qt Programming
    Replies: 19
    Last Post: 3rd December 2007, 22:52

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.