Custom delegate for color picker- PySide -Maya

Hey guys,

My first post here!! Awesome forum!! :slight_smile:

So, I’m working on a Lights Manager for our lighting team. Basically an Attribute Spreadsheet specifically for lighting, some render engines and our pipeline. I’m using custom delegates to create specific editors on a TableView widget. I need some sort of editor to be able to work with a cell on my table which contains color info so I went with QColorDialog.

Problem is when I pass QColorDialog as an editor to my custom delegate, it doesn’t work. I cheated a lot (I know it’s wrong, but just wanted to understand what was going on) and got it to sort of work but it doesn’t take into account user input regarding Accepting or Canceling the dialog edits.

Could you guys please point me in the right direction on how to proceed with this problem? Some possible ways I’ll research: QItemEditorFactory and QItemEditorCreatorBase, the starrating.py example included with PySide (which is complex, but seems to be the most customizable).

Thanks in advance for all the help you can give me!!

The summarized code for the delegate is this:

class PropertiesDelegate(QtGui.QItemDelegate):
	def __init__(self, parent=None):
		super(PropertiesDelegate, self).__init__(parent)
		
	def createEditor(self, parent, option, index):
                dataList = index.model().getDataList()
		currentValue = dataList[index.row()][index.column()]
		thisColor = QtGui.QColor(currentValue)
		colorDialog = QtGui.QColorDialog(self.parent)
		colorDialog.setCurrentColor(thisColor)
		return colorDialog

	def setModelData(self, editor, model, index):
		modelHeaders = index.model().getHeaders()
		dataList = index.model().getDataList()
		if modelHeaders[index.column()] == "default":
			uiControl = dataList[index.row()][2]
			if uiControl == "colorswatch":
				model.setData(index, editor.colorSelected().name()) #If I use selectedColor(), in this particular case, it always returns black
		else:
			QtGui.QItemDelegate.setModelData(self, editor, model, index)
	
	def setEditorData(self, editor, index):
		QtGui.QItemDelegate.setEditorData(self, editor, index)


I’ll respond to myself in case anyone needs an answer to this in the future.

Delegates for this case are not the best way to go. My approach was an incorrect use of QItemDelegate.createEditor() anyway.

Found the solution here: Qt Centre Forum

connect to the view’s doubleClicked() signal and upon that signal call QColorDialog::getColor() and afterwards set the model’s data using QAbstractItemModel::setData().

Hope it helps someone else!!