2012-04-05 10 views
1

私はtabeViewを取得して、そのカラムの1つをcomboBoxとして表示しようとしていました。これを行うために、私は、カスタムデリゲートのコードを書いている:PySideのカスタムデリゲート

class comboBoxDelegate(QStyledItemDelegate): 

def __init__(self, model, parent=None): 
    super(comboBoxDelegate, self).__init__(parent) 
    self.parent= parent 
    self.model= model 

def createEditor(self, parent, option, index): 

    if not index.isValid(): 
     return False 

    self.currentIndex=index 

    self.comboBox = QComboBox(parent) 
    self.comboBox.setModel(self.model) 
    value = index.data(Qt.DisplayRole) 
    self.comboBox.setCurrentIndex(value) 

    return self.comboBox 

def setEditorData(self, editor, index): 
    value = index.data(Qt.DisplayRole) 
    editor.setCurrentIndex(value) 

def setModelData(self, editor, model, index): 

    if not index.isValid(): 
     return False 

    index.model().setData(index, editor.currentIndex(), Qt.EditRole) 

def paint(self, painter, option, index): 
    currentIndex= index.data(Qt.DisplayRole) 

    opt= QStyleOptionComboBox() 
    opt.rect= option.rect 
    currentComboIndex= self.model.createIndex(currentIndex,0) 
    opt.currentText= self.model.data(currentComboIndex, Qt.DisplayRole) 

    QApplication.style().drawComplexControl(QStyle.CC_ComboBox, opt, painter) 

問題は、私はそれをしようとすると、コンボボックスは、あなたがクリックしただけで後は(最初は任意のテキストが表示されないということですそれ)。 currentTextプロパティが機能していないようです。どんな助けもありがとう。

+0

'self.model.data(currentComboIndex、Qt.DisplayRole)'が実際に有効な文字列を返すかどうかチェックしましたか? – Masci

+0

ありがとう、はい、それは文字列を返します –

答えて

0

親クラスpaint()メソッドを呼び出す必要があると思います。追加:

QStyledItemDelegate.paint(self, painter, option, index) 

あなたのクラスのpaintメソッドの最後で、drawComplexControl

+0

それは動作します、ありがとう! –

+0

'QStyledItemDelegate.paint(self、painter、option、index) 'を呼び出すと、その前に行われたペイントが完全に取り消されます。あなたが 'paint'の最後のステートメントとしてこれを常に呼び出すなら、' paint'をオーバーライドしないでください。 – Nathan

1

への呼び出しの後にあなたが(塗料を再実装することなく、あなたのデリゲートの表示テキストを作るためにQStyledItemDelegate.displayText()メソッドをオーバーライドすることができます)。何かのように

class comboBoxDelegate(QStyledItemDelegate): 
    ... 
    def displayText(self, value, locale=None): 
     return get_appropriate_text_representation_for_value(value) 
2

私はこれが古いと知っていますが、本当に絵を扱う必要はありません。コンボボックスの現在のインデックスはintの代わりに文字列として設定されている可能性があるため、コンボボックスは値を表示しません。

class ComboBoxDelegate(QtGui.QStyledItemDelegate): 
    """ComboBox view inside of a Table. It only shows the ComboBox when it is 
     being edited. 
    """ 
    def __init__(self, model, itemlist=None): 
     super().__init__(model) 
     self.model = model 
     self.itemlist = None 
    # end Constructor 

    def createEditor(self, parent, option, index): 
     """Create the ComboBox editor view.""" 
     if self.itemlist is None: 
      self.itemlist = self.model.getItemList(index) 

     editor = QtGui.QComboBox(parent) 
     editor.addItems(self.itemlist) 
     editor.setCurrentIndex(0) 
     editor.installEventFilter(self) 
     return editor 
    # end createEditor 

    def setEditorData(self, editor, index): 
     """Set the ComboBox's current index.""" 
     value = index.data(QtCore.Qt.DisplayRole) 
     i = editor.findText(value) 
     if i == -1: 
      i = 0 
     editor.setCurrentIndex(i) 
    # end setEditorData 

    def setModelData(self, editor, model, index): 
     """Set the table's model's data when finished editing.""" 
     value = editor.currentText() 
     model.setData(index, value) 
    # end setModelData 
# end class ComboBoxDelegate 

このデリゲートは、アイテムが編集中の場合のみコンボボックスを表示し、それ以外の場合は通常のテキストアイテムデリゲートを表示します。