tobspr tobspr - 7 months ago 11
Python Question

Check if QColorDialog got cancelled

I'm using a

QColorDialog
in PyQt5 to let the user select a color. My code looks like this:

result = QColorDialog.getColor(QColor(self.red, self.green, self.blue))
self.red, self.green, self.blue = result.red(), result.green(), result.blue()


This works fine so far, however I am unable to detect whether the user cancelled the dialog.

Even when the dialog is canceled, the returned color is
QColor(0, 0, 0, 255)
, and not
None
, as expected.

Is there a way to detect whether the dialog got canceled using the static
getColor
method? Or do I have to manually construct a
QColorDialog
instance?

Answer

Use QColor::isValid to check if the color returned is a valid color.