python - PyQt: Using QTextEdit as editor in a QStyledItemDelegate -


i looking clean way use multi line text edit in qstyleditemdelegate. createeditor implementation pretty straight forward returning qtextedit instance

def createeditor(self, parent, option, index):     return qtgui.qtextedit("some text") 

but setmodeldata expects edit widget derived qwidget parameter instead of qtextedits base qscrollarea. qt documentation tells me (at least in pyqt doc) setmodeldata function tries data qwidget userdata field. without having edit widget derived qwidget there no option in setting data. throws attributeerror because can't find text() on editor.

is there proven way use non-qwidget editor? or missing widget that?

currently quick fixed issue instanciating qlineedit qtextedit data toplaintext() , passing setmodeldata. hacky!! use duck typing , implement text() method on qtextedit derivate. still not nice way, isn't it? way in c++?

so took time , digged deeper problem. there in fact multiple cleaner solutions:

  1. implementing property got work implementing qproperty on subclass of qtextedit adds text property user property object. here code:

    class delegatabletextedit(qtgui.qtextedit):      @pyqtproperty(str, user=true)     def text(self):          return self.toplaintext()      @text.setter     def text(self, text):          self.settext(text) 
  2. using factory there seems way solve using delegates setdefaultfactory() method takes qitemeditorfactory has registered custom editor creator using registereditor() function. can register custom implementation of qitemeditorcreatorbase class there has overriden createwidget method (and if needed valuepropertyname function well). haven't tried 1 yet

i decided take first solution needs custom qtextedit , overriden createeditor() function. both should work in c++


Comments