PySide中的自定义委托

时间:2012-04-05 23:44:06

标签: python qt delegates pyside

我一直试图让tabeView将其中一列显示为组合框。为此,我编写了自定义委托的代码:

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)

问题在于,当我尝试它时,comboBox最初不会显示任何文本(只有在你点击它之后)。似乎currentText属性不起作用。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分: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

此代表只会在编辑项目时显示组合框,否则会显示正常的文本项目委托。

答案 1 :(得分:1)

您可以覆盖QStyledItemDelegate.displayText()方法,使您的委托显示文本而无需重新实现paint()。像

这样的东西
class comboBoxDelegate(QStyledItemDelegate):
    ...
    def displayText(self, value, locale=None):
        return get_appropriate_text_representation_for_value(value)

答案 2 :(得分:0)

我认为你应该调用父类paint()方法。添加:

QStyledItemDelegate.paint(self, painter, option, index)
在调用drawComplexControl

之后,在您的班级的paint方法结束时