models & editors in PyQt4

David Boddie david at boddie.org.uk
Wed Aug 30 11:35:23 EDT 2006


Skink wrote:

> I created simple property classes with editing option, but since i'm not
> too much experienced in PyQt4 i'd like to ask if i handle ColorProperty
> changing right. Any other Property has its own editor and their control
> flow is imho ok. Hovewer i'm not sure about ColorProperty.
>
> What i do is: in createEditor(self, parent, option, index) i call
> QtGui.QColorDialog.getColor() and return None
>
> What do you think? Is it Ok?

It should be OK - it shouldn't crash, anyway. It depends on the view
doing the right thing when it finds that it hasn't received an editor
widget.

You could create an empty placeholder widget in the createEditor()
method, call QColorDialog.getColor() with the existing color from the
model in setEditorData(), record the color returned by the dialog in
some internal instance variable, and finally set the data in the model
when setModelData() is called:

class ColorProperty(Property):
  ...
  def createEditor(self, parent, option, index):
      return QtGui.QWidget(parent)
  def setEditorData(self, editor, index):
      self.color = QtGui.QColorDialog.getColor(
          index.model().getObjectData(self.name()))
  def setModelData(self, editor, model, index):
      if self.color.isValid():
          index.model().setObjectData(self.name(), self.color)
  ...

I find it strange that you have to triple-click to edit any of the
items in your example. Do you see the same behaviour?

David




More information about the Python-list mailing list