[PyQt] QColorDialog: saving custom colors on Mac

Phil Thompson phil at riverbankcomputing.com
Fri Jul 29 18:12:32 BST 2011

On Mon, 18 Jul 2011 12:06:05 -0400, Nathan Weston <nathan at genarts.com>
> I'm trying to save the custom colors of QColorDialog using QSettings. On

> Windows this is working correctly, but on Mac I'm having some problems:
> 1. QColorDialog.customColor() returns a long instead of an int
> 2. When I save that long using QSettings, it comes back as an int with a

> value of -1
> This seems like a bug in Qt or PyQt to me. Am I missing something?

It's the usual problem with QSettings not being able to restore the value
exactly because it doesn't retain sufficient type information. In this case
the fact that the value is unsigned and not signed (the bit pattern is the

You should always use the 'type' keyword argument to QSettings.value() to
explicitly state what you are expecting back. As Python doesn't have an
unsigned type you have to specify a C++ type...

    settings.value('color', type='uint')

...should work.

> Related to this, when you pass a negative int to 
> QColorDialog.setCustomColor(), it throws this rather baffling exception:
>    TypeError: QColorDialog.setCustomColor(int, int): argument 2 has 
> unexpected type 'int'
> It seems like ValueError would be more appropriate. At the very least, a

> more accurate error message would have saved me about 20 minutes of 
> scratching my head. :)

It's actually an overflow. In tonight's SIP snapshot I've switched to
using PyLong_AsUnsignedLongMask() which ignores overflows. SIP ignores
overflows anyway when converting to small C++ types (eg. short) so this
makes the behavior more consistent and means that you can pass -1 in the
call above.


More information about the PyQt mailing list