1

我一直在尝试让 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)

问题是当我尝试它时,组合框一开始不显示任何文本(只有在你点击它之后)。似乎 currentText 属性不起作用。任何帮助将不胜感激。

4

3 回答 3

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

此委托仅在编辑项目时显示组合框,否则将显示普通文本项目委托。

于 2014-03-17T14:01:14.077 回答
1

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

class comboBoxDelegate(QStyledItemDelegate):
    ...
    def displayText(self, value, locale=None):
        return get_appropriate_text_representation_for_value(value)
于 2012-05-02T21:23:28.037 回答
0

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

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

在你的类中的paint方法结束时,在调用之后drawComplexControl

于 2012-04-08T21:59:55.000 回答