[PyQt] QColorDialog: saving custom colors on Mac

Nathan Weston nathan at genarts.com
Mon Jul 18 17:06:05 BST 2011

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?

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. :)

Here's my complete code. I'm using the v2 QVariant API, but I get 
similar results with the v1 API.

import sip
sip.setapi('QVariant', 2)
from PyQt4.QtCore import *
from PyQt4.QtGui import *

settings = QSettings()
c = QColorDialog.customColor(0)
print 'Original color:', c, type(c)
settings.setValue('color', c)

settings = QSettings()
c = settings.value('color')
print 'Restored color:', c, type(c)
QColorDialog.setCustomColor(0, settings.value('color'))

More information about the PyQt mailing list