tags:

views:

17

answers:

0

I tried to implement QItemEditorCreatorBase to get QComboBox editor for colors in a table. So I wrote an editor:

class ColorListEditor(QtGui.QComboBox):
def __init__(self, parent=None):
    super(ColorListEditor, self).__init__(parent)
    self.populateList()

def populateList(self):
    for i, name in enumerate(QtGui.QColor.colorNames()):
        self.insertItem(i, name)
        self.setItemData(i, QtCore.QVariant(QtGui.QColor(name)),
                         Qt.DecorationRole)

def color(self):
    return self.itemData(self.currentIndex(), Qt.DecorationRole).toPyObject()

def setColor(self, color):
    self.setCurrentIndex(self.findData(QtCore.QVariant(color), 
                                       Qt.DecorationRole))

color = QtCore.pyqtProperty('QColor', color, setColor, user=True)

and a creator:

class ColorListCreator(QtGui.QItemEditorCreatorBase):
def __init__(self):
    super(ColorListCreator, self).__init__()

def createWidget(self, parent):
    return ColorListEditor(parent)

After that I registered editor:

    factory = QtGui.QItemEditorFactory();
    factory.registerEditor(QtCore.QVariant.Color, ColorListCreator());
    QtGui.QItemEditorFactory.setDefaultFactory(factory);

Now I have a table with color columnt. When I double click on cell in this column the combobox successfully appears (the color, that was in the cell, is selected in editor). But when I select new color and leave editing the new color of the cell is always #000000.

So it seems like my editor returns new color with incorrectly, but I can't understand, what is wrong with it.

I've found an example (implementing of coloreditorfactory Qt-example, that doesn't exist in PyQt) here, but the problem is the same.

Tell me please, if you know what's wrong with my editor.

I'm using python 2.6 and PyQt 4.7.3 with it.