2

I'm using PySide6 on MacOS Catalina (10.15.7).

I was trying to do something with QColorDialog and getting some unexpected values. I believe this must be due to some colorspace conversions, but I'm not clear on what is happening or how to correct it.

As an example, I painted a patch of color using the hex value #ACC0C6. When I launch a color picker and use the eyedropper to pick the color of that patch, the returned color is hex value #ADC0C7. The code I used is below.

When I used Mac's Digital Color Meter to check the color of the patch, the "Display native values" option shows the expected hex code and the "Display in sRGB" shows the same value given by QColorDialog.

I have a calibrated display, so I'm thinking the display calibration profile is transforming the color. I'm just not at all sure what to do about it.

#!/usr/bin/env python

import sys
from PySide6.QtWidgets import ( 
    QApplication, QColorDialog, QVBoxLayout, QPushButton, QWidget, )
from PySide6.QtCore import ( Qt, QRect, ) 
from PySide6.QtGui import ( QBrush, QColor, QPainter, QPen, )

class Test(QWidget):
    def __init__(self):
        super().__init__()
        self.setMinimumSize(200,200)
        self.layout = QVBoxLayout(self)
        btn = QPushButton("Pick")
        btn.clicked.connect(self.chooseColor) 
        self.layout.addWidget(btn)
        self.layout.addWidget(ColorPatch())
        self.show()

    def chooseColor(self):
        clrpick = QColorDialog()
        clrpick.setOption(QColorDialog.DontUseNativeDialog)
        print(clrpick.testOption(QColorDialog.DontUseNativeDialog))
        color = clrpick.getColor()
        if color.isValid():
            self.colorfg = color

class ColorPatch(QWidget):
    def __init__(self):
        super().__init__()
        self.painter = QPainter()
        self.nopen = QPen()
        self.nopen.setStyle(Qt.NoPen)
    
    def paintEvent(self, event):
        self.painter.begin(self)
        brush = QBrush()
        brush.setColor(QColor('#ACC0C6'))
        brush.setStyle(Qt.SolidPattern)
        self.painter.setPen(self.nopen)
        self.painter.setBrush(brush)
        self.painter.drawRect(0,0,self.width(),self.height())
        self.painter.end()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setAttribute(Qt.ApplicationAttribute.AA_DontShowIconsInMenus, True)
    window = Test()
    sys.exit(app.exec())

Screenshot

BONUS QUESTION: The QColorDialog.DontUseNativeDialog option doesn't seem to work for me even though testing that option before showing the dialog indicates it is set correctly. Is this an issue specific to MacOS or its version (10.15.7 in my case?)

J Bones
  • 627
  • 6
  • 13
  • 2
    The [getColor function](https://doc.qt.io/qt-6/qcolordialog.html#getColor) is static, so the QColorDialog instance you created is never used. Do this instead: `clrpick.getColor(options=QColorDialog.DontUseNativeDialog)`. – ekhumoro Nov 18 '22 at 00:55
  • @ekhumoro - Well that answers my "bonus question." Thanks. – J Bones Nov 18 '22 at 01:02
  • Does the screen picker in the Qt color dialog show the same difference in color values? – ekhumoro Nov 18 '22 at 01:04
  • @ekhumoro - Good question. I can't answer because when I click "Pick Screen Color", the cursor only seems to work within the bounds of the dialog itself. – J Bones Nov 18 '22 at 01:07
  • @ekhumoro - apparently I'm not alone in that issue: https://github.com/pyqtgraph/pyqtgraph/issues/2161 – J Bones Nov 18 '22 at 01:11
  • @JBones Try providing the parent: `QColorDialog.getColor(parent=self)`. – musicamante Nov 18 '22 at 01:16
  • 1
    If setting a parent doesn't work, try: `self.clrpick = QColorDialog(); self.clrpick.open()`. – ekhumoro Nov 18 '22 at 01:22
  • @musicamante - adding parent=self didn't seem to change anything. Cursor still doesn't work outside the dialog. – J Bones Nov 18 '22 at 01:23
  • 2
    See: [QTBUG-84124](https://bugreports.qt.io/browse/QTBUG-84124). – ekhumoro Nov 18 '22 at 01:31
  • 1
    The out-of-window issue might be related to [QTBUG-72548](https://bugreports.qt.io/browse/QTBUG-72548). – musicamante Nov 18 '22 at 01:35
  • @ekhumoro - that kind of works, although it's a bit wonky. The crosshairs cursor changes to the normal arrow cursor and, when not over one of the application's windows, it seems to update slowly or not at all. But, I was able to pick the color in the patch I painted and it *does* pick #ACC0C6 as I had initially expected. (And in contrast to the native picker.) – J Bones Nov 18 '22 at 01:40

0 Answers0