Results 1 to 4 of 4

Thread: How to get RGB color from pixel under cursor

  1. #1
    Join Date
    Jul 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question How to get RGB color from pixel under cursor

    Helo,
    i want to get RGB color from under of cursor after click on image.
    I have this code
    Qt Code:
    1. self.label_10 = QLabel(self.tab_2)
    2. pixmap = QPixmap('rgb.png')
    3. self.label_10.setPixmap(pixmap)
    4. self.label_10.setGeometry(QtCore.QRect(20, 40, 391, 241))
    5. self.label_10.setObjectName("rgb_image")
    6. self.label_10.resize(pixmap.width(),pixmap.height())
    7. self.label_10.mousePressEvent = self.getPos
    8.  
    9. def getPos(self , event):
    10. x = event.pos().x()
    11. y = event.pos().y()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to get RGB color from pixel under cursor

    QImage is used for accessing the image data.
    QPixmap can return a QImage object.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get RGB color from pixel under cursor

    Now function looks like that.
    But is return always: 12345
    Qt Code:
    1. def getPos(self , event):
    2. x = event.pos().x()
    3. y = event.pos().y()
    4. q = QImage()
    5. rgb = q.pixel(x, y)
    6. print ("color")
    7. print (rgb)
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to get RGB color from pixel under cursor

    You are initializing an empty QImage() not the one in your QPixmap.
    As I said in my previous post:
    QPixmap can return a QImage object.
    Get a QImager from your QPixmap and then access the pixel.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. How to get color pixel in QML?
    By tanthinh1510 in forum Qt Quick
    Replies: 9
    Last Post: 29th October 2014, 07:31
  2. How to get color of pixel or point?
    By qt_developer in forum Newbie
    Replies: 2
    Last Post: 28th June 2012, 20:14
  3. Get QWidget pixel color below mouse Cursor
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 24th May 2010, 18:17
  4. How to get pixel color from QPainter ??
    By rameshg87 in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2008, 09:58
  5. getting color from a pixel on canvas
    By teeshift in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 05:12

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.